Iterative Statements


C# has the full repertoire of C-style iterative statements plus the foreach statement. Iterative statements repeat statement_blocks until a condition has been satisfied.

The for statement is designed for structured iteration when a proper iterator is available. The for statement contains three clauses. First is the initializer_clause in which the loop iterators are declared. The scope of an iterator is the for statement and statement_block. Second is the boolean_expression that must evaluate to a Boolean type. The expression normally compares the iterator to an end value. Third, the iterator_expression is executed at each iteration and is usually responsible for updating iterator values. Each clause is optional and delimited with a semicolon. The statement_block is repeated until the boolean_expression is false.

The statement_block is repeated zero or more times. If the boolean_expression is initially false, the statement_block is executed zero times.

This is a for statement:

 for(initializer_clause;boolean_expression;iterator_expression) { statement_block } 

This is a rather mundane for loop:

 static void Main() {     for(int iCounter=0;iCounter<10;++iCounter) {         Console.Write(iCounter);     } } 

Both the initializer_clause and iterator_expression can contain multiple statements delimited by commas. This allows additional flexibility and complexity than shown previously. Here is an example:

 static void Main() {     for(int iBottom=1, iTop=10; iBottom < iTop; ++iBottom, --iTop) {         Console.WriteLine("{0}x{1} {2}", iBottom, iTop, iBottom*iTop);     } } 

The while statement, which is an iterative statement, is more free-form than the for statement. The statement_block of the while statement is executed while the boolean_expression is true. The statement_block is executed zero or more times. If the boolean_expression is initially false, the statement_block is executed zero times.

Typically, the statement_block is responsible for altering an iterator or something else where-upon the boolean_expression evaluates to false, which ends the loop. Care should be taken to avoid unattended infinite loops.

This is an example of a while statement:

 while(boolean_expression) { statement_block } 

This is source code for selecting a choice rewritten with a while statement:

 static void Main() {     string resp;     Console.WriteLine("Enter command ('x' to end):");     while((resp=(Console.ReadLine()).ToLower()) != "x") {         switch(resp) {             case "a":                 Console.WriteLine("Doing Task A");                 break;             case "b":                 Console.WriteLine("Doing Task B");                 break;             default:                 Console.WriteLine("Bad choice");                 break;             }     } } 

A do statement is a loop with the boolean_expression after the statement_block. This is the reverse of the while statement. The impact is that the statement_block of the do statement is repeated one or more times. The niche for the do statement is when the statement_block needs to execute once before the boolean_expression. The iteration of the statement_block continues while the boolean_expression is true.

This is a do statement:

 do { statement_block } while(conditional_statement) 

Here is sample code of the do statement:

 static void Main() {     string resp;     do {         Console.WriteLine("Menu\n\n1 - Task A");         Console.WriteLine("2 - Task B");         Console.WriteLine("E - E(xit)");         resp=(Console.ReadLine()).ToLower();     }     while(resp!="e"); } 

The foreach statement is a convenient mechanism for automatically iterating elements of a collection. The alternative is manually iterating a collection with the requested enumerator object. The foreach statement is unquestionably simpler. Collections implement the IEnumerable interface.

This is the syntax of the foreach statement:

 foreach(type identifier in collection) { statement_block } 

The foreach statement iterates the elements of the collection. As each element is enumerated, the identifier is assigned the new element, and the statement_block is repeated. The scope of the identifier is the foreach statement and statement_block. When the collection is fully iterated, the iteration stops, and the statement_block no longer repeats.

The identifier is a class, struct, or interface. The identifier type should be related to the type of elements extracted from the collection. In addition, the identifier is read-only. Even using the identifier in a context that implies change, such as passing the identifier as a ref function parameter, is an error.

The foreach statement confirms that the enumerator or elements of the collection are disposable objects. If so, the foreach statement calls the Dispose method on the applicable objects. Disposable objects implement the IDisposable interface.

This code iterates an array of numbers:

 static void Main() {     string [] numbers={ "uno", "dos", "tres",         "quatro", "cinco"};     foreach(string number in numbers) {     Console.WriteLine(number);     } } 

The break statement prematurely exits the containing switch or iterative statement and transfers control to the statement after the statement_block. For switch statements, the break prevents fall through between switch_labels. For an iterative statement, the break stops the iteration unconditionally and exits the loop. Control is transferred to the statement following the iterative loop. If the switch or iterative statement_block is nested, only the immediately statement_block is exited.

The continue statement transfers control to the end of a statement_block where execution continues. The boolean_expression of the iterative statement then determines whether the iteration continues, and the statement_block is repeated. This is sample code of the break statement:

 static void Main() {     string resp;     while(true) {         Console.WriteLine("Menu\n\n1 - Task A");         Console.WriteLine("2 - Task B");         Console.WriteLine("E - E(xit)");         resp=(Console.ReadLine()).ToLower();         if(resp=="e") {              break;         }     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net