Friday 20 November 2015

The while 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.I will explain about the while loop here.

Explanation:

It is like the for loop, while is a pre-test loop. The syntax is similar, but while loops take only one expression:

while(condition)
statement(s);


Unlike the for loop, the while loop is most often used to repeat a statement or a block of statements for a number of times that is not known before the loop begins. Usually, a statement inside the while loop’s body will set a Boolean flag to false on a certain iteration, triggering the end of the loop, as in the following example:

bool condition = false;
while (!condition)
{
// This loop spins until the condition is true
DoSomeWork();
condition = CheckCondition(); // assume CheckCondition() returns a bool
}

All of C#’s looping mechanisms, including the while loop, can forego the curly braces that follow them if they intend to repeat just a single statement and not a block of statements. Again, many programmers consider it good practice to use braces all of the time.

No comments:

Post a Comment