Ads block

Banner 728x90px

c#step18


C# - for loop

The for keyword indicates a loop in C#. The for loop executes a block of statements repeatedly until the specified condition returns false.
Syntax:
for (variable initialization; condition; steps)
{
    //execute this code block as long as condition is satisfied 
}
As per the syntax above, the for loop contains three parts: initialization, conditional expression and steps, which are separated by a semicolon.
  1. variable initialization: Declare & initialize a variable here which will be used in conditional expression and steps part.
  2. condition: The condition is a boolean expression which will return either true or false.
  3. steps: The steps defines the incremental or decremental part
Consider the following example of a simple for loop.
Example: for loop
for (int i = 0; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 0 
Value of i: 1 
Value of i: 2 
Value of i: 3 
Value of i: 4 
Value of i: 5 
Value of i: 6 
Value of i: 7 
Value of i: 8 
Value of i: 9
The below figure illustrates the execution steps of above example.
For loop
for loop execution steps
As you can see in the above example, first step is to declare & initialize an int type variable. The second step is to check the condition. The third step is to execute the code block if the 'if' condition returns true. The fourth step is to increment the int variable and last step is to eveluate the condition again and repeat the steps.
It is not necessary to put the initialization, condition and steps into brackets. You can initialize a variable before the 'for' loop, and the condition and steps can be defined inside the for loop.
Example: for loop C#
int i = 0;

for(;;)
{
    if (i < 10)
    {
        Console.WriteLine("Value of i: {0}", i);
        i++;
    }
    else
        break;
}
Output:
Value of i: 0 
Value of i: 1 
Value of i: 2 
Value of i: 3 
Value of i: 4 
Value of i: 5 
Value of i: 6 
Value of i: 7 
Value of i: 8 
Value of i: 9

Infinite for Loop

Be careful with infinite loop. It will be an infinite loop if for loop does not contain initialization, condition or steps part. Also, make sure that conditional expression will return false at some point of time to stop the looping.
Example: Infinite loop
for (  ;  ; )
{
    Console.Write(1);
}
Output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.....
The control variable for the for loop can be of any numeric data type, such as double, decimal, etc.
Example: for loop
for (double d = 1.01D; d < 1.10; d+= 0.01D)
{
    Console.WriteLine("Value of i: {0}", d);
}
Output:
Value of i: 1.01 
Value of i: 1.02 
Value of i: 1.03 
Value of i: 1.04 
Value of i: 1.05 
Value of i: 1.06 
Value of i: 1.07 
Value of i: 1.08 
Value of i: 1.09
The steps part in a for loop can either increase or decrease the value of a variable.
Example: for loop
for(int i = 10; i> 0;i--)
{
    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 10 
Value of i: 9 
Value of i: 8 
Value of i: 7 
Value of i: 6 
Value of i: 5 
Value of i: 4 
Value of i: 3 
Value of i: 2
Value of i: 1

break in for loop

You can also exit from a for loop by using the break keyword.
Example: break in for loop
for (int i = 0; i < 10; i++)
{
    if( i == 5 )
        break;

    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 0 
Value of i: 1 
Value of i: 2 
Value of i: 3 
Value of i: 4

Nested for Loop

C# allows a for loop inside another for loop.
Example: Nested for loop
for (int i = 0; i < 10; i++)
{
    for(int j =i; j< 10; j++)
        Console.WriteLine("Value of i: {0}, J: {1} ", i,j);
}
Output:
Value of i: 0 , j: 0 
Value of i: 0 , j: 1 
Value of i: 0 , j: 2 
Value of i: 0 , j: 3 
Value of i: 0 , j: 4 
Value of i: 0 , j: 5 
Value of i: 0 , j: 6 
Value of i: 0 , j: 7 
Value of i: 0 , j: 8 
Value of i: 0 , j: 9 
Value of i: 1 , j: 1 
Value of i: 1 , j: 2 
Value of i: 1 , j: 3 
Value of i: 1 , j: 4 
Value of i: 1 , j: 5 
Value of i: 1 , j: 6 
Value of i: 1 , j: 7 
Value of i: 1 , j: 8 
Value of i: 1 , j: 9 
Value of i: 2 , j: 2 
Value of i: 2 , j: 3 
Value of i: 2 , j: 4 
Value of i: 2 , j: 5 
Value of i: 2 , j: 6 
Value of i: 2 , j: 7
 Points to Remember :
  1. The for loop executes the block of code repeatedly.
  2. The for loop has three steps: initialization, condition and increment/decrement.
  3. The for loop can use control variable of any numeric data type.
  4. Use break keyword to stop the execution and exit from for loop.
  5. Nested for loop is allowed in C#.

No comments:

Post a Comment