Sie sind auf Seite 1von 15

Iterative Control Structure

Module 5
Loop
A control structure that repeats a group of
steps in a program. It defines a block of
code that is repeatedly executed.
Depending on the kind of loop that you
are using, the block of code could be
executed a set number of times or until a
certain condition is met.
Loop Components
1. Initialization of variables – setting an initial
value of zero before the loop statement is
reached.
2. Condition/testing – (that would evaluate to
either true or false) the condition to check
is usually made on the current value of the
variable initialized in (1) for it controls the
loop repetition.
3. Body – statements that are repeated in the
loop.
4. Updating – a statement inside the body of
the loop that updates the value of the
variable that is initialized during each
repetition.
3 Types of Loop
1. do-while statement
2. while statement
3. for statement
do-while
 This loop executes a block of codes as
long as the specified condition is true.
 It is a post checked loop because it
checks the controlling condition after the
code is executed.
do-while
 Syntax:
do
{
statement;
} while (loop repetition condition);
 Example:
c=1;
do
{
printf(“Hello World\n”);
c++;
} while (c<=5);
Initialization c=1

do do

“Hello world!”
Process inside the loop

c++
T
While
Condition?
T
While
F
c<=5

F
while
 Like do-while, this will execute a block of
codes while the given condition is true.
 It is a pre-checked loop because it checks
the controlling condition first before the
code is executed.
while
 Syntax:
while (loop repetition condition)
{
statement;
}
 Example:
c=1;
while (c<=5)
{
printf(“Hello World\n”);
c++;
}
Initialization c=1

F F
While While
Condition? c<=5

T T

“Hello world!”
Process inside the loop

c++
for
 It is usually used to execute code block
for a specified number
 It is important to note that all
expressions in the for loop are optional. It
means that you can omit any expression
or all of them.
 When you omit all expressions, the C for
loop behaves slightly similar to the while
loop and do while loop with break
statement.
for
 Syntax:
for (initialization expression; loop repetition
condition; update expression)
{
statement;
}
 Example
for (c=1; c<=5; c++)
printf(“Hello World\n”);
Counter
 It is set up in a program to keep track the
number of times the program segment is
repeated. The program can then be
terminated after the completion of a
predetermined number of passes.
Trailer Record
 A special value used to terminate a loop
instead of a counting process. The value is
chosen to be a data value which is outside
the range of the data values to be
processed.
Break and Continue
Repetition loops can also be altered by break or
continue statements.
Break - Exits from the lowest-level loop in which
it occurs.
- used for early exit from the loop, or for
exiting a forever loop
Continue – causes immediate loop iteration,
skipping the rest of the loop statements
- jumps to the loop test when used with while
or do
- jumps to loop increment, then test when used
with for
- does not exit the loop (unless next loop is
false)