![]() |
C - Entwicklung Programm - Flusskontrolle: Schleifen Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer |
|
| Program Flow Control | |
| Iteration Statements | Iteration statements cause statements (or compound statements) to be executed zero or more times, subject to some loop-termination criteria. |
| for Statement | Iteration statement |
| while Statement | The while statement executes a statement repeatedly until the termination condition (the expression) specified evaluates to zero. |
| do-while Statement | The do-while statement
lets you repeat a statement or compound statement until a specified expression
becomes false. Syntax |
| continue Statement | The continue statement forces immediate transfer of control to the loop-continuation statement of the smallest enclosing loop. |
| Iteration Statements Iteration statements cause statements (or compound statements) to be executed zero or more times, subject to some loop-termination criteria. When these statements are compound statements, they are executed in order, except when either the break statement or the continue statement is encountered. (For a description of these statements, see the break Statement and the continue Statement.) C++ provides three iteration statements while, do, and for. Each of these iterates until its termination expression evaluates to zero (false), or until loop termination is forced with a break statement. The table below summarizes these statements and their actions; each is discussed in detail in the sections that follow. C++ Iteration Statements
Syntax iteration-statement: while ( expression ) statement do statement while ( expression ) ; for ( for-init-statement expressionopt ; expressionopt ) statement for-init-statement: expression-statement declaration-statement The statement part of an iteration statement cannot be a declaration. However, it can be a compound statement containing a declaration. |
| for
Statement Iteration statement
The for-init-statement is commonly used to declare and initialize loop-index variables. The expression1 is often used to test for loop-termination criteria. The expression2 is commonly used to increment loop indices. The for statement executes the statement repeatedly until expression1 evaluates to zero. The for-init-statement, expression1, and expression2 fields are all optional. The following for loops:
|
||||||||||||||||||
| A convenient way to specify an infinite loop using the for statement
is: for( ; ; ) { } This is equivalent to: while( 1 ) { } |
||||||||||||||||||
| The initialization part of the for
loop can be a declaration statement or an expression statement,
including the null statement.
The initializations can include any sequence of expressions and declarations,
separated by commas. Any object declared inside a for-init-statement
has local scope, as if it had been declared immediately prior to the for
statement. Although the name of the object can be used in more than one
for loop in the same scope,
the declaration can appear only once. For example: #include <iostream.h> void main() |
Although the three fields of the for statement are normally used for
initialization, testing for termination, and incrementing,
they are not restricted to these uses. For example, the following code prints the numbers 1 to 100. The substatement is the null statement: #include <iostream.h> void main() { } |
|||||||||||||||||
| while
Statement Iteration statement The while statement executes a statement repeatedly until the termination condition (the expression) specified evaluates to zero. The test of the termination condition takes place before each execution of the loop; therefore, a while loop executes zero or more times, depending on the value of the termination expression. |
| The following example code uses a while
loop to trim trailing spaces from a string: char *trim( char *szSource ) { char *pszEOS; // Set pointer to end of string
to point to the character just while( pszEOS >= szSource
&& *pszEOS == ' ' ) |
| do-while
Statement Iteration statement The do-while statement lets you repeat a statement or compound statement until a specified expression becomes false. Syntax iteration-statement : do statement while ( expression ) ; The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once. The expression must have arithmetic or pointer type. Execution proceeds as follows: The do-while statement can also terminate when a break, goto, or return statement is executed within the statement body. |
| This is an example of the do-while
statement: do { } while ( x > 0 ); In this do-while statement, the two statements y = f( x ); and x--; are executed, regardless of the initial value of x. Then x > 0 is evaluated. If x is greater than 0, the statement body is executed again and x > 0 is reevaluated. The statement body is executed repeatedly as long as x remains greater than 0. Execution of the do-while statement terminates when x becomes 0 or negative. The body of the loop is executed at least once. |
| continue Statement
The continue statement forces immediate transfer of control to the loop-continuation statement of the smallest enclosing loop. (The "loop-continuation" is the statement that contains the controlling expression for the loop.) Therefore, the continue statement can appear only in the dependent statement of an iteration statement (although it may be the sole statement in that statement). In a for loop, execution of a continue statement causes evaluation of expression2 and then expression1. |
| The following example shows how the continue
statement can be used to bypass sections of code and skip to the next
iteration of a loop: #include <conio.h> // Get a character that is
a member of the zero-terminated |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |