Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - WHILE LOOP


A loop is a concept where something continues endlessly until it is asked to stop.


Just like the sun rises in the east and sets in the west is a loop that continues endlessly.


Let us take the below example to understand loops.


Say, you are asked to print the String "Hello World", five times on the screen.


A simple solution would be,


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World");
	    System.Console.WriteLine("Hello World");
	    System.Console.WriteLine("Hello World");
	    System.Console.WriteLine("Hello World");
	    System.Console.WriteLine("Hello World");
    }
}


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

So, we have printed "Hello World" five times on the screen.


Simple! Right?


But, what if you are asked to print "Hello World" 100 times or even 1000 times on the screen.


You can't just write 100 or 1000 System.Console.WriteLine(...) statements.


And this is where loops comes into picture.


And while loop is one kind loop used by C#.


While Loop


Let us solve the above problem of printing "Hello World" 100 or 1000 times using while loop.


For now, let us rewrite the same program to print "Hello World" 5 times using while loop


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int x = 1;
        while (x <= 5) 
        {
            System.Console.WriteLine("Hello World");
            x = x + 1;
        }    
    }
}


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

So, in the above code, what we have done is, created a variable named x and initialised it with 1.


int x = 1;
C_Sharp


Then we have used the while loop to print "Hello World" 5 times.


while (x <= 5) {
	System.Console.WriteLine("Hello World");
	x = x + 1;
}

So, the first line,


while (x <= 5)

Of the while loop says, continue to be in the loop until the value of x is less than or equal to 5 (i.e. x <= 5).


And starts the Iteration(You will understand it eventually).


1st Iteration


So, the control goes inside the while loop and encounters the System.Console.WriteLine(...) statement.


System.Console.WriteLine("Hello World")

And it print "Hello World" for the first time.

Output :



  Hello World

Then it finds the statement,


x = x + 1;

Where the value of x is incremented by 1. So, if you look above, the value of x is 1.After increasing the value by 1, the new value of x becomes 2.

C_Sharp

After executing both the statements in the while loop, the control comes back to the while condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of x is less than or equal to 5.


And right now the value of x is 2. So, the control cannot exit the loop and gets into the while loop for second iteration.


2nd Iteration


Once again, the control goes inside the while loop and encounters the System.Console.WriteLine(...) statement.


System.Console.WriteLine("Hello World");

And it print "Hello World" for the second time.

Output :



  Hello World
  Hello World

Again it finds the statement,


x = x + 1;

Where the value of x is incremented by 1. So, if you look above, the value of x is 2.After increasing the value by 1, the new value of x becomes 3.

C_Sharp

After executing both the statements in the while loop, the control comes back to the while condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of x is less than or equal to 5.


And right now the value of x is 3. So, the control cannot exit the loop and gets into the while loop for third iteration.


3rd Iteration


Similarly, the control goes inside the while loop and encounters the System.Console.WriteLine(...) statement.


System.Console.WriteLine("Hello World");

And it print "Hello World" for the third time.

Output :



  Hello World
  Hello World
  Hello World

Again it finds the statement,


x = x + 1;

Where the value of x is incremented by 1. So, if you look above, the value of x is 3.After increasing the value by 1, the new value of x becomes 4.

C_Sharp

After executing both the statements in the while loop, the control comes back to the while condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of x is less than or equal to 5.


And right now the value of x is 4. So, the control cannot exit the loop and gets into the while loop for fourth iteration.


4th Iteration


Similarly, the control goes inside the while loop and encounters the System.Console.WriteLine(...) statement.


System.Console.WriteLine("Hello World");

And it print "Hello World" for the fourth time.

Output :



  Hello World
  Hello World
  Hello World
  Hello World

Again it finds the statement,


x = x + 1;

Where the value of x is incremented by 1.

C_Sharp

After executing both the statements in the while loop, the control comes back to the while condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of x is less than or equal to 5.


And right now the value of x is 5. So, the control cannot exit the loop and gets into the while loop for fourth iteration.


5th Iteration


Similarly, the control goes inside the while loop and encounters the System.Console.WriteLine(...) statement.


System.Console.WriteLine("Hello World")

And it print "Hello World" for the fifth time.

Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

Again it finds the statement,


x = x + 1

Where the value of x is incremented by 1.

C_Sharp

After executing both the statements in the while loop, the control comes back to the while condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of x is less than or equal to 5.


And right now the value of x is 6. And the condition x <= 5 fails and the control gets out of the while loop exiting it.


And if you see the final output, "Hello World" is printed 5 times.


Even if you want to print "Hello World" 100 times or 1000 times. Just change the conditionalpart of while loop and you are done.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int x = 1;
        while (x <= 1000) {
            System.Console.WriteLine("Hello World");
            x = x + 1;
        }   
    }
}



So, the above program prints "Hello World" 1000 times.


Now, let us look at the syntax of while loop.


while (x <= 1000) {
	System.Console.WriteLine("Hello World");
	x = x + 1;
}

The while loop starts with while statement, followed by the conditional part x <= 1000, inside brackets ().


while (x <= 1000)

And all the statements inside the while loop should be inside braces/flower brackets {}.


while (x <= 1000) {
	System.Console.WriteLine("Hello World");
	x = x + 1;
}

Next, let us look at the do - while loop.