Iteration Constructs
Iteration Constructs
Iteration Constructs
Iteration constructs, also known as loops, allow a set of instructions to be performed
until a certain condition is reached. This condition may be predefined or open ended. The
loop structures available in C are :
parameter)
statement;
The initialize counter is an assignment statement that sets the loop control variable
, before the loop is entered. This statement is executed only once. The conditional test is
a relational expression which determines when the loop will exit. The re-evaluation
parameter defines how the loop control variable changes (mostly, an increment or
decrement in the variable set at the start) each time the loop is repeated. These three
sections of the for loop must be separated by semicolons. The statement which forms the
body of the loop can be either a single statement or a compound statement.
The for loop continues to execute as long as the conditional test evaluates to true.
When the condition becomes false, the program resumes on the statement following the for
loop.
for(initialize counter; conditional test; re-evaluation parameter)
1. Initialize counter:Initialize counter is always assign statement which sets loop control variable
before loop is entered.
2. Conditional test:Conditional test is always relational expression which determine when the loop
will exit.
3. Re-evaluation parameter:Re-evaluation parameter determines how loop control variable changes after
every iteration.
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in
<
50; i++,
k- -)
This example has a normal loop variable i, but it also initialises another variable j,
and decrements a third k. The variables j and k do not need to have anything with each
other, or with i.
Nested for L oops
A for is said to be nested when it occurs within another for. The code will be
something like
for ( i = 1;
{
for ( j
{
}
i
=
}
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in
The for loop can be used without one or all of its definitions.
For example,
for
{
0;
!=
255;
);
}
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in
for
checking
100;
);
This loop does not have an initialization factor or a re-evaluation factor. for loop,
when used without any definitions gives an infinite loop.
f or ( ; ; )
printf (This loop will go on and on and on...\n);,.
However, a break statement used within this loop will cause an exit from it.
for (; ;)
{
printf(This will
i = getchar () ;
if
(i ==
X
break;
}
go
||
on
and
on (x
= =
x)
WILL
exit));
The above loop will run until the user types x or X at the keyboard.
for loop (or any other loop) can also be used without the body (statements). This
helps increase efficiency of some algorithms and to create time delay loops.
for(i
0;
<
4. Accept a number (num) from the user and display its factorial.
Program:int main(void) {
unsigned int count;
int num;
long double fact = 1.0;
printf("Please enter a integer : ");
scanf("%i", &num);
for (count = 2u; count <= num; count++)
{
fact = fact * count;
}
printf("The factorial of %d is %.0Lf\n",num,fact);
return (EXIT_SUCCESS);
}
; intVar
printf("\t%d\n", intVar-- );
}
return 0;
}
// alternative method
int main(void) {
int intVar;
printf("Please enter a number : ");
scanf("%d", &intVar);
printf("Integers in the range of %d..1 are\n", intVar);
for (;;) //infinite loop
{
printf("\t%d\n", intVar--);
if (intVar == 0)
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in
:);
The above program asks for number of characters to be entered, accepts the
characters and displays number of characters entered.
The while loop accepts characters until the variable j, which is initially set at 1, is
equal to i. i is accepted initially as the number of characters to be entered, j is incremented
within the while loop.
<stdio.h>
main ()
{
int i = 0;
while
(i < 100)
{
printf(This goes on
i
+= 10;
printf(\t%d,
i);
i+= 10;
printf(\t%d,
i);
printf(\nCtrl-C will
}
}
forever, HELP!!!\n);
help);
In the above, i is 0 whenever the condition is checked, and so the loop never
terminates.
If more than one condition is to be checked to terminate a while loop, the loop will
terminate if at least one of the conditions become false. The following example illustrates
this.
#include <stdio.h>
main ()
{
int
i,
j;
i = 0;
a = 10;
while
(i < 100
{
.
.
i++ ;
a- = 2 ;
}
.
.
}
&&
>
5)
This loop will perform 3 iterations, the first time a will 10, the next time it will be 8 and
the third time it will be 6. After this though i is still less than 100 (i is 3), a becomes 4, and
the condition a > 5 becomes false, so the loop terminates.
Impetus IT Services Pvt.Ltd.
B-16, First floor, Sant Tukaram Vyapar Sankul, Sector - 24, Nigdi, Pune, Maharashtra. India. Pin 411044.
Mobile 9970600774, 9730012775|Board 91-20-27640406|Fax 91-20-27641703
Email : [email protected] | Website : www.impetusits.in
main()
{
int i = 0;
int j = 0;
int num = 0;
printf(Please enter a integer);
scanf(%d,&num);
num = ( num % 2 ) ? num + 1 : n;
for( i = 1; i <= num; j++ )
{
printf(\n);
for( j = 1; j <= num; j++ )
{
if( i == j) !! (i == (n j) + 1 )
{
printf(*);
}
else
{
printf( );
}
}
}
Return 0;}
#include <stdio.h>
main ()
{
int
x;
char
i
i,
ans;
do{
x
0;
ans
y ;
printf (\nEnter
sequence
of
character:);
do{
i
getchar () ;
x++;
} while
i =
(i
!=
\n);
while
sequences
(Y/N)?);
getchar () ;
(ans
==
| |
ans
== Y);
This program code first asks the user to enter a sequence of characters till the enter
key is hit (nested while), it then asks if more sequences of characters are to be entered. If
yes (outer while), it prompts the user to enter another sequence. This goes on till the user
hits any other key except y or Y. The program then terminates.
for
(...)
for
(...)
for
(...)
while
(...)
if
(...)
goto
errorl;
.
.
.
}
}
}
}
errorl:
printf
(Error
!!!);
statement
or
label:
{
statement
sequence
<stdio.h>
main ()
{
int
i,
for ( i
j;
= 1, j = 0; i <= 100;
i++ )
{
printf(Enter
scanf(%d,
if
( j
==
%d
i );
&j);
100 )
break;
}
}
In the above code, the user can enter 100 values for j, however, if 100 is entered, the loop
terminates and control is passed to the next statement.
Another point to be remembered while using a break is that it causes an exit from an
inner loop. This means that if a for is nested within another for, and a break statement is
encountered in the inner loop, the control is passed back to the outer for loop.
The exit()
The function exit() is a standard C library function, Its working is similar to the working
of a jump statement, the major difference being that the jump statements are used to break
out of a loop, whereas exit() is used to break out of the program. This function causes
immediate termination of the program and control is transferred back to the operating
system. An exit() is usually used to check if a mandatory condition for a program execution
is satisfied or not. The general form of an exit() is
exit(int
return _code);
where return _code is optional. Zero is generally used as a return _code to indicate normal
program termination. Other values indicate some sort of error.