Chapter 3 - Flow of Control Part II
Chapter 3 - Flow of Control Part II
Computer Programming
Chapter 3
Flow of Control Part II
(Loop Statements)
Chere L. (M.Tech)
Lecturer, SWEG, AASTU
1
Outline
Introduction to iterative flow control
Iterative flow controls (Looping statements)
for loop
while loop
do . . . while loop
Jumping statements
break, continue, goto
3
Chapter 3
1. Introduction to looping
The loop Statements allow a set of instructions to be performed
repeatedly until a certain condition is fulfilled.
Following is the general from of a loop statement in most of
the programming languages
4
Chapter 3
1. Introduction to looping (cont’d)
Part of loop
Initialization Expression(s)
initialize(s) the loop
variables in the beginning of the loop.
Test Expression
Decides whether the loop will be executed (if test expression is
true) or not (if test expression is false).
Update Expression(s)
update(s) the values of loop variables after every iteration of
the loop.
The Body-of-the-Loop
Contains statements to be executed repeatedly.
5
Chapter 3
1. Introduction to looping (cont’d)
Types of loop
Most programming language provides the following types of loop to
handle looping requirements
nested loops You can use one or more loop inside any another
while, for or do..while loop.
6
Chapter 3
1. Introduction to looping (cont’d)
Category of loops
1) Pretest and Posttest loops
Pretest loops (while loop & for loop) - the loop condition checked first,
if false, statements in the loop body never executed.
Posttest loop (do .. while loop) - the loop condition is checked/tested
after the loop body statements are executed.
Loop body always executed at least once
2) Count-controlled and Event-Controlled loops
Count-controlled (for loop) – also called fixed count loop
Repeat a statement or block a specified number of times
Used when exactly how many loops want to made
Event-controlled (while and do-while loop) – also called variable condition loop
Repeat a statement or block until a condition within the loop
body changes that cause the repetition to stop.
7
Chapter 3
1. Introduction to looping (cont’d)
Types of Event-Controlled Loops
Sentinel controlled
Keep processing data until a special value (sentinel value)
that is not a possible data value is entered to indicate
that processing should stop.
End-of-file controlled
Keep processing data or executing statement(s) as long as
there is more data in the file.
Flag controlled
Keep processing data until the value of a flag changes in
the loop body
8
Chapter 3
2. while loop
Syntax
while (repetition condition) {
statement (s);
}
next statement(s);
Repetition condition
It is the condition which controls the loop
Must evaluated to true/false (i.e. Boolean expression)
Can be formed by combining two or more relational expression with
logical operators
The statement is repeated as long as the loop repetition condition is true.
infinite loop - if the loop repetition condition is always true.
9
Chapter 3
2. while loop (cont’d)
Logic of a while loop
10
Chapter 3
2. while loop (cont’d)
EXAMPLE:
11
Chapter 3
3. for loop
Syntax
The initialization is The statement is executed
executed once before the until the condition becomes
loopbegins false
Condition
controls the loop and must evaluated to true/false
Can be formed by combining two or more relational expression with
logical operators
The statement is repeated as long as the loop repetition condition is true
12
Chapter 3
3. for loop (cont’d)
Logic of a for loop
initialization
condition
evaluated
true false
statement
increment
13
Chapter 3
3. for loop (cont’d)
EXAMPLE:
14
Chapter 3
3. for loop (cont’d)
The for loop Variations
a) Multiple initialization and update expressions
A for loop may contain multiple initialization and/or multiple
update expressions.
These multiple expressions must be separated by commas.
Example:
Reason being that when used alone, prefix operators are faster
executed than postfix
16
Chapter 3
3. for loop (cont’d)
Empty loop
If a loop does not contain any statement in its loop-body, it is said
to be an empty loop:
for(j=25; (j);--j) //(j) tests for non zero value of j.
If we put a semicolon after for’s parenthesis it repeats only for
counting the control variable.
And if we put a block of statements after such a loop, it is not a
part of for loop.
17
Chapter 3
4. do . . . while loop
Syntax
do {
statement (s);
} while (repetition condition)
next statement(s);
19
Chapter 3
4. do . . . while loop (cont’d)
EXAMPLE:
20
Chapter 3
5. Nested loop
Nested loops consist of an outer loop with one or more inner loops
21
Chapter 3
5. Nested loop (cont’d)
EXAMPLE:
22
Chapter 3
6. Jumping Statements
(a) The goto statement
It can transfer the program control anywhere in the program.
The target destination is marked by a label.
The target label and goto must appear in the same statement.
The syntax:
goto label;
………
………
label:
23
Chapter 3
6. Jumping Statements (cont’d)
(b) The break statement
Enables a program to skip over part of the code.
It terminates the smallest enclosing while, do-while and for
loop statements.
It skips the rest of the loop and jumps over to the
statement following the loop.
The figures on the next slide explains the working of a break
statement :
Aslo use along with switch as discussed under the selection
control section
Syntax:
break;
24
Chapter 3
6. Jumping Statements (cont’d)
How break statement works with loops
Note:
• The break statement can be used in similar fashion with do…while loop also
25
Chapter 3
6. Jumping Statements (cont’d)
Example of break statement
26
Chapter 3
6. Jumping Statements (cont’d)
(c) The continue statement
Enables a program to skip over part of the code.
works somewhat like the break statement.
For the for loop, continue causes the conditional test and
increment portions of the loop to execute.
For the while and do...while loops, program control
passes to the conditional tests.
Syntax:
continue;
27
Chapter 3
6. Jumping Statements (cont’d)
Example of continue statement
28
Chapter 3
6. Jumping Statements (cont’d)
Examples (break and continue
29
Chapter 3
7. Terminating Program
(a) The return statement
As you seen in the main() function it terminate the program
and return control back to the Operating System
Syntax: return returnValue;
(b) The exit() function
Used to terminate the program normally and return the
control to the Operating System.
Syntax: exit(int exitCode);
Avaliable in <cstdlib> library (ported from C's "stdlib.h")
(c) The abort() function
The same as exit() function but except it used to terminate the
program abnormally.
Syntax: abort(int exitCode);
30
Chapter 3
7. Terminating Program
Example if (errorCount > 10)
{
cout << "too many errors" << endl;
return 1;
}
(3) A looping process that checks the test condition at the end of loop?
(a) for while (b) do-while (c) while (d) none
(4) A looping process is best used when the number of iterations is
known
(a) for while (b) do-while (c) while (d) all are require
32
Chapter 3
Exercises (MCQ)
(5) A continue statement causes execution to skip to
(a) The return 0; statement
(b) The first statement after the loop
(c) The statement following the continue statement
(d) The next iteration of the loop
Chapter 5:
Gary J. Bronson; C++ For Engineers and Scientists [3rd
edition], Course Technology, Cengage Learning, 2010
35
Chapter 3