The for loop in C# is like the while loop. But the for loop is way more powerful than the while loop.
The for loop has inbuilt features, by which you can reduce lines of code.
So far we have seen, how while loop was used to print "Hello World" 5 times.
public class MyApplication { public static void Main(string[] args) { int x = 1; while (x <= 5) { System.Console.WriteLine("Hello World"); x = x + 1; } } }
The same program could be reduced to less lines of code using the for loop.
Let us see below.
Let us use for loop to print "Hello World" five times on the screen.
public class MyApplication { public static void Main(string[] args) { for(int x = 1; x <= 5 ; x++) { System.Console.WriteLine("Hello World"); } } }
Now, if we break the contents of for loop, we get :
All together forms the for loop.
for(int x = 1; x <= 5 ; x++) { System.Console.WriteLine("Hello World"); }
So in the 1st iteration,
The control gets into the for loop and checks, if the value of x is less than or equal to 5.
In this case the value of x is 1. Which is less than 5.
So, the control enters the for loop.
for(int x = 1; x <= 5 ; x++) { System.Console.WriteLine("Hello World"); }
and finds the System.Console.WriteLine statement,
System.Console.WriteLine("Hello World");
So, it prints Hello World.
Next, C# finds, it has come to the end of the loop.
So, it increments the value of x by 1 (And C# does the increment internally).
And the value of x becomes 2.
And the second Iteration starts.
Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5(i.e. Internally C# checks if i <= 5).
In this case the value of x is 2. Which is less than 5.
So, the control enters the for loop.
And finds the print statement,
System.Console.WriteLine("Hello World");
So, it print Hello World for the second time.
Similarly, C# finds, it has come to the end of the loop.
And increments the value of x by 1.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.
Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
In this case, the value of x is 6. Which more than 5.
And the control comes out of the for loop.
Now, if you see the output, Hello World is exactly printed 5 times.
So, we have seen, how to print Hello World 5 times.
Now, let us see, how can we print the numbers from 1 to 5.
public class MyApplication { public static void Main(string[] args) { for(int x = 1; x <= 5 ; x++) { System.Console.WriteLine(x); } }
The above code is self explanatory.
However, we have provided the explanation below.
So in the 1st iteration,
The control gets into the for loop and checks, if the value of x is less than or equal to 5.
In this case the value of x is 1. Which is less than 5.
So, the control enters the for loop.
for(int x = 1; x <= 5 ; x++) { System.Console.WriteLine(x); }
and finds the print statement,
System.Console.WriteLine(x);
So, it print the value of x, which is currently 1.
Next, C# finds, it has come to the end of the loop.
So, it increments the value of x by 1 (And C# does the increment internally).
And the value of x becomes 2.
And the second Iteration starts.
Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
In this case the value of x is 2. Which is less than 5.
So, the control enters the for loop.
And finds the System.Console.WriteLine statement,
System.Console.WriteLine(x)
So, it print the value of x, which is 2 currently.
Similarly, C# finds, it has come to the end of the loop.
And increments the value of x by 1.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.
Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.
In this case, the value of x is 6. Which more than 5.
And the control comes out of the for loop.
We will be learning about it in the Arrays section of C#.