C# – Loops

Loops are used for iteration purpose, i.e., doing a task multiple times.

C# provides following types of loop to handle looping requirements.

Sr. No.Loop type & Description
1.For loop
It executes a sequence of statements multiple times and abbreviates the code that manage the loop variable.
2.While loop
It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body
3.do…while loop
It is similar to a while statement, except that it tests the condition at the end of the loop body.
4.Nested loops
you can use one or more loop inside any another while, for or do..while loop.

The for loop

The most common type of loop in C# is the for loop. The basic structure of a for loop is exactly the same as in java and C / C++ and is :

for(assignment; condition; increment/ decrement)

statement or block of statements

enclosed in { } brackets

Let’s see a for loop that will write the integers from 1 to 10 to the console:

for(int a=1; a<=10; a++)

{

Console.WriteLine(“In the loop, the value of a is {0}.”, a);

}

While loop

The while loop is similar to the do…while loop, except that it checks the condition before entering the first iteration (execution of code inside the body of the loop). The general form of a while loop is:

while (boolean condition)

statements or block of statements

Our program to print the integers 1 to 10 to the console using while will be:

int a=1;

while (a<=10)

{

Console.WriteLine(“In the loop, value of a is {0}.”,a);

a++;

}

The do…while loop

The general structure of a do…while loop is

do

statement or block of statements

while (boolean expression);

The statements under do will execute the first time and then the condition is checked. The loop will continue while the condition remains true.

The program for printing the integers 1 to 10 to the console using the do…while loop is:

int m=1;

do

{

Console.WriteLine (“In the loop, value of m is {0}.”,.m);

m++;

while (m<= 10);

Some important points here are:

  • The Statements in a do…while() loop always execute at least once.
  • There is a semicolon; after the while statement.

Nested Loop

You can use one or more loop inside any another while, for or do…while loop.

Loop Control Statements

Loop control statements change execution from its normal sequence.when execution leaves a scope, all automatic objects that were created in that scope are destroyed.

C# provides the following control statements . Click the following links to check their details.

Sr.No.Control statement & Description
1Break statement
Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
2Continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Infinite Loop

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.

Example :-

using system;

namespace loops {

class program {

static void Main (string[] args) {

for (; 😉 {

Console.WriteLine(“Hey ! I am Trapped”);

}

}

}

}

when the conditional expression is absent, it is assumed to be true. you may have an initialization and increment expression, but programmers more commonly use the for(;;) construct to signify an infinite loop.

Leave a Reply

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