while keyword PDF Print E-mail
User Rating: / 0
PoorBest 
Monday, 09 March 2009

while

The while statement is one of C#'s control of flow statements. The statement executes one or more statements until a spcified expression evaluates to false. The general form is:

HTML clipboard while (expression) statement 

where:

expression - used to test the loop-termination expression. This expression can be implicitly converted to a bool tye or some other type that has a overloading of the true and false operators.

statement - a single or block of statements that are to be executed.

Key Points:

Since the expression that is tested (loop-termination expression) before even one iteration of the loop, a while loop can potentially execute zero or more times. You may terminate a while loop with a: break, goto, return or throw. A neat little trick is to use the continue statement which will take control to the next iteration of the loop without actually exiting the loop.
HTML clipboard

// keyword while
using System;
class whileExample
{
public static void Main()
{
int iCSharpSalary = 60000;    // C# developer's average salary        
while (iCSharpSalary < 80000 )
{
Console.WriteLine("I need more than ${0} a year"
, iCSharpSalary);
iCSharpSalary += 5000;  // bonus
}
// pause console window
Console.ReadLine();
}
}


 

Output:

I need more than $60000 a year

I need more than $65000 a year

I need more than $70000 a year

I need more than $75000 a yea

Last Updated ( Monday, 09 March 2009 )
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials