0% found this document useful (0 votes)
44 views37 pages

Loops: Introduction and

This document discusses loops and the for statement in C programming. It begins by defining what a loop is and the different types of loops. It then focuses on the for statement, explaining its general structure and how it allows a programmer to repeat a block of code a specific number of times. Examples are provided to demonstrate how to use the for statement to iterate over a range of values or countdown. The document also discusses nested for loops, where one for loop is contained within another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views37 pages

Loops: Introduction and

This document discusses loops and the for statement in C programming. It begins by defining what a loop is and the different types of loops. It then focuses on the for statement, explaining its general structure and how it allows a programmer to repeat a block of code a specific number of times. Examples are provided to demonstrate how to use the for statement to iterate over a range of values or countdown. The document also discusses nested for loops, where one for loop is contained within another.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Programming Methodology &

Abstactions
1

Lecture 10
Loops: Introduction and for






CS106
Programming Methodology
& Abstractions
Spring 2003
Balochistan University of IT & MS
Faculty of System Sciences
Sadique Ahmed Bugti
Programming Methodology &
Abstactions
2
What is a loop?
A loop allows a program to repeat a group of
statements any number of times, or until some
loop condition occurs.
The loop body contains the statements to be
repeated.
Commonly known as iterative or repetition
statements:
for
while
do-while
Programming Methodology &
Abstactions
3
What is a loop?
Programming Methodology &
Abstactions
4
What is a loop?
Programming Methodology &
Abstactions
5
Counting Loops
Counting loops are used when it can
be determined before loop execution
how many loop repetitions will be
needed.
while
for
Programming Methodology &
Abstactions
6
Conditional Loops
Conditional loops are used when a loop
should be repeated until a desired
condition is met.
while
do-while
for
Programming Methodology &
Abstactions
7
for statement
Situations in which the number of passes through
the loop is known in advance are often best dealt
with using a for statement.
The general form of a for statement is
for (expr1; expr2; expr3)
statement;
where expr1 is used to initialize some parameter
(called a control variable, or index) that controls
the looping action, expr2 represents a condition
that must be true for the loop to continue
execution, and expr3 is used to modify the value
of the control variable initially assigned by expr1 .
Programming Methodology &
Abstactions
8
for statement
The for statement can also incorporate a
compound statement:

for (expr1; expr2; expr3)
{
statement 1 ;
statement 2 ;
...
statement n ;
}
Programming Methodology &
Abstactions
9
An example without loops
Sum the first eight odd numbers:
int sum = 0;
sum = sum + 1;
sum = sum + 2;
sum = sum + 2;
sum = sum + 2;
sum = sum + 2;
sum = sum + 2;
sum = sum + 2;
sum = sum + 2;
Programming Methodology &
Abstactions
10
An example using loops
int i, sum = 0;

for (i=1; i<=15; i = i + 2)
sum = sum + i;
Programming Methodology &
Abstactions
11
for statement
When a for statement is executed, expr2 is
evaluated and tested at the beginning of each
pass through the loop, whereas expr3 is
evaluated at the end of each pass.
If the loop-continuation condition is initially
false, the body portion of the loop is not
performed.
Any of the three parts can be omitted, although
the semicolons must remain.
Programming Methodology &
Abstactions
12
for statement
for structure

expr1
expr2
expr3 Body of loop
true
false
Programming Methodology &
Abstactions
13
for statement
for ( i = 1 ; i <= n ; i = i + 1)
for keyword control variable i
final value of control variable
for which the condition is true
initial value of control variable increment of control variable
loop-continuation condition
Programming Methodology &
Abstactions
14
for statement
i = 1
i <= n printf(%d, i); i = i + 1
body of loop increment the
control variable
determine if the final value of the
control variable has been reached
true
false
establish initial value of
control variable
Programming Methodology &
Abstactions
15
for statement
i <= 4
statement1
statement 2

statement n
i = 2
i <= 4
statement1
statement 2

statement n
i = 3
i <= 4
statement1
statement 2

statement n
i = 4
i <= 4
statement1
statement 2

statement n
i = 5
i = 1
condition
false
exit loop
i <= 4
Programming Methodology &
Abstactions
16
for statement - omissions
Any of the three parts can be omitted, although
the semicolons must remain.
If expr1 can be omitted if the control variable is
initialized elsewhere:

i = 0;
for ( ; i <= n ; i++)
{

}
Programming Methodology &
Abstactions
17
for statement - omissions
If expr3 can be omitted if the control variable is
altered by statements in the body of the for
loop:

for (i = 1; i <= n; )
{

i = i + 1;
}
Programming Methodology &
Abstactions
18
for statement - omissions
If the logical expression, expr2 is not
present, it is taken as permanently true,
so
for ( ; ; )
{

}
is an infinite loop.
Programming Methodology &
Abstactions
19
for statement
expr3: altering the value of the control variable
This expression acts like a standalone C
statement at the end of the body of the for loop.
Therefore, the expressions:
i = i +1
i += 1
++i
i++
are all equivalent in incrementing the value of I by 1.
Programming Methodology &
Abstactions
20
for statement
For example for a loop :

for (i = 1 ; i <= n ; i++)
statement;

for (i = 1 ; i <= n ; i = i + 1)
statement;
Programming Methodology &
Abstactions
21
for statement
The value of expr3 may increment or
decrement the variable.
If expr3 is negative, the loop actually
counts downwards.
For example:

for (i = n ; i >= 0 ; i--)
statement;
Programming Methodology &
Abstactions
22
for statement
The initialization, loop-continuation
condition and control-variable
modification can contain arithmetic
expressions.
For example:
for (i = n; i <= n * j; i++)
statement;
Programming Methodology &
Abstactions
23
Examples using the for statement
Vary the control variable from 1 to 100 in
increments of 1.
for (i = 1 ; i <= 100 ; i++)
Vary the control variable from 100 to 1 in
increments of 1 (decrements of 1).
for (i = 100; i >= 1; i--)
Vary the control variable for 3 to 33 in
increments of 3.
for (i = 3; i <= 33; i+=3)
Programming Methodology &
Abstactions
24
Examples using the for statement
Vary the control variable from 20 to 2 in
steps of -2.

for (i = 20 ; i >= 2 ; i-=2 )
Programming Methodology &
Abstactions
25
Control Variables
Can you change the value of the counter
variable in the body of a for loop?
Yes, but the practice is not recommended.
A counter variable is used to control the loop,
so its value should be changed in the
modification expression, not in the loop body.
The following loop is correct, but would
cause an infinite loop:
for (k=1; k<3; k++)
k=1;
Programming Methodology &
Abstactions
26
for statement
#include <stdio.h>
void main(void)
{
/* A program to produce a Celsius to Fahrenheit
conversion chart for the numbers 0 to 100 */

int celsius;
for (celsius = 0; celsius <= 100; celsius++)
printf(Celsius: %d Fahrenheit: %d\n,
celsius, (celsius * 9) / 5 + 32);
}
Programming Methodology &
Abstactions
27
for statement
There are two common problems with
use of the for statement. They can have
too little content, or too much.
Programming Methodology &
Abstactions
28
Missing parts of for loops
The for statement is actually three statements in
one. Sometimes all three parts are not needed, so
one or more is left blank:
for ( ; start < end ; start++)

The initialization section of the for is the empty
statement ;. But with just a ; to guide you,
How can you tell that the initialization statement
hasnt been forgotten?
Including the comment: /* Start condition set */
tells you the omission was intentional.
Programming Methodology &
Abstactions
29
Missing parts of for loops
A comment is also needed when there is
no conditional clause. For example:

for (start = 0; /* break below */; start++)
{
.
}
Programming Methodology &
Abstactions
30
The comma operator can be used to combine
statements in a for statement. For example,
the statement:

for (two = 2, three = 3; two < 50; two +=2, three += 3)

is perfectly legal. This statement causes the
variable two to increment by 2 and the variable
three to increment by 3, all in one loop.
Overstuffed for loops
Programming Methodology &
Abstactions
31
Nested for loops
Loops can sometimes be nested
Nested means that there is a loop within a loop. A
nested loop is executed, essentially, from the
inside out.
Each loop is like a layer and has its own counter
variable, its own loop expression and its own loop
body.
In a nested loop, for each value of the outermost
counter variable, the complete inner loop will be
executed once.
Programming Methodology &
Abstactions
32
Nested for loops
Can you deduce how a nested loop is executed?
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 5; j+=2)
{
k = i + j;
printf(i=%d, j=%d, k=%d, i, j, k);
}
}
i is the outer loop counter and j is the inner loop counter.
The outer loop is executed 4 times, when i =1,2,3, and 4. For
each i value the inner loop is executed three times. Since
j=1,3, and 5 the inner loop is executed 4*3 or 12 times.
inner
loop
outer
loop
Programming Methodology &
Abstactions
33
Nested for loops
i=1, j=1, k=2
i=1, j=3, k=4
i=1, j=5, k=6
i=2, j=1, k=3
i=2, j=3, k=5
i=2, j=5, k=7
i=3, j=1, k=4
i=3, j=3, k=6
i=3, j=5, k=8
i=4, j=1, k=5
i=4, j=3, k=7
i=4, j=5, k=9
Programming Methodology &
Abstactions
34
Nested for loops
The general form of the nested for statement
loop is:
for (loop1_exprs)
{
loop_body_1a
for (loop2_exprs)
{
loop_body_2
}
loop_body_1b
}
Programming Methodology &
Abstactions
35
Nested for loops
How many nests can be used for nested
for loops?
Most compilers allow 15 nesting levels.
Programming Methodology &
Abstactions
36
Nested for loops
What nesting patterns for looping
control structures are allowable?
Programming Methodology &
Abstactions
37
The End

You might also like