The do-while repetition structure is similar to the while structure. In the while structure, the program tests the loop-continuation condition at the beginning of the loop, before performing the body of the loop. The do-while structure tests the loop-continuation condition after performing the body of the loop; therefore, the loop body always executes at least once. When a do-while structure terminates, execution continues with the statement after the while clause. Note that it is necessary to use braces in the do-while structure if there is only one statement in the body. However, most programmers include the braces, to avoid confusion between the while and do-while structure. For example,
while ( condition )
normally is the first line of a while structure. A do-while structure with no braces around a single-statement body appears as
do
statement
while ( condition );
which can be confusing. Reader may misinterpret the last line- while (condition ); - as a while structure containing an empty statement (the semicolon by itself). Thus, to avoid confusion, the do-while structure with one statement often is written as follows:
do{
statement
} while ( condition );
Example:
int i = 1;
do{
cout << (i++) << endl;
}while (i <= 10);
will display the following pattern:
1
2
3
4
5
6
7
8
9
10
Do it yourself:
Problem #1:
Write a program to display the following pattern using do-while loop structure:
0 3 6 9 12 15 18 21
Answer:
int i = 0;
do{
cout << i << " ";
i+=3;
}while ( i <= 21);
Problem #2:
Translate the following code snippet using a do-while structure:
int i=1;
while ( i <= 5){
cout << i << " ";
i++;
}
Answer:
int i=1;
do{
cout << i << " ";
i++;
}while (i <=5);
*************************************************
Lab Activity No. 2
Midterm
Problem:
Write a program to display a multiplication table using do-while loop.
Expected output:
12345678910
2468101214161820
36912151721242730
481216202428323640
Note: I will check your program on Tuesday next week.
Check some Viral Stuff and Trending News.
Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts
Thursday, August 28, 2014
Wednesday, August 27, 2014
The for Repetition Structure
The for repetition structure handles all of the details of counter-controlled repetition. The first line is sometimes called the for structure header. Notice that the for structure "does it all": It specifies each of the items needed for counter-controlled repetition with a control variable. If there is more than one statement in the body of the for structures, braces ( { and } ) are required to define the body of the loop.
for ( int counter=1; counter <= 10; counter++ )
where:
The general format of the for structure is
for (expression1; expression2; expression3 )
statement
where expression1 names the loop's control variable and provides its initial value, expression2 is the loop-continuation condition (containing the control variable's final value) and expression3 modifies the value of the control variable, so that the loop-continuation condition eventually becomes false. In most cases, the for structure can be represented with an equivalent while structure, with expression1, expression2 and expression3 placed as follows:
expression1;
while ( expression2 ) {
statement;
expression3;
}
for ( int counter=1; counter <= 10; counter++ )
where:
- for is a keyword
- counter is the control variable
- 1 is the initial value of control variable
- 10 is the final value of control variable
- counter <= 10 is the loop-continuation condition
- counter++ is the increment of control variable
The general format of the for structure is
for (expression1; expression2; expression3 )
statement
where expression1 names the loop's control variable and provides its initial value, expression2 is the loop-continuation condition (containing the control variable's final value) and expression3 modifies the value of the control variable, so that the loop-continuation condition eventually becomes false. In most cases, the for structure can be represented with an equivalent while structure, with expression1, expression2 and expression3 placed as follows:
expression1;
while ( expression2 ) {
statement;
expression3;
}
Subscribe to:
Comments (Atom)