The while and do-while statements in C#

 

While Statement:

A while statement conditionally executes a statement zero or more times – as long as a boolean test is true.

 

public class Employee

{

        public void FindEmployee(string[] arrNames, string name)

        {

            int i = 0;

            while (arrNames[i] != name)

            {

                if (i > arrNames.Length)

                    return;

                Console.WriteLine("Wrong Employee Details");

                i = i + 1;

            }

            Console.ReadLine();

        }

}

 

As shown above FindEmployee() method displays wrong Employee details.

 

do-while Statement:

A do statement conditionally executes a statement one or more times.

 

public class Employee

{

        public void FindEmployee(string[] arrNames, string name)

        {

            int i = 0;

            do

            {

                if (i > arrNames.Length)

                    return;

                Console.WriteLine("Wrong Employee Details");

                i = i + 1;

            }

            while (arrNames[i] != name);

            Console.ReadLine();

        }

 }