Saturday, May 16, 2020

C# Tutorial: 05 - Looping Structures

Tutorial 05 - Looping Structures in C#

There are four types of Iterative/looping structures in C#:
  1. for
  2. while
  3. do-while
  4. foreach
1. for loop
    for loop is use for executing a statement or set of statements for a specific number of times.
    General syntax is given below:
    for ( initialization; condition; increment/decrement ) 
    {    
    statement(s);
    }
  • initialization part is executed first and it is executed only one time. The purpose of this is to initialize and declare the variable to be use in controlling the loop. If you want to skip this part, you can simply put a semicolon here.
  • condition part is executed in second step. Condition is evaluated and if it is true control goes into body of loop and starts executing statements and if its false control goes out of loop. The purpose of this is to initialize and declare the variable to be use in controlling the loop. If you want to skip this part, you can simply put a semicolon here.
  • increment/decrement part is executed when body of loop is executed. Based of statement either it increments or decrements the value of loop variable
    Consider the following example for a for loop:

2. While loop
    while loop executes statements or set of statements until given condition remains true.
    The general syntax for a while loop is:
  while(condition) 
{
     statement(s)
       increment/decrement
  }
  • First condition part is evaluated, if it evaluates to either true or false value based on expression and body of loop is executed on this evaluation. Initially, if condition is false, body of loop won't be executed. 
  • Increment/decrement part is usually at the end of body of loop in braces. Note that you need to put loop variable properly or it will affect the outcome of the loop. For example, output of a while loop will be different if we put increment/decrement part before body of loop.
Consider the following example for while loop:
3. do while loop
    In do while loop, body of loop is executed first unlike for and while loop. After execution of        body, condition is evaluated, based on this evaluation, body of loop will be executed. Also             remember, there is semicolon used at end of loop. General syntax for while loop is as follow:
  do 
  {
    statement(s);
       increment/decrement
}
 while(condition);
  • First body of loop is executed and the condition part comes, if it evaluates to either true or false value based on expression and body of loop will be executed again on this evaluation. Initially, even if condition is false, body of loop will be executed at-least once.
  • Increment/decrement part is usually at the end of body of loop in braces. 
Consider the following example for a do-while loop:

4. foreach loop
    In C#, a for each loop is available to handle the arrays and collections.
    The general syntax for a foreach loop is given below:
    foreach (item in iterable-item)
    {
    // body of foreach loop
    }
  • iterable-item can be a collection i.e. stack, queue etc or array
  • in keyword is used for iterating over the iterable-item in each iteration and store it in item
Consider following example for foreach loop.

No comments:

Post a Comment