Loop or Iteration Statements:
Loop control statements are used when a section of code may either be executed a fixed number of times, or while some condition is true.
Three Loop structures are:
- The while loop keeps repeating an action until an associated condition returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.
- The do while loop is similar, but the condition is checked after the loop body is executed. This ensures that the loop body is run at least once.
- The for loop is used usually where the loop will be traversed a fixed number of times.
Every loop statement must have the following three requirements:
- Declaration and initialisation of control variable
- Condition based on control variable
- Change of value of control variable within the body of the loop so that it can make loop condition at some value.
Infinite Loop: A loop with no termination condition becomes infinite since it keep running.
while Loop:
The syntax is as follows:
while (test condition)
{
body_of_the_loop;
}
/* Program to calculate factorial of given number */ #include<stdio.h> main() { int x; long int fact = 1; printf(“Enter any number to find factorial:n”); /*read the number*/ scanf(“%d”,&x);
while (x > 0) { fact = fact * x; /* factorial calculation*/ x=x-1; } printf(“Factorial is %ld”,fact); }
do…while loop
This loop structure executes the block of statements once irrespective of the condition’s evaluation. It checks the condition before 2nd execution.
General syntax of do…while
…………
…………
do
{
…………
…………
} while(cond);
…………
…………
//code prompts user for non-negative denominator and outputs dividend after division
#include<stdio.h>
void main()
{
int n,d;
float r;
printf("Enter Numerator:");
scanf("%d",&n);
do
{
printf("Enter Denominator:");
scanf("%d",&d);
}while(d==0);
r=(float)n/(float)d;
printf("Dividend of your division is %.2f",r);
}
for Loop:
We understood any kind of loop statement must have three requirements they are declaration and initialisation, condition, modification. In the case of while and do…while loops we have to take care about the above specified steps. The ‘for’ loop simplifies the syntax by including these three steps in its header.
General Syntax of ‘for’ loop
for (initialization; condition; modification)
{
………………
………………
}
//code to check whether the given number is prime or not
#include<stdio.h>
void main()
{
int n;
printf("Enter your number:");
scanf("%d",&n);
for(int i=2;i<n;i++)
{
if(n%i==0)
{
printf("%d is not prime",n);
break;
}
else
{
printf("%d is prime",n);
break;
}
}
}
Nested Loop
A loop statement exists inside another loop statement is known as nested loop. The loop contains another loop in its body is known as outer loop. Here the inner loop completes all its iterations for each iteration of its outer loop. Hence, we can say the inner loop will be executes n2 times for n iterations of outer loop. Any loop can be nested into any other type of loop. For example For loop can have while loop inside its body.
General syntax:
OuterLoop(cond)
{
InnerLoop(cond)
{
………….
…………
}
}
/* Program to print the pattern */
#include<stdio.h>
void main()
{
int i,j;
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
printf("%dt",j);
printf(“n”);
}
}