Loop Structures in C
Loop Structures in C
Loop Structure
Objectives:
• Loop structure
• While, for, and do/while loop structures
• Nested loops
• Exercises
CPU
Example of the indefinite repetition loop
CPU
Example of the indefinite repetition loop
What are the differences with the first version?
CPU
Example of a nested loop
Program Memory
Print “Enter an integer”
Read value into num Working
Repeat while num not equal to -1 Memory
Assign 1 to factorial
Assign num to ctr num
Repeat while ctr larger or equal to 1
Assign factorial*ctr to factorial factorial
Decrement ctr
Print “The factorial of “, num, “is ”,
factorial
Print “Enter an integer”
Read value into num
Print “You have chosen to quit”
CPU
Loop Structures in C
True
Expression command;
?
False
Components of a Loop
• Initial value of counter
• Condition (defines the final value)
• Body of statements
• Increment/decrement counter
int counter = 1;
While(counter<5)
{
printf(“%d\n”, counter);
counter++;
}
A definite while loop is controlled by a counter; the following are required if a
definite loop is to execute correctly:
– initialization of the counter before the loop,
– a command in the loop that modifies the value of the counter,
– a logical condition that tests the value of the counter against its expected
final value such that the logical expression evaluates to false and
the loop is exited.
An indefinite while loop is controlled by a sentinel; the following are required
if an indefinite loop is to execute correctly:
– initialization of the sentinel before the loop,
– a command in the loop that modifies the sentinel,
– a logical condition on the sentinel that eventually causes the logical
expression to evaluate to false in order to exit the loop.
It is very good practice to indent the instructions in the instruction block of a
while loop in order to help visualize the structure of the program. Using {} to
include a single command in a while loop is possible and can also help clarify
the code.
Example of a definite repetition loop using the while structure:
int ctr;
ctr = 1;
while (ctr <= 10)
{
printf(“Iteration number: %d\n”, ctr);
ctr++;
}
do
command;
True
while( )
False
Operation of a do/while structure in a program:
do Program execution flows into the
do/while structure, performing
{ at least once the commands
command 1; between the {}. The logical
command 2; expression is then tested and if
true, execution returns to the do
} statement and the commands
True while (logical expression); between the {} are repeated.
When logical expression
False becomes false, program execution
continues after the while ();
It is very good practice to indent the instructions in the instruction block of the
loop in order to help visualize the structure of the program. Using {} to include a
single command in a do/while loop is possible and can also help clarify the
code.
Example of a definite repetition loop using the do/while structure:
int ctr;
ctr = 1;
do
{
printf(“Iteration %d\n”, ctr);
ctr++;
}
while (ctr <= 10);
Counter
expression 1;
Initialization
True
expr. 2; command; expression 3;
Logical
expression that Counter
tests the counter Modification
False
against its final
value
Operation of a for structure in a program:
for (initial value; condition; increment/decrement)
{
command 1;
command 2;
}
A Program execution flows first into initial value which is evaluated once to
initialize the loop counter.
B condition is then evaluated; recall that it is a logical expression containing the
loop counter;
D if condition evaluates to false, then the commands between the {} are skipped
and the program continues to execute after the loop;
E if condition evaluates to true, then the commands between the {} are executed
and increment/decrement is performed to modify the value of the counter;
F back to step B.
The for structure is a pre-tested loop.
It is possible that the commands in the loop are never executed.
In theory, real variables can be tested for equality against a given value but in practice this
is not robust due to the imprecise nature of arithmetic with real variables on computers.
It is best to modify the logical expression for the loop termination condition such that
testing for equality is not required.
– Looping from 1.0 to 2.5 inclusively might be coded as:
float start=1.0, end=2.5, inc=0.5, ctr;
for (ctr=start; ctr <= end; ctr=ctr+inc)
printf(“Value of ctr: %f\n”, ctr);
– The above loop is best implemented as:
Robust! for (ctr=start; ctr < (end+0.5*inc); ctr=ctr+inc)
printf(“Value of ctr: %f\n”, ctr);
Nested Loops
Any C command can be placed inside any of our repetition structures;
this includes decision structures and other loops. Loops placed inside
of loops are called nested loops.
Example:
int x,y,
for(x = 1; x < 3; x++)
{
for(y = 1; y < 4; y++)
{
printf(“sume = %f\n”, x+y);
}
}
Note how indentation and {} helps to clear up the programmer’s
intentions.
Thank You!
متشکرم