Decision making and looping – for and foreach loops in C#

In the previous blog we spoke about the while and do loops under the decision making and looping concepts, under the decision making and looping-for and foreach loops are discussed in detail here.

The for loop in C#

for loop is another entry-controlled loop that provides more concise loop control structure. The for loop takes the following form

for ( initialization ; test condition ; increment )
{
  body of the loop
}


the execution of the for statement is as follows:

  1. initialization of the variables are done first, using the assignment statements such as  i = 1,  count = 0 and so on and so forth here the i and count are known as loop control variables.
  2. The value of the control variables are tested using the test condition. The test condition is a relational statement such as i<10 which decides when the control has to exit the loop.If the condition is true the body of the loop is executed if it’s false the statement following the loop is executed.
  3. After the execution of the  body the loop the control is transferred to the for loop . Here now the control variable is incremented using an assignment statement such as i= i+1 and now the test condition is tested for the  new control variable if the condition is true then body of the loop is executed else the control comes out of the loop.

consider the following segment of program

for ( x = 0; x <=9 ; x=x+1 )
{
   System.Console.WriteLine(x);
}

This for loop is executed 10 times and the values from 0 to 9 are printed on the output screen.The three sections enclosed within the parentheses  must be separated by semicolon

The for loop allows negative increments.The above statement can be written as:

for ( x = 9; x >=0; x = x-1 )
{
   System.Console,WriteLine(x);
}

This loop is also executed 10 times but the value is displayed from  9 to 0

Since the condition is tested in beginning itself in some cases the body of the loop may not even be executed

for ( x =9; x < 9; x = x-1 )

{

-------------------

}

In this case the loop will not be executed because the condition is false at the very beginning itself.

program to find the sum of the squares of the number

using System;
public class Solution
{
  public static void Main()
{
     sum = 0;
     int n;
     for ( int n; n <=10; n++ )
      { 
        sum = sum + n*n;
      }
      Console.WriteLine(sum);
}

output

385

The foreach statement in C#

The foreach statement is similar to the for statement but it’s implemented differently.It enables us to iterate elements in arrays and collections such as list and hash table. the foreach loop syntax is s follows

for ( type variable in expression )
{
  body of the loop
}

the type and variable declare the iteration variable. During execution, the iteration variable represents an array element ( or collection of elements in case of collections )  for which an iteration is currently being performed and in is a keyword.

The expression must be an array or collection type

code to printing array values using foreach statement

using System;
class ForEach
{
   public static void Main()
    {
        int []a = { 11 22,33 };
        foreach ( int m in a)
         {
            Console.WriteLine (" " + m);
         }
     }
}

output

11 22 33

here the variable m is an iteration variable that will actually fetch the array values (i.e) from a .

An important point to remember is that we cannot increment the iteration variable. For instance in the below code snippet

int[] x = { 1,2,3};

foreach ( int i in x)
{
    i++;
    Console.WriteLine(i);
}



This will not work. If we need to change the values we have to use the for loop construct.

The advantage of foreach over for loop is that the foreach loop automatically detects boundaries of the collections being iterated.

Leave a Comment

Your email address will not be published. Required fields are marked *