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




C# - FOR LOOP


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.


Example :



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.


Example of 'for' loop:


Let us use for loop to print "Hello World" five times on the screen.


Example :



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


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

Now, if we break the contents of for loop, we get :

  1. Initialisation part : It is run before the loop starts.


    C_Sharp


    x is getting initialized with value 1.

  2. Conditional part : It states the condition for running the loop.


    C_Sharp


    It says, continue to be in the loop until the value of x is not more than 5.

  3. Increment part : It is run each time after the loop.


    C_Sharp


    x++ is the Increment part. i.e. Same as x = x + 1.

All together forms the for loop.


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

So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

C_Sharp
C_Sharp

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.

Output :



  Hello World

Next, C# finds, it has come to the end of the loop.

C_Sharp

So, it increments the value of x by 1 (And C# does the increment internally).


And the value of x becomes 2.

C_Sharp

And the second Iteration starts.


2nd Iteration


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).

C_Sharp

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

C_Sharp

And finds the print statement,


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

So, it print Hello World for the second time.

Output :



  Hello World
  Hello World

Similarly, C# finds, it has come to the end of the loop.

C_Sharp

And increments the value of x by 1.

C_Sharp

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  Hello World
  Hello World
  Hello World
C_Sharp

4th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
C_Sharp

5th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World
C_Sharp

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

C_Sharp

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.


Printing the numbers 1 to 5 using 'for' loop


Example :



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


Output :



  1
  2
  3
  4
  5

The above code is self explanatory.


However, we have provided the explanation below.


So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

C_Sharp
C_Sharp

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.

Output :



  1

Next, C# finds, it has come to the end of the loop.

C_Sharp

So, it increments the value of x by 1 (And C# does the increment internally).


And the value of x becomes 2.

C_Sharp

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

C_Sharp

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

C_Sharp

And finds the System.Console.WriteLine statement,


System.Console.WriteLine(x)

So, it print the value of x, which is 2 currently.

Output :



  1
  2

Similarly, C# finds, it has come to the end of the loop.

C_Sharp

And increments the value of x by 1.

C_Sharp

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  1
  2
  3
C_Sharp

4th Iteration

Output :



  1
  2
  3
  4
C_Sharp

5th Iteration

Output :



  1
  2
  3
  4
  5
C_Sharp

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

C_Sharp

In this case, the value of x is 6. Which more than 5.


And the control comes out of the for loop.


Example of for each loop:


We will be learning about it in the Arrays section of C#.