|
A loop is defined as the block of code which repeats itself for a given true condition. The following loops are available in c# programming constructs.
- While Loop
- Do… While Loop
- For Loop
- ForEach Loop
While Loop :
A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a Boolean value of true.
Syntax :
while (<Boolean expression >) {
//Execute block of code
}
- The Boolean expression is evaluated before any code in the following block has executed.
- When the Boolean expression evaluates to true, the statement will execute. Once the statement have executed, control returns to the beginning of the while loop to check the boolean expression again.
- When the boolean expression evaluates to false , the while loop statements are skipped and execution begins after the closing brace of that block of code. Variable must be declared and initialized before entering the while loop, and must Increment or decrement the variable in the loop. So that the loop should be definite.
Do… While Loop :
A “Do… While” loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the “Do… While” loop is guaranteed to execute at least one time. This means that the “Do… While” loop will display whatever there in the statement or block at least once.
Syntax:
do {
// execute this statements or blocks at least once.
} while (<Boolean expression>);
For Loop :
The for loop is much like the while loop, but the initialization, evaluation and increment or decrement can be done in one go. The for loop executes until the condition or evaluated expression returns true.
Syntax:
for ( <initializer list>; <Boolean expression>; <iterator list>) {
// execute the statement if the Boolean expression return true
}
- Initializer List:- This can be comma separated list of expressions. The expressions are evaluated only once during the lifetime of for loop.
- Boolean Expression:- The for loop execution depend on this expression, it will executes until or unless this expression returns true.
- Iterator List:- The iterator list contains comma separated counter and this is basically used Either to increment or decrement the variable that is initialized in the Beginning.
ForEach Loop:
A ForEach loop is used to iterate through the items in the list. The ForEach loop is used to operate over an arrays or collections such as ArrayList, which is under System.Collections namespace.
Syntax:
foreach (<datatype><itemname>in<list>) {
//statements
}
Note:-The foreach loop is used to iterate over collection. The datatype should be same as the elements contained in the list.
Example: Demonstration of while, do…while, for, foreach loop

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace cSHARPEXAMPLES
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void btnWhile_Click( object sender, EventArgs e)
{
int _tempValue = 0;
lblWhile.Text = "\n" ;
while (_tempValue < 10)
{
lblWhile.Text = lblWhile.Text + "Loop Value: " + _tempValue + "\n" ;
_tempValue++;
}
}
private void doWhile_Click( object sender, EventArgs e)
{
int _tempValue = 0;
do {
MessageBox.Show( "This Executes at Least once even though the condition is false" );
} while (_tempValue > 0);
}
private void btnFor_Click( object sender, EventArgs e)
{
lblFor.Text = "\n" ;
for ( int i = 0; i < 10; i++)
{
if (i%2 == 0)
lblFor.Text = lblFor.Text + "Loop Value: " + i + "\n" ;
}
}
private void btnForEach_Click( object sender, EventArgs e)
{
ArrayList arrList = new ArrayList ();
arrList.Add( "Vikrant" );
arrList.Add( "Atul" );
arrList.Add( "Mandeep" );
arrList.Add( "Gautam" );
arrList.Add( "Pardeep" );
lblforEach.Text = "\n" ;
foreach ( String arr in arrList)
{
lblforEach.Text = lblforEach.Text + arr + "\n" ;
}
}
}
}
OutPut:
Clicking on all the Buttons:

|