122 Programming and Data Structures
Table 6.1 Loops in C
for while do-while
fo r (expression -1 ;
expression-2;
expression-3)
statement;
expression -1;
w h ile (expression-2)
{
statement;
expression -3;
}
expression -1;
do
{
statement;
expression-3;
}
while (expression-2);
The for loop statement comprises of three actions. The three actions are placed in the for
statement itself. The three actions are initialize counter, test condition and Re-evaluation parameters are
included in one statement. The expressions are separated by semi-colons (;). This leads to the
programmer to visualize the parameters easily. The for statement is equivalent to while and do-
while statement. The only difference between for and while is that the latter checks the logical
condition and then executes the body of the loop, whereas in the for statement test is always
performed at the beginning of the loop. The body of the loop may not be executed at all times if the
condition fails at the beginning.
for (a=10;a<10;a--)
prin tf ("%d", a );
For example, the above program will never execute because the test condition is not proper at the
beginning.
The do - w h ile loop executes the body of the loop at least once regardless of the logical condition.
6.2 THE for LOOP
The for loop allows to execute a set of instructions until a certain condition is satisfied. Condition
may be predefined or open-ended.
The general syntax of the loop will be as given in the Table 6.2.
Table 6.2 Syntax of for loop.
for (initialize counter ;test condition ;re-evaluation parameter)
{
statement ;
statement ;
}
Explanation
1) The initialize counter sets a loop to an initial value. This statement is executed only once.
2) The test condition is a relational expression that determines the number of iterations desired or
it determines when to exit from the loop. The ' f o r 7 loop continues to execute as long as
conditional test is satisfied. When the condition becomes false the control of the program exits
from the body of the ' for ' loop and executes next statement after the body of the loop.
3) The re-evaluation parameter decides how to make changes in the loop (quite often increase
or decrease operations are to be used). The body of the loop may contain either a single
Loop Control Statements 123
statement or multiple statements. In case there is only one statement after the fo r loop braces
may not be necessary. In such a case only one statement is executed till the condition is
satisfied. It is a good practice to use braces even for single statement following the fo r loop.
The syntax of ' f o r ' loop with only one statement will be as shown in the 3rd row of Table 6.3.
The fo r loop can be specified by different ways and it is as per Table 6.3.
Table 6.3 Various Formats of for loop
Syntax Output
Remarks
1) for ( ; ;)
Infinite loop
No arguments
2) for (a=0; a<=20;)
Infinite loop 'a' is neither increased nor decreased.
3) for (a =0;a<=10;a++)
printf ("%d",a);
Displays value
from 1 to 10
'a' is increased from 0 to 10. Curly braces
are not necessary. Default scope of for loop
is one statement after for loop.
3) for (a =10;a>=0;a—
Displays value
from 10 to 0
'a' is decreased from 10 to 0.
6.1 Print the first five numbers starting from one together with their squares.
main()
<
in t i ;
clrs cr();
for (7=2;j<=5;i++)
printf ("number: %5d it's Square : %8d",I,i*i);
I
OUTPUT:
Number: 1 it's Square: 1
Number: 2 it's Square: 4
Number 3 it's Square: 9
Number: 4 it's Square: 16
xNumben 5,it's Square: 25
Explanation Let us now examine how the program executes.
1) The value of i sets to one for the first time when the program execution starts in the fo r loop.
2) The condition i <=5 is specified and tested in each iteration. Initially the condition is true since
the value of i is 1. The statements following the fo r loop executes.
3) Upon execution of p r in tf statement compiler sends control back to the fo r loop where the
value of i is increased by one. This is repeated till the value of ' i ' is less than or equal to 5.
4) The new updated value of i if exceeds 5, the control exits from the loop.
5) The p r in tf statement executes as long as the value of i reaches to 5.
124 Programming and Data Structures
The fo r loop can be used by different ways. Various examples using fo r loops are given below.
6.2 Display numbers from 1 to 15 using for loop. Use i ++.
# in clu de <stdio.h >
# include <conio.h>
void main()
{
in t i ;
/ d r s c r () ;
for (i=l^<=15;t++)
printf ("%5d",i);
}
Q-UTFUT :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Explanation In the above program counter is initialized with a variable i =1. Testing and increase
in the counter value is done in the fo r statem en t itself. Instead of i++ we can also use i = i +1.
Here, we are illustrating a program using i = i +1.
6.3 Display numbers from 1 to 15 using for loop. Use i = i +1.
# inclu de <stdio.h >
# in clude <conio.h>
void main()
{
in t i ;
c lr s c r ();
for (i=l,i<=15;i=i+l)
printf ("%5d,i);
I
OUT P U T ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Explanation In the above program also the value of counter is increased at each time in for
statement itself. Instead of i ++, i = i +1 is used in the for statement. The same result can be
observed as found in the previous example.
The increase in a counter can be done anywhere in the body of for loop and for infinite times.
An example is stated below wherein the counter value is increased in the body of the loop and not in
the for statement.
Loop Control Statements 125
6.4 Write a program to display numbers from 1 to 16. Use Incrementation operation in the
body of the loop for more than once.
# include <stdio.h>
# include <conio.h>
void main() »
{
int i ,c » 0 ;
c l r s c r ( );
fo r (i=0,H<=15;)
I
»'++;
printf ("%5d",i);
i=i+l;
printf r%5d",i);
C+4>*
I
printf ("tin The Body of the loop is executed for %d times ,c);
I
OUTPUT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The Body of the loop is executed for 8 times.
Explanation The Body of the loop is executed for 8 times. Increment operation is done following
the for statement.. Hence, the counter which is initialized in the for statement is increased first i.e.
the value of i after the increment is one and the same is displayed with first p rin tf () statement.
After, this operation once again in the loop i is increased by 1 and the values are displayed using
second p rintf () statement. The body of the loop is executed till i=15 and for 8 times.
In the example given below the declaration of counter is done before the for statement.
6.5 Write a program to display even numbers from 0 to 14. Declare the initial counter value
before the for loop statement
# include <stdio.h>
# include <conio.h>
void mainO
{
int i* 0 ;
c l r s c r ();
fo r (,i<=15;)
printf ("%5d",i);
i+=2;
)
I
126 Programming and Data Structures
OUTPUT:
0 2 4 6 8 10 12 14
Explanation In this program the for statement contains only test condition. The (semicolon) is
essential before and after the condition. The for statement contains tw o(sem icolons) even though
no parameters are provided.
6.6 Write a program to display numbers in ascending and descending orders.
# include <stdio.h>
# include <conio.h>
void main()
{
int i=0;
clrs cr();
printf ("Numbers in Ascending Order :");
for (;++i<=10;)
printf ("%3d",i);
printf ("tin");
printf ("Numbers in Descending Order
for (;i-> l;)
printf ("%3d",i);
}
O U TPUT :
N um bers in A scending O rder : 1 2 3456789 10
N um bers in D escending O rder: 10 987654321
Explanation In the above example comparison and increment operations are done in the first f o r
loop. The decrement operation is in the second f o r loop.
With++i<=10 (++ p reced e s i ) firstly, counter is increased and comparison is made and the
result is displayed through the printf () statement.
With i -->1, counter value is compared first. After satisfying, the decreased operation is performed
and the value is displayed.
6.7 Write a program to display numbers up to 10 using infinite for loop. Use goto statement
to exit from the loop.
# include <stdio.h>
# include <conio.h>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.77