Part 15 – C# Tutorial – for and foreach loops in c#

C# for loop & Foreach Loop:

Suggested Videos:

Part 12 –  Switch continued
Part 13 – While loop
Part 14 – Do while 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

[youtube https://www.youtube.com/watch?v=2zbFaDbADsQ]

Consider the following example of a simple for loop.

Example: for loop

for (int i = 0; i < 4; 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

 

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.

How to use C# foreach loop

The foreach loop in C# executes a block of code on each element in an array or a collection of items. When executing foreach loop it traversing items in a collection or an array . The foreach loop is useful for traversing each items in an array or a collection of items and displayed one by one.

  foreach(variable type in collection){

// code block

}

  variable type : The variable used for collect the item from Collection

collection : Collection of items

  string[] days = { "Sunday", "Monday", "TuesDay"};
  foreach (string day in days)
  {
	  MessageBox.Show("The day is : " + day);
  }

The above C# example first declared a string array ‘days’ and initialize the days in a week to that array. In the foreach loop declare a string ‘day’ and pull out the values from the array one by one and displayed it.

 

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

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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