For
Loop
for loop is another powerful loop construct in C#. It is
powerful and easy to use. for loop includes all three characteristics as
initialization, termination and increment/decrement in a single line.
for (initialization; termination; increment/decrement)
;
There are many situation when you need to execute a block of
statements several number of times in your applications. The for loop in C# is useful for iterating over arrays
and for sequential processing. This kind of for loop is useful for iterating
over arrays and for other applications in which you know in advance how many
times you want the loop to iterate. That is the statements within the code
block of a for loop will execute a series of statements as long as a specific
condition remains true.
Every for loops defines initializer, condition, and iterator
sections.
Syntax:
for(initialization;
condition; step) code statement
Initialization : Initialize the value of variable.
condition : Evaluate the condition
step : Step taken for each execution of loop body
The for loop initialize the value before the first step. Then checking the condition against the current value of variable and execute the loop statement and then perform the step taken for each execution of loop body.
for (i = 1; i <= 10; i++)
{
result = num * i;
Console.WriteLine("{0} x {1} = {2}", num, i, result);
}
The initializer declares and initializes a local loop variable, i, that maintains a 10 of the iterations of the loop. The loop will execute 10 times because we set the condition i is less than or equal to count.
for (int i = 1; i < = 10; i++)
initialization : int i = 1
Initialize the variable i as 1, that is when the loop starts the value of i is set as 1
condition : i < = 10
Set the condition i < =10, that is the loop will execute up to
when the value of i < = 10 (Ten times)
step : i++
Set the step for each execution of loop block as i++ ( i = i +1)
The output of the code as follows :
Infinite Loop
All of the expressions of the for loop statements are optional. A loop becomes infinite loop if a condition never becomes false. You can make an endless loop by leaving the conditional expression empty. The following statement is used to write an infinite loop.
for (; ; ) { // statements }
Here the loop will execute infinite times because there is no initialization , condition and steps.
Break and Continue
We can control for loop iteration with the break and continue statements. break terminates iteration and continue skips to the next iteration cycle. The following program shows a simple example to illustrate break and continue statement.
using System;
class Program
{
static void Main(string[] args)
{
int num, i, result;
Console.Write("Enter a number\t");
num = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= 10; i++)
{
result = num * i;
Console.WriteLine("{0} x {1} = {2}", num, i, result);
}
Console.ReadLine();
}
}
Output:
Nested Loop
C# allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
Syntax:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
A sample prime number program for nested for loop
using System;
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 2; i < 50; i++)
{
for (j = 2; j <= (i / j); j++)
if ((i % j) == 0) break;
if (j > (i / j))
Console.WriteLine("{0} is prime", i);
}
Console.ReadLine();
}
}
Output will be:
No comments :
Post a Comment