Looping and Iteration


Looping and Iteration

Looping is a powerful programming technique through which a group of statements is
executed repeatedly, until certain specified condition is satisfied. Looping is also
called a repetition or iterative control mechanism.
C provides three types of loop control structures. They are:
for statement
while statement
do-while statement

 


The for statement:


The for loop statement is useful to repeat a statement/s a known number of
times. The general syntax is as follows:
for (initialization; condition;
operation) statement;

The initialization is generally an assignment statement that is used to set the
loop control variable.
The condition is an expression(relational/logical/arithmetic/bitwise ....)
that determines when the loop exists.
The Operation defines how the loop control variable changes each time the loop
is repeated.
We must separate these three major sections by semicolon.
The for loop continues to execute as long as the condition is true. Once the condition
becomes false, program execution resumes on the statement following the for. The
control flow of the for statement is as follows:

Example 1:
// printing all odd and even numbers between 1 to
5 int x;
main ()
{
for (x=1; x <=5 ; x++)
{
if( x % 2 == 0 )
printf( ― %d is EVEN \n‖,x);
else
printf(― %d is ODD \n‖,x);

}
}
Output to the screen:
1 is ODD
2 is EVEN
3 is ODD
4 is EVEN
5 is EVEN
Example 2:
// sum the squares of all the numbers between 1 to 5
main()
{
int x, sum = 0;
for (x = 1; x <= 5; x ++)
{
sum = sum + x * x;
}
printf (―\n Sum of squares of all the numbers between 1 to 5 = %d ‖, sum);
}
Output to the screen:
Sum of squares of all the numbers between 1 to 5 = 55

The comma ( , ) operator is used to extend the flexibility of the for loop. It allows
the general form to be modified as follows:
for (initialization_1, initialization_2; condition; operation_1,
operation_2) statement;

All the following are legal for statements in C. The practical application of such
statements is not important here, we are just trying to illustrate peculiar features that
may be useful:
1. for (x=0; ((x>3) && (x<9)); x++)
2. for (x=0,y=4; ((x>3) && (y<9)); x++, y+=2)
3. for (x=0, y=4, z=4000; z; z/=10)
The second example shows that multiple expressions can be separated by a , (comma).
Example:
main()
{
int j ;
double degC, degF;
clrscr ();
printf (―\n Table of Celsius and Fahrenheit degrees \n\n‖);
printf (―Celsius Degree \t Fahrenheit Degree \n―)
degC = -20.0;
for (j = 1; j <= 6; j++)
{
degC = degC + 20.0;
degF = (degC * 9.0/5.0) + 32.0;
printf (―\n %7.2lf\t\ %7.2lf ―, degC, degF);
}
}
Output:
Table of Celsius and Fahrenheit degrees
Celsius Degree Fahrenheit Degree
0.00 32.00
20.00 68.00
40.00 104.00
60.00 140.00
80.00 176.00
100.00 212.00


Nested for loop:


Nested loops consist of one loop placed inside another loop. An example of a nested
for loop is:
for (initialization; condition; operation)
{
for (initialization; condition; operation)
{
statement;
}
statement;
}

In this example, the inner loop runs through its full range of iterations for each single
iteration of the outer loop.
Example:
Program to show table of first four powers of numbers 1 to 9.
#include <stdio.h >
void main()
{
int i, j, k, temp;
printf("I\tI^2\tI^3\tI^4 \n");
printf("-------------------------------- \n");
for ( i = 1; i < 10; i ++) /* Outer loop */
{
for (j = 1; j < 5; j ++) /* 1st level of nesting */
{
temp = 1;
for(k = 0; k < j; k ++)
temp = temp * I;
printf ("%d\t", temp);
}
printf ("\n");
}
}
Output to the screen:
I I ^

2 I ^ 3 I ^ 4
-----------------------------------
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
7 49 343 2401
8 64 512 4096
9 81 729 6561


Infinite for loop:


We can make an endless loop by leaving the conditional expression empty as given
below:
for( ; ; )
printf(―This loop will run for ever‖);

To terminate the infinite loop the break statement can be used anywhere inside
the body of the loop. A sample example is given below:

for(; 😉
{
ch = getchar
(); if(ch == ̳A‘)
break;

}
printf(―You typed an A‖);
This loop will run until the user types an A at the keyboard.
2.4.4. for with no bodies:
A C-statement may be empty. This means that the body of the for loop may also be
empty. There need not be an expression present for any of the sections. The
expressions are optional.
Example 1:
/* The loop will run until the user enters 123 */
for( x = 0; x != 123; )
scanf (―%d‖, &x);

This means that each time the loop repeats, ̳x‘ is tested to see if it equals 123, but no
further action takes place. If you type 123, at the keyboard, however the loop condition
becomes false and the loop terminates.
The initialization some times happens when the initial condition of the loop control
variable must be computed by some complex means.
Example 2:
/* Program to print the name in reverse order. */
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char s[20];
int x; clrscr
();
printf ("\nEnter your name:
"); gets (s);
x = strlen (s);
for ( ; x > 0 ; )
{
--x;
printf ("%c\t", s[x]);
}
}

Output to the screen:
Enter your name: KIRAN N A R I K

Custom Search

%d bloggers like this: