Iterative Statements
Iterative Statements
In c, iterative statements (also called loops) are allows a set of instructions to be executed
repeatedly. Each repetition of the set of statements is known as a pass or iteration.
While loop
Do while loop
For loop
while loop
The while statement is used to carry out looping operations, in which a group of
statements is executed repeatedly, as long as some condition is satisfied.
Statement x;
while(condition)
{
Statement block;
}
Statement y;
Since while loops check the test condition at the beginning of the loop, it is also known as entry
controlled loop.
A conditional expression is used to check the condition. The statements defined inside the
while loop will repeatedly execute until the given condition fails.
The condition will be true if it returns 0. The condition will be false if it returns any non-
zero number.
In while loop, the condition expression is compulsory.
Running a while loop without a body is possible.
We can have more than one conditional expression in while loop.
If the loop body contains only one statement, then the braces are optional.
1. Suppose we want to display the consecutive digits 0, 1, 2,. . . ,9,10 with one digit on each
line. This can be accomplished with the following program.
#include<stdio.h>
main()
{
int digit = 0;
while(digit <= 10)
{
printf("%d\n", digit);
digit++;
}
}
OUTPUT : 0 1 2 3 4 5 6 7 8 9 10
OUTPUT :
Ënter the value of n:
5
The sum is 15
3. Write a program to read a number and find reverse of a number.
#include <stdio.h>
void main()
{
int n,rev=0,temp;
printf("Enter the value of n");
scanf("%d",&n);
while ( n > 0 )
{
temp = n%10;
rev = rev * 10 + temp;
n = n/10;
}
printf("Reverse = %d",rev);
}
OUTPUT :
OUTPUT :
Enter the value of n:156
Not Palindrome
Enter the value of n:121
Palindrome
*For more program refer textbook*
do-while loop
The do..while loop is similar to the while loop with one important difference. The body
of do...while loop is executed at least once. Only then, the test expression is evaluated.
do-while loop is an exit controlled loop and checks its condition at the end of the loop.
OUTPUT :
return 0;
}
OUTPUT :
12345
3. Program to add numbers until the user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
OUTPUT :
Enter a number: 5
Enter a number: 2
Enter a number: 3
Enter a number: -1
Enter a number: 0
Sum = 9.00
*For more program refer textbook*
for loop
The for loop in C language is used to iterate the statements or a part of the program several
times. It is frequently used to traverse the data structures like the array and linked list.
Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is
terminated.
However, if the test expression is evaluated to true, statements inside the body of the for loop are
executed, and the update expression is updated.
Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop
terminates.
OUTPUT :
Enter the value of n:5
Factorial = 120
Although all kinds of loops can be nested, the most common nested loop involves for loops.
These loops are particularly useful when displaying multidimensional data. When using these
loops, the first iteration of the first loop will initialize, followed by the second loop.
*****
*****
*****
*****
*****
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
*
**
***
****
*****
******
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
1
12
123
1234
12345
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
#include<stdio.h>
int main( )
{
int i,j,n,k,m;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
{
printf(" ");
}
/* Display number in ascending order upto middle*/
for(k=1;k<=i;k++)
{
printf("%d",k);
}
/* Display number in reverse order after middle */
for(m=i-1;m>=1;m--)
{
printf("%d",m);
}
printf("\n");
}
return 0;
}
*For more program refer textbook*