You are on page 1of 24

Computer Programming

SECT-M1082
Chapter 3
Flowcharts and Control Statements
SECT-M1082
Looping Statements
• Loop is a repetition control statement.

• Each loop contains a loop continuation condition, a Boolean expression that


controls the execution of the body.

• A one-time execution of a loop body is referred to as an iteration of the loop.

• After each iteration, the boolean expression is reevaluated.


• If the condition is true, the execution of the loop body is repeated.

• If the condition is false, the loop terminates.

3
Looping Statements

• Make sure that the loop-continuation-condition eventually becomes


false so that the program will terminate.

• A loop that runs forever is called an infinite loop.


• There are three kinds of loops in programming:
1. While loop
2. Do-while loop
3. For loop

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

• Most common loop mistakes are


1. Off – by-one loop
2. Infinite loop

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.

• No semicolon after the Boolean expression. }

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.

• Boolean expression is tested at the end of the loop. //loop body


• There is a semicolon after the Boolean expression.
• Identical to a while loop except that it performs at }while(condition);
least one iteration even if the condition is false.

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.

• This variable is referred to as a control variable.

• For loop checks condition before iteration.

12
For Loop
Syntax:

for (initialization expression; test condition; update expression) {

//loop body
}

1. The initialization expression often initializes a control variable,

2. The update expression usually increments or decrements the control variable.

3. The Boolean-expression or test condition tests whether the control variable has reached
a termination value.

13
For Loop
Syntax:

for (initialization expression; test condition; update expression) {

//loop body
}

1. The initialization expression often initializes a control variable,

2. The update expression usually increments or decrements the control variable.

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.

• It immediately ends the innermost loop that contains it.

• It is generally used with an if or switch statement to check the condition


before terminating the loop.

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.

• It causes it to jump to the start of the following 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 while loop may be used if the number of repetitions is not 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

You might also like