C++ course(fifth section 2)
Fifth section(continued Loops)
The for loop executes as follows:
The initial statement executes
The loop condition is evaluated
if the loop condition evaluates to true
execute the for loop statement
execute the update statement (the third expression in the parentheses)
Repeat the previous step until the loop condition evaluates to false
The initial statement initializes a variable .
The initial statement in the for loop is the first to be executed and is executed only once.
cout<<x<<“\t”;
Fractional values:
The following is a legal for loop:
for(;;)
cout<<"Hello"<<endl;
do
statement
while(expression);
The statement executes first, and then the expression is evaluated
If the expression evaluates to true, the statement executes again
As long as the expression in a do...while statement is true, the statement executes
do
cout<<x++<<“\t”;
while (x<=5);
Fifth section(continued Loops)
The for loop executes as follows:
The initial statement executes
The loop condition is evaluated
if the loop condition evaluates to true
execute the for loop statement
execute the update statement (the third expression in the parentheses)
Repeat the previous step until the loop condition evaluates to false
The initial statement initializes a variable .
The initial statement in the for loop is the first to be executed and is executed only once.
If the loop condition is initially false, the loop body does not execute
The update expression, when executed, changes the value of the loop control variable which eventually sets the value of the loop condition to false
The for loop executes indefinitely if the loop condition is always true.
The update expression, when executed, changes the value of the loop control variable which eventually sets the value of the loop condition to false
The for loop executes indefinitely if the loop condition is always true.
Example:
for (int x=1; x <= 5; x++)cout<<x<<“\t”;
The output is:
1 2 3 4 5
can be used for loop control variables of the double type or any real data type
should be avoided because different computers can give different results for the variables
A semicolon at the end of the for statement (just before the body of the loop) is a semantic error, in this case, the action of the for loop is empty
If the loop condition is omitted it is assumed to be true.
In a for statement, all three statements—initial statement, loop condition, and update statement can be omittedshould be avoided because different computers can give different results for the variables
A semicolon at the end of the for statement (just before the body of the loop) is a semantic error, in this case, the action of the for loop is empty
If the loop condition is omitted it is assumed to be true.
The following is a legal for loop:
for(;;)
cout<<"Hello"<<endl;
The do…while Loop
The general form of a do...while statement is:do
statement
while(expression);
The statement executes first, and then the expression is evaluated
If the expression evaluates to true, the statement executes again
As long as the expression in a do...while statement is true, the statement executes
To avoid an infinite loop, the loop body must contain a statement that ultimately makes the expression false and assures that it exits
The statement can be a simple or a compound statement
If it is a compound statement, it must be enclosed between braces
Because the while and for loop has an entry condition, it may never activate, whereas, the do...while loop, which has an exit condition, always goes through at least once
The statement can be a simple or a compound statement
If it is a compound statement, it must be enclosed between braces
Because the while and for loop has an entry condition, it may never activate, whereas, the do...while loop, which has an exit condition, always goes through at least once
Example:
int x = 1;do
cout<<x++<<“\t”;
while (x<=5);
output is:
1 2 3 4 5
*Important note: the difference between (while loop statement) and (do while statement) that the do while must be executed at least one time , but the while statement can be used without being executed.
0 comments:
Post a Comment