SMU Assignment C#: Sample Question

We are providing a Sample Question for Assignments Based on C# Programming (Code BT8903) which is an elective subject for B. Sc. (IT) and DIT.

Q. Explain the different types of iteration control systems used in C#, using appropriate syntax with an example for each.

Ans. Iteration Statements (C# Reference)

You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a jump statement is encountered. The following keywords are used in iteration statements:

• do

The do statement executes a statement or a block of statements enclosed in {} repeatedly until a specified expression evaluates to false. In the following example the do loop statements execute as long as the variable y is less than 5.

Example

// statements_do.cs

using System;

public class TestDoWhile

{

public static void Main ()

{

int x = 0;

do

{

Console.WriteLine(x);

x++;

}

while (x <>

}

}

Unlike the while statement, the body loop of the do statement is executed at least once regardless of the value of the expression

• for

The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The for loop is handy for iterating over arrays and for sequential processing. In the following example, the value of int i is written to the console and i is incremented each time through the loop by 1.

Example

// statements_for.cs

// for loop

using System;

class ForLoopTest

{

static void Main()

{

for (int i = 1; i <= 5; i++)

{

Console.WriteLine(i);

}

}

}

The for statement executes the enclosed statement or statements repeatedly as follows:

• First, the initial value of the variable i is evaluated.


• Then, while the value of i is less than 5, the condition evaluates to true, the Console.WriteLine statement is executed and i is reevaluated.

• When i is greater than 5, the condition becomes false and control is transferred outside the loop.

Because the test of conditional expression takes place before the execution of the loop, therefore, a for statement executes zero or more times.

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

for (;;)

{

// ...

}

• foreach , IN

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.

Example

In this example, foreach is used to display the contents of an array of integers.

// cs_foreach.cs

class ForEachTest

{

static void Main(string[] args)

{

int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };

foreach (int i in fibarray)

{

System.Console.WriteLine(i);

}

}

}

• while

The while statement executes a statement or a block of statements until a specified expression evaluates to false.

Example

// statements_while.cs

using System;

class WhileTest

{

static void Main()

{

int n = 1;

while (n <>

{

Console.WriteLine("Current value of n is {0}", n);

n++;

}

}

}