Professional Documents
Culture Documents
SECT-M1082
Chapter 3
Flowcharts and Control Statements
SECT-M1082
Looping Statements
• Loop is a repetition control statement.
3
Looping Statements
4
Designing Loops
• When designing a loop we need to design three things:
1. The body of the loop
2. The initializing statement
3. The condition for ending the loop
• Debugging loops
5
While Loop
• The while keyword is followed by the condition in parentheses.
Syntax
• Loop body can be a single statement, a null statement, or a
block.
while(condition){
• If the condition in the parenthesis is false the loop exits and
jump to the subsequent code.
• If the test condition never evaluate to false, the loop will never
//loop body
terminate.
6
Flowchart representation of a while loop
7
While Loop example representing eating
food.
int gursha = 10;
while(gursha > 0){
cout<<“Eating”<<“\n”;
gursha--;
}
cout<<“Done eating”;
8
Do-while Loop
• The loop body is executed first, then the Boolean Syntax
expression is evaluated.
• If the evaluation is true, the loop body is executed again; do{
• if it is false, the do-while loop terminates.
9
Flowchart representation of a do-while loop
10
Do-while loop example
int answer=0;
do {
cout<<”What would you like to do?\n”
<<”1. Display \”Hello World\”\n”
<<”2. Add two numbers\n”
<<”Enter your choice (0 to exit):> “;
cin>>answer;
cout<<”OK then!\n”;
/*Use switch statement to handle the choice*/
} while (answer!=0);
11
For Loop
• A for loop generally uses a variable to control how many times the loop
body is executed and when the loop terminates.
12
For Loop
Syntax:
//loop body
}
3. The Boolean-expression or test condition tests whether the control variable has reached
a termination value.
13
For Loop
Syntax:
//loop body
}
3. The Boolean-expression or test condition tests whether the control variable has reached
a termination value.
14
Flowchart representation of a for loop
15
break statement
• Used to leave a loop even if the condition for its end is not fulfilled.
16
Flowchart representation of break statement
17
Example: Terminate loop if x is even
int x;
do{
cin>>x;
if(x%2==0)
break;
}while(x>0);
18
continue statement
• Continue statement forces the current iteration of the loop to end.
• It causes the program to skip the rest of the loop in the current
iteration.
19
Flowchart representation of continue
statement
20
Example: Jump to start if x is odd
int x;
do{
cin>>x;
if(x%2!=0)
continue;
cout<<x/2;
}while(x>0);
21
Nested Loops
• Nested loops consist of an outer loop and one or more inner loops.
• Each time the outer loop is repeated, the inner loops are reentered,
and all the required iterations are performed.
Example:
for( int j=0; j < 5; j++){
for( int i=0; i < 5; i++){
cout<<“j = ”<<j<<“ and i = “<<i;
}
22 }
Which loop should I use?
• In general, a for loop may be used if the number of repetitions is known
• A do-while loop can be used to replace a while loop if the loop body has to
be executed before testing the continuation condition.
• But it is recommended that you use the one that is most intuitive and
comfortable for you.
23
Thank you!
24