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,
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"); } }
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#.
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
public class MyApplication { public static void Main(string[] args) { int x = 1; while (x <= 5) { System.Console.WriteLine("Hello World"); x = x + 1; } } }
So, in the above code, what we have done is, created a variable named x and initialised it with 1.
int x = 1;
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Again it finds the statement,
x = x + 1;
Where the value of x is incremented by 1.
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.
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.
Again it finds the statement,
x = x + 1
Where the value of x is incremented by 1.
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.
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.