Professional Documents
Culture Documents
Loops PDF
Loops PDF
Instruction
Loops
The while Loop
Tips and Traps
More Operators
Summary
Exercise
81
82 Let Us C
Loops
The versatility of the computer lies in its ability to perform a set of
instructions repeatedly. This involves repeating some portion of the
program either a specified number of times or until a particular
condition is being satisfied. This repetitive operation is done through a
loop control instruction.
There are three methods by way of which we can repeat a part of a
program. They are:
(a) Using a for statement
(b) Using a while statement
(c) Using a do-while statement
Each of these methods is discussed in the following pages.
START
count = 1
is No
count <= 3
Yes
INPUT p, n, r
si = p * n * r / 100
PRINT si
count = count + 1
STOP
Figure 5.1
Let us now write a program that implements the logic of this flowchart.
count = 1 ;
while ( count <= 3 )
{
printf ( "\nEnter values of p, n and r " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "Simple interest = Rs. %\nf", si ) ;
count = count + 1 ;
84 Let Us C
}
return 0 ;
}
The program executes all statements after the while 3 times. The logic
for calculating the simple interest is written in these statements and
they are enclosed within a pair of braces. These statements form the
‘body’ of the while loop. The parentheses after the while contain a
condition. So long as this condition remains true the statements in the
body of the while loop keep getting executed repeatedly. To begin with,
the variable count is initialized to 1 and every time the simple interest
logic is executed, the value of count is incremented by one. The variable
count is often called either a ‘loop counter’ or an ‘index variable’.
The operation of the while loop is illustrated in Figure 5.2.
START
initialise
False
test
Tru
e
body of loop
increment
STOP
Figure 5.2
Chapter 5: Loop Control Instruction 85
while ( i <= 10 )
while ( i >= 10 && j <= 15 )
while ( j > 10 && ( b < 15 || c < 20 ) )
while ( i <= 10 )
i=i+1;
is same as
while ( i <= 10 )
{
i=i+1;
}
86 Let Us C
Almost always, the while must test a condition that will eventually
become false, otherwise the loop would be executed forever,
indefinitely.
# include <stdio.h>
int main( )
{
int i = 1 ;
while ( i <= 10 )
printf ( "%d\n", i ) ;
return 0 ;
}
# include <stdio.h>
int main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i=i+1;
}
return 0 ;
}
# include <stdio.h>
int main( )
{
int i = 5 ;
while ( i >= 1 )
{
printf ( "Make the computer literate!\n" ) ;
i=i-1;
}
}
Chapter 5: Loop Control Instruction 87
It is not necessary that a loop counter must only be an int. It can
even be a float.
# include <stdio.h>
int main( )
{
float a = 10.0 ;
while ( a <= 10.5 )
{
printf ( "Raindrops on roses..." ) ;
printf ( "...and whiskers on kittens\n" ) ;
a = a + 0.1 ;
}
return 0 ;
}
# include <stdio.h>
int main( )
{
int i = 1 ;
while ( i <= 10 ) ;
{
printf ( "%d\n", i ) ;
i=i+1;
}
return 0 ;
}
This is an indefinite loop, and it doesn’t give any output at all. The
reason is, we have carelessly given a ; after the while. It would
make the loop work like this...
while ( i <= 10 )
;
{
printf ( "%d\n", i ) ;
88 Let Us C
i=i+1;
}
More Operators
There are several operators that are frequently used with while. To
illustrate their usage, let us consider a problem wherein numbers from 1
to 10 are to be printed on the screen. The program for performing this
task can be written using while in following different ways:
int i = 0 ;
while ( ++i <= 10 )
printf ( "%d\n", i ) ;
return 0 ;
}
Summary
(a) The three type of loops available in C are for, while, and do-while.
(b) In a while loop there are three distinct steps of initialization, testing
and incrementation of loop counter.
(c) The loop counter can be incremented or decremented by any
suitable step value.
(d) There are two forms of ++ operator—pre-increment and post-
increment.
(e) There are two forms of -- operator—pre-decrement and post-
decrement.
Exercise
[A] What will be the output of the following programs:
(a) # include <stdio.h>
int main( )
{
int i = 1 ;
while ( i <= 10 ) ;
{
printf ( "%d\n", i ) ;
i++ ;
}
return 0 ;
}
(b) # include <stdio.h>
int main( )
Chapter 5: Loop Control Instruction 91
{
int x = 4, y, z ;
y = --x ;
z = x-- ;
printf ( "%d %d %d\n", x, y, z ) ;
return 0 ;
}
(c) # include <stdio.h>
int main( )
{
int x = 4, y = 3, z ;
z = x-- - y ;
printf ( "%d %d %d\n", x, y, z ) ;
return 0 ;
}
(d) # include <stdio.h>
int main( )
{
while ( 'a' < 'b' )
printf ( "malayalam is a palindrome\n" ) ;
return 0 ;
}
(e) # include <stdio.h>
int main( )
{
int i ;
while ( i = 10 )
{
printf ( "%d\n", i ) ;
i=i+1;
}
return 0 ;
}
(f) # include <stdio.h>
int main( )
{
float x = 1.1 ;
while ( x == 1.1 )
{
92 Let Us C
printf ( "%f\n", x ) ;
x = x – 0.1 ;
}
return 0 ;
}
[B] Attempt the following:
(f) Write a program for a matchstick game being played between the
computer and a user. Your program should ensure that the
computer always wins. Rules for the game are as follows:
(g) Write a program to enter numbers till the user wants. At the end it
should display the count of positive, negative and zeros entered.
(h) Write a program to receive an integer and find its octal equivalent.
(i) (Hint: To obtain octal equivalent of an integer, divide it continuously
by 8 till dividend doesn’t become zero, then write the remainders
obtained in reverse direction.)
Chapter 5: Loop Control Instruction 93
(j) Write a program to find the range of a set of numbers entered
through the keyboard. Range is the difference between the smallest
and biggest number in the list.
94 Let Us C
6 More Complex
Repetitions
The for Loop
Nesting of Loops
Multiple Initializations in the for Loop
The break Statement
The continue Statement
The do-while Loop
The Odd Loop
Summary
Exercise
95
96 Let Us C
Let us now write down the simple interest program using for. Compare
this program with the one that we wrote using while. The flowchart is
also given in Figure 6.1 for a better understanding.
Chapter 6: More Complex Repetitions 97
START
count = 1 is No
count = count + 1 count <= 3
Yes
INPUT p, n, r
si = p * n * r / 100
PRINT si
STOP
Figure 6.1
si = p * n * r / 100 ;
printf ( "Simple Interest = Rs.%f\n", si ) ;
}
return 0 ;
}
If you compare this program with the one written using while, you can
observe that the three steps—initialization, testing and
incrementation—required for the loop construct have now been
incorporated in the for statement.
Let us now examine how the for statement gets executed:
98 Let Us C
When the for statement is executed for the first time, the value of
count is set to an initial value 1.
Next the condition count <= 3 is tested. Since count is 1, the
condition is satisfied and the body of the loop is executed for the
first time.
Upon reaching the closing brace of for, control is sent back to the for
statement, where the value of count gets incremented by 1.
Again the test is performed to check whether the new value of
count exceeds 3.
If the value of count is less than or equal to 3, the statements within
the braces of for are executed again.
The body of the for loop continues to get executed till count doesn’t
exceed the final value 3.
When count reaches the value 4, the control exits from the loop and
is transferred to the statement (if any) immediately after the body of
for.
START
initialize
False
test
True
body of loop
increment
STOP
Figure 6.2
Chapter 6: More Complex Repetitions 99
It is important to note that the initialization, testing and incrementation
part of a for loop can be replaced by any valid expression. Thus the
following for loops are perfectly ok.
for ( i = 10 ; i ; i -- )
printf ( "%d ", i ) ;
for ( i < 4 ; j = 5 ; j = 0 )
printf ( "%d ", i ) ;
for ( i = 1; i <=10 ; printf ( "%d ", i++ ) )
;
for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
printf ( "%d", i ) ;
return 0 ;
}
Here, the incrementation is done within the body of the for loop
and not in the for statement. Note that, in spite of this, the
semicolon ( ; ) after the condition is necessary.
Here again, both, the comparison and the incrementation are done
through the same expression, ++i <= 10. Since ++ precedes i firstly
incrementation is done, followed by comparison. Note that it is
necessary to initialize i to 0.
Nesting of Loops
The way if statements can be nested, similarly whiles and fors can also
be nested. To understand how nested loops work, look at the program
given below.
When you run this program, you will get the following output:
102 Let Us C
r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5
Here, for each value of r, the inner loop is cycled through twice, with the
variable c taking values from 1 to 2. The inner loop terminates when the
value of c exceeds 2, and the outer loop terminates when the value of r
exceeds 3.
As you can see, the body of the outer for loop is indented, and the body
of the inner for loop is further indented. These multiple indentations
make the program easier to understand.
Instead of using two statements, one to calculate sum and another to
print it out, we can compact them into one single statement by saying:
The way for loops have been nested here, similarly, two while loops can
also be nested. Not only this, a for loop can occur within a while loop, or
a while within a for.
# include <stdio.h>
int main( )
{
int num, i ;
i=2;
while ( i <= num - 1 )
{
if ( num % i == 0 )
{
printf ( "Not a prime number\n" ) ;
break ;
}
i++ ;
}
if ( i == num )
printf ( "Prime number\n" ) ;
}
In this program, the moment num % i turns out to be zero, (i.e., num is
exactly divisible by i), the message “Not a prime number” is printed and
the control breaks out of the while loop. Why does the program require
the if statement after the while loop at all? Well, there are two
possibilities the control could have reached outside the while loop:
(a) It jumped out because the number proved to be not a prime.
104 Let Us C
(b) The loop came to an end because the value of i became equal to
num.
When the loop terminates in the second case, it means that there was
no number between 2 to num - 1 that could exactly divide num. That is,
num is indeed a prime. If this is true, the program should print out the
message “Prime number”.
The keyword break, breaks the control only from the while in which it is
placed. Consider the following program, which illustrates this fact:
# include <stdio.h>
int main( )
{
int i = 1 , j = 1 ;
In this program when j equals 150, break takes the control outside the
inner while only, since it is placed inside the inner while.
# include <stdio.h>
Chapter 6: More Complex Repetitions 105
int main( )
{
int i, j ;
12
21
Note that when the value of i equals that of j, the continue statement
takes the control to the for loop (inner) bypassing the rest of the
statements pending execution in the for loop (inner).
do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;
having executed the statements within the loop. Figure 6.3 would clarify
the execution of do-while loop still further.
START
initialize
body of loop
increment
True
test
False
STOP
Figure 6.3
This means that do-while would execute its statements at least once,
even if the condition fails for the first time. The while, on the other hand
will not execute its statements if the condition fails for the first time.
This difference is brought about more clearly by the following program:
# include <stdio.h>
int main( )
{
while ( 4 < 1 )
printf ( "Hello there \n" ) ;
return 0 ;
}
Here, since the condition fails the first time itself, the printf( ) will not
get executed at all. Let's now write the same program using a do-while
loop.
# include <stdio.h>
int main( )
{
Chapter 6: More Complex Repetitions 107
do
{
printf ( "Hello there \n" ) ;
} while ( 4 < 1 ) ;
return 0 ;
}
In this program, the printf( ) would be executed once, since first the
body of the loop is executed and then the condition is tested.
return 0 ;
}
Enter a number 5
square of 5 is 25
Want to enter another number y/n y
108 Let Us C
Enter a number 7
square of 7 is 49
Want to enter another number y/n n
In this program, the do-while loop would keep getting executed till the
user continues to answer y. The moment user answers n, the loop
terminates, since the condition ( another == 'y' ) fails. Note that this
loop ensures that statements within it are executed at least once even if
n is supplied first time itself.
Perhaps you are wondering what for have we used the function fflush( ).
The reason is to get rid of a peculiarity of scanf( ). After supplying a
number when we hit the Enter key, scanf( ) assigns the number to
variable num and keeps the Enter key unread in the keyboard buffer. So
when it’s time to supply Y or N for the question ‘Want to enter another
number (y/n)’, scanf( ) will read the Enter key from the buffer thinking
that user has entered the Enter key. To avoid this problem, we use the
function fflush( ). It is designed to remove or ‘flush out’ any data
remaining in the buffer. The argument to fflush( ) must be the buffer
which we want to flush out. Here we have used ‘stdin’, which means
buffer related with standard input device, i.e., keyboard.
Though it is simpler to program such a requirement using a do-while
loop, the same functionality if required, can also be accomplished using
for and while loops as shown below.
break and continue are used with do-while just as they would be in a
while or a for loop. A break takes you out of the do-while bypassing the
conditional test. A continue sends you straight to the test at the end of
the loop.
Summary
(a) Unlike a while loop, in a for loop the initialization, test and
incrementation are written in a single line.
(b) A break statement takes the execution control out of the loop.
(c) A continue statement skips the execution of the statements after it
and takes the control through the next cycle of the loop.
(d) A do-while loop is used to ensure that the statements within the
loop are executed at least once.
(e) The operators +=, -=, *=, /=, %= are compound assignment
operators. They modify the value of the operand to the left of
them.
110 Let Us C
Exercise
[A] What will be the output of the following programs:
(a) # include <stdio.h>
int main( )
{
int i = 0 ;
for ( ; i ; )
printf ( "Here is some mail for you\n" ) ;
return 0 ;
}
(b) # include <stdio.h>
int main( )
{
int i ;
for ( i = 1 ; i <= 5 ; printf ( "%d\n", i ) ) ;
i++ ;
return 0 ;
}
(c) # include <stdio.h>
int main( )
{
int i = 1, j = 1 ;
for ( ; ; )
{
if ( i > 5 )
break ;
else
j += i ;
printf ( "%d\n", j ) ;
i += j ;
return 0 ;
}
}
[B] Answer the following:
(a) The three parts of the loop expression in the for loop are:
1. An if statement
2. A for loop
3. A program
4. The main( ) function
(c) A do-while loop is useful when we want that the statements within
the loop must be executed:
1. Only once
2. At least once
3. More than once
4. None of the above
1. int i = 1 ; 2. for ( ; ; ) ;
while ( 1 )
{
i++ ;
}
3. int t = 0, f ; 4. int y, x = 0 ;
while ( t ) do
{ {
f=1; y=x;
} } while ( x == 0
);
(f) Which keyword is used to take the control to the beginning of the
loop?
(g) How many times the while loop in the following C code will get
executed?
112 Let Us C
# include <stdio.h>
int main( )
{
int j = 1 ;
while ( j <= 255 ) ;
{
printf ( "%c %d ", j, j ) ;
j++;
}
return 0 ;
}
(i) Which of the following statement is true about a for loop used in a
C program?
1. for loop works faster than a while loop.
2. All things that can be done using a for loop can also be done
using a while loop.
3. for ( ; ; ) implements an infinite loop.
4. for loop can be used if we want statements in a loop to get
executed at least once.
5. for loop works faster than a do-while loop.
Chapter 6: More Complex Repetitions 113
[C] Attempt the following:
(a) Write a program to print all prime numbers from 1 to 300. (Hint:
Use nested loops, break and continue)
(b) Write a program to fill the entire screen with a smiling face. The
smiling face has an ASCII value 1.
(c) Write a program to add first seven terms of the following series
1 2 3 ……
1! 2! 3! .
using a for loop:
(d) Write a program to generate all combinations of 1, 2 and 3 using for
loop.
(e) A machine is purchased which will produce earning of Rs. 1000 per
year while it lasts. The machine costs Rs. 6000 and will have a
salvage value of Rs. 2000 when it is condemned. If 9 percent per
annum can be earned on alternate investments, write a program to
determine what will be the minimum life of the machine to make it
a more attractive investment compared to alternative investment?
(c) Write a program to print the multiplication table of the number
entered by the user. The table should get displayed in the
following form:
29 * 1 = 29
29 * 2 = 58
…
(f) According to a study, the approximate level of intelligence of a
person can be calculated using the following formula:
i = 2 + ( y + 0.5 x )
Write a program that will produce a table of values of i, y and x,
where y varies from 1 to 6, and, for each value of y, x varies from
5.5 to 12.5 in steps of 0.5.
(g) When interest compounds q times per year at an annual rate of
r % for n years, the principal p compounds to an amount a as per
the following formula
nq
a=p(1+r/q)
Write a program to read 10 sets of p, r, n & q and calculate the
corresponding as.
114 Let Us C
x 1 1 x 1 1 x 1 1 x 1
2 3 4
....
x 2 x 2 x 2 x
1
2 3
4 5 6
7 8 9 10
(n) Write a program to produce the following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
116 Let Us C