Friday 20 November 2015

The for loop in asp.net


Introduction:

C# provides four different loops (for, while,do...while, and foreach) that allow us to execute a block of code repeatedly until a certain condition is met.The for loop is the first that we shall examine here.

Explanation:

C# for loops provide a mechanism for iterating through a loop where we test whether a particular condition holds before we perform another iteration. The syntax is:

for (initializer; condition; iterator)
statement(s)
 
where:

    The initializer is the expression evaluated before the first loop is executed (usually initializing a local variable as a loop counter).

    The condition is the expression that is checked before each new iteration of the loop (this must evaluate to true for another iteration to be performed).

    The iterator is an expression that will be evaluated after each iteration (usually incrementing the loop counter). The iterations end when the condition evaluates to false.

The for loop is a so-called pre-test loop, because the loop condition is evaluated before the loop statements are executed, and so the contents of the loop won’t be executed at all if the loop condition is false.The for loop is excellent for repeating a statement or a block of statements for a predetermined number of times. The following example is typical of the use of a for loop. The following code will write out all the integers from 0 to 99:

for (int i = 0; i < 100; i = i+1) // this is equivalent to

// For i = 0 To 99 in VB.

{

Console.WriteLine(i);

}

Here, we declare an int called i and initialize it to zero. This will be used as the loop counter. We then immediately test whether it is less than 100. Since this condition evaluates to true, we execute the code in the loop, displaying the value 0. We then increment the counter by one, and walk through the process again. Looping ends when i reaches 100.

Actually, the way we have written the above loop isn’t quite how you would normally write it. C# has a shorthand for adding 1 to a variable, so instead of i = i + 1, we can simply write i++:

for (int i = 0; i < 100; i++)

{

// etc.

 In C# you can do anything; for example, you can multiply the loop control variable by 2.It’s not unusual to nest for loops so that an inner loop executes once completely for each iteration of an outer loop. This scheme is typically employed to loop through every element in a rectangular multidimensional array. The outer most loop loops through every row, and the inner loop loops through every column in a particular row. The following code is available as the NumberTable sample, and displays

rows of numbers. It also uses another Console method, Console.Write(), which does the same as Console.WriteLine() but doesn’t send a carriage return to the output.

using System;

namespace Wrox.ProCSharp.Basics

{

class MainEntryPoint

{

static void Main(string[] args)

{

// This loop iterates through rows...

for (int i = 0; i < 100; i+=10)

{

// This loop iterates through columns...

for (int j = i; j < i + 10; j++)

{

Console.Write(“ “ + j);

}

Console.WriteLine();

}

}

}

}

Although j is an integer, it will be automatically converted to a string so that the concatenation can take place.



No comments:

Post a Comment