tailieunhanh - Praise for C# 2.0: Practical Guide for Programmers 2005 phần 6

Tham khảo tài liệu 'praise for c# : practical guide for programmers 2005 phần 6', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 112 Chapter 6 Statements and Exceptions Iteration Statements Iteration statements or loops allow a single statement or block to be executed repeatedly. The loop condition is a boolean expression that determines when to terminate the loop. C provides four kinds of loops while do-while for and foreach statements. while Statement I EBNF I The syntax of the while loop is WhileStmt while BooleanExpr EmbeddedStmt . EmbeddedStmt is executed zero or more times until the BooleanExpr evaluates to false. Example Countdown int sec 9 while sec 0 0 sec-- . Go Output Countdown 9876543210. Go do-while Statement I EBNF I The syntax of the do-while loop is DoStmt do EmbeddedStmt while BooleanExpr . EmbeddedStmt is executed one or more times until the BooleanExpr evaluates to false. Example giving the same output Countdown int sec 9 do 0 sec-- while sec 0 . Go Embedded Statements 113 for Statement The syntax of the for loop is I EBNF I ForStmt for ForInitializer ForCondition Forlterator EmbeddedStmt . and is equivalent to the following statements ForInitializer while ForCondition EmbeddedStmt ForIterator where I EBNF I ForInitializer LocalVarDecl StmtExprList . ForCondition BooleanExpr . ForIterator StmtExprList . Example giving the same output Countdown for int sec 9 sec 0 --sec 0 sec . Go An infinite for loop that prints dots for . is equivalent to the following while statement while true . foreach Statement The syntax of the foreach loop is I EBNF I ForeachStmt foreach Type Identifier in Expr EmbeddedStmt . The foreach statement enumerates the elements of a given collection and executes the embedded statement for each one. The Type and Identifier declare a read-only iteration variable to be used locally within the scope of the embedded statement. During the loop execution this iteration variable represents a