Labsheet 5
Labsheet 5
Objectives
1. for loop
2. while loop
3. do-while loop
4. break/continue in loops
5. Nested loops
Iterative/Loop Constructs
Iterative constructs are used to execute a block/single line of code for a specied number
of times or until a condition is satised. Iterative constructs declaration and execution can
be done in the following way,
1. declare one or more variables to be used for condition check
2. check condition to start loop
3. execute conditions inside loop
4. change value of variables to be used for condition check
The execution of a loop depends upon satisfaction of a condition. If the loop condition
yields non zero value(negative and positive values), the loop would continue to execute,
otherwise the execution control would be transferred to the next line after the loop block1 .
Iterative constructs are classied as:
for
while
1 The
part of code that would execute until loop condition satised is referred to as loop block here
do-while
Figure 2 shows the owchart of exit controlled loop.
for is an entry controlled iterative construct. First initialization is done and then condition is checked. If condition yields non zero(True) value then loop block is executed. after
completion of execution of loop block, values of variables used in condition are changed and
then condition is rechecked for next iteration. This continues until the condition yields 0
(False).
and also as
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t i =0;
f o r ( ; i <10;){
p r i n t f ( " H e l l o World\n " ) ;
i ++;
}
}
or simply
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t i =0;
for (;;){
p r i n t f ( " H e l l o World\n " ) ;
i ++;
i f ( i >=10)
break ;
}
}
As already explained, the 3 parts of the for loop are optional.
In the last code, the initialization was done right before for statement. Then the value was
updated at i++. Then the condition check was done at if (i >= 10) statement. If the 'if
condition' is satised, break statement will take the control out of the for construct.
Answer to yourself: what would the following program do?
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
for (;;){
p r i n t f ( " H e l l o World\n " ) ;
}
}
while construct is also an entry-controlled looping structure like for. If the condition
evaluates to non zero then loop block is executed, otherwise control jumps out of the while
loop to the next statement after the while block.
A program to print table of a number after receiving number as input from the user
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t num , i =1;
p r i n t f ( " Enter a number : " ) ;
s c a n f ("%d",&num ) ;
w h i l e ( i <=10){
p r i n t f ("%d %d = %d\n" ,num , i , i num ) ;
num=num+1;
}
}
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t n , x , i , j =0;
p r i n t f ( " Enter n , x : " ) ;
s c a n f ("%d%d",&n,&x ) ;
i=x ;
w h i l e ( i >1){
i=i /n ;
j ++;
}
p r i n t f ( " c e i l ( log_%d(%d))=%d\n " , n , x , j ) ;
}
do-while is an exit controlled iterative construct unlike for and while. Here the body of
the loop is executed rst and then the condition is checked. If the condition evaluates to
non zero then the loop body execution is continued otherwise it stops executing loop body.
Thus this loop is executed at least once without considering the condition. Refer to Figure
2 for a owchart.
Structure of do-while
// d e c l a r a t i o n
do{
// s t a t e m e n t ( s )
} while ( condition ) ;
Here the loop block is executed rst of all after which condition in the while bracket
is checked. If that condition is satised then the statements of loop block are re-executed,
otherwise the control is transferred to rst statement after the while statement of do-while
loop.
i f ( n%2)
else
Note : The char variable dm in the above program is a variable used to store the newline
entered by the user after inputting n. Otherwise, that newline would go into ch variable and
loop would exit at once. Try the code by removing variable dm yourself.
Innite loop
while (1){
// s t a t e m e n t ( s )
}
or
for (;;){
// s t a t e m e n t ( s )
}
or
do{
// s t a t e m e n t ( s )
} while ( 1 ) ;
All the loops above will run innitely. How to control such loops? The answer lies in break
statement.
or
i =0;
do{
i ++;
i f ( i ==100)
break ;
} while ( 1 ) ;
");
if
if
if
if
if
with
with
with
with
with
break \n " ) ;
break \n " ) ;
break \n " ) ;
break \n " ) ;
break \n " ) ;
to 34), (36 to 41), (43 to 48), (50 to 55), (57 to 62), (64 to 69), (71 to 76), (78 to 83),
(85 to 90),(92 to 97) and (99 to 100). continue statement would facilitate us under such
circumstances when there are certain conditions, on satisfaction of which execution of block
after some point should not be done.
Nested Loops
Just like 'if' constructs, iterative constructs can also be nested and can be nested upto as
many levels as we want. Nested iterative constructs are quite useful as the coming Examples
will show.
Note : we can nest any construct into another dierent construct as well up to any level.
while ( condition1 ){
f o r ( i n i t i a l i z a t i o n ; c o n d i t i o n 2 ; update ) {
do{
while ( condition4 ){
do{
f o r ( i n i t i a l i z a t i o n ; c o n d i t i o n 6 ; update )
// f u r t h e r n e s t i n g
}
} while ( condition5 ) ;
}
} while ( condition3 ) ;
...
1 2 3 4 ...
1
1 2
1 2 3
1 2 3 4
n
Try yourself
What will be the outputs of following code snippets(Note the semicolon right after the
"for" and "while" brackets close):
1.
2.
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
int i , j ;
f o r ( i =0; i <20; i ++){
i f ( i ==0|| i ==19){
f o r ( j =0; j <20; j ++){
p r i n t f ( "_" ) ;
}
p r i n t f ("\n " ) ;
}
e l s e i f ( i !=0&& i !=19){
printf ("|");
f o r ( j =1; j <19; j ++){
p r i n t f (" " ) ;
}
printf ("|\n");
}
}
Exercises
1. Approximate the value of pi using the following series expansion.
pi = 4 4/3 + 4/5 4/7 + . . . + (-1)n-1 4/(2n+1) + . . .
2. Find all prime numbers that are less than 100. Recall that a number is prime only if
the only numbers that evenly divide it are 1 and itself.
3. Write a program to reverse a number i.e. 12345 to 54321
2 Note : the result of code may appear to be rectangle because vertical 1 unit might not be the same as
horizontal 1 unit on screen.
(3)
(4)
N and x will be input values. Use math library for sqrt function. Note that sqrt takes
a double argument and returns a double value. To obtain better precision dene x as a
double and obtain the sum as a double value as well. Also note that the math library
has to be linked explicitly.
12. Write a program to nd the rst N prime numbers.
13. Write a program to print the following pyramid.
0
111
22222
3333333
444444444
Solution 1
#i n c l u d e <s t d i o . h>
i n t main ( )
{
i n t count , numOfTerms ;
double pi = 0 ;
Solution 3
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t num , r e v e r s e =0,temp ;
s c a n f ("%d",&num ) ;
temp=num ;
w h i l e ( temp !=0){
r e v e r s e=r e v e r s e 10+(temp %10);
temp=temp / 1 0 ;
}
p r i n t f ("%d\n " , r e v e r s e ) ;
}
Solution 9
#i n c l u d e <s t d i o . h>
v o i d main ( ) {
i n t num , b i n a r y =0,temp , i =1;
s c a n f ("%d",&num ) ;
temp=num ;
w h i l e ( temp !=0){
b i n a r y=b i n a r y +(temp%2) i ;
i=i 1 0 ;
temp=temp / 2 ;
}
p r i n t f ("%d\n " , b i n a r y ) ;
}