PS-Trainer C - Entwicklung
Programm - Flusskontrolle: Schleifen
Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer
PS-Trainer 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
Statement Evaluated At Initialization Increment
while Top of loop No No
do Bottom of loop No No
for Top of loop Yes Yes

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
Syntax Name When Executed Contents
for-init-statement Before any other element of the for statement or the substatement Often used to initialize loop indices. It can contain expressions or declarations.
expression1 Before execution of a given iteration of the loop, including the first iteration. An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type.
expression2 At the end of each iteration of the loop; expression1 is tested after expression2 is evaluated. Normally used to increment loop indices.

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:
for( for-init-statement; expression1; expression2 )
{
// Statements
}

for (i=0;i<10;i++)
{
printf("%5d",i);
}

are equivalent to the following while loops:
for-init-statement;
while( expression1 )
{
// Statements
expression2;
}
i=0;
while (i<10)
{
printf("%5d",i);
i++;
}
A convenient way to specify an infinite loop using the for statement is:
for( ; ; )
{
// Statements to be executed.
}

This is equivalent to:
while( 1 )
{
// Statements to be executed.
}
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()
{
for( int i = 0; i < 100; ++i )
cout << i << "\n";

// The loop index, i, cannot be declared in the
// for-init-statement here because it is still in scope.
for( i = 100; i >= 0; --i )
cout << i << "\n";
}

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()
{
for( int i = 0; i < 100; cout << ++i << endl )
;
}

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
// before the 0 at the end of the string.
pszEOS = szSource + strlen( szSource ) - 1;

while( pszEOS >= szSource && *pszEOS == ' ' )
*pszEOS-- = '\0';

return szSource;
}

The termination condition is evaluated at the top of the loop. If there are no trailing spaces, the loop of this example never executes.
The expression must be of an integral type, a pointer type, or a class type with an unambiguous conversion to an integral or pointer type.


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 statement body is executed.
Next, expression is evaluated. If expression is false (zero), the do-while statement terminates and control passes to the next statement in the program.
If expression is true (nonzero), the process is repeated, beginning with step 1.

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
{
y = f( x );
x--;
} 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
// string, szLegalString. Return the index of the character
// entered.
int GetLegalChar( char *szLegalString )
{
char *pch;

do
{
char ch = _getch();

// Use strchr library function to determine if the
// character read is in the string. If not, use the
// continue statement to bypass the rest of the
// statements in the loop.
if( (pch = strchr( szLegalString, ch )) == NULL )
continue;

// A character that was in the string szLegalString
// was entered. Return its index.
return (pch - szLegalString);

// The continue statement transfers control to here.
} while( 1 );

return 0;
}


Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1