Page 1
ITERATIVE STATEMENTS
w h ile statement
The while statement is used when the program needs to perform repetitive tasks. The
general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:
main( )
{ int p, n, count;
float r, si;
count = 1;
while ( count < = 3 )
{
printf ( "\nEnter values o f p, n and r ") ;
scanf(“%d %d %f', &p, &n, & r) ;
si=p * n * r /100 ;
printf ( "Simple interest = Rs. % f, si) ;
count = count+1;
}
}
Some outputs of this program:
Enter values o f p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values o f p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values o f p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
Page 2
ITERATIVE STATEMENTS
w h ile statement
The while statement is used when the program needs to perform repetitive tasks. The
general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:
main( )
{ int p, n, count;
float r, si;
count = 1;
while ( count < = 3 )
{
printf ( "\nEnter values o f p, n and r ") ;
scanf(“%d %d %f', &p, &n, & r) ;
si=p * n * r /100 ;
printf ( "Simple interest = Rs. % f, si) ;
count = count+1;
}
}
Some outputs of this program:
Enter values o f p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values o f p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values o f p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
The program executes all statements after the while 3 times. These statements form what
is called the ‘body’ of the while loop. The parentheses after the while contain a condition.
As long as this condition remains true all statements within the body of the while loop
keep getting executed repeatedly.
Consider the following program;
/* This program checks whether a given number is a palindrome or not */
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf(” Enter a number to check if it is a palindrome or not\n” );
scanf(” %d” , &n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse +temp%10;
temp = temp/10;
}
if ( n == reverse )
printf(” %d is a palindrome number. \n” , n);
else
printf(” %d is not a palindrome number. \n” , n);
return 0;
}
Output:
Enter a number to check if it is a palindrome or not
12321
12321 is a palindrome
Enter a number to check if it is a palindrome or not
12000
12000 is not a palindrome
Page 3
ITERATIVE STATEMENTS
w h ile statement
The while statement is used when the program needs to perform repetitive tasks. The
general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:
main( )
{ int p, n, count;
float r, si;
count = 1;
while ( count < = 3 )
{
printf ( "\nEnter values o f p, n and r ") ;
scanf(“%d %d %f', &p, &n, & r) ;
si=p * n * r /100 ;
printf ( "Simple interest = Rs. % f, si) ;
count = count+1;
}
}
Some outputs of this program:
Enter values o f p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values o f p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values o f p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
The program executes all statements after the while 3 times. These statements form what
is called the ‘body’ of the while loop. The parentheses after the while contain a condition.
As long as this condition remains true all statements within the body of the while loop
keep getting executed repeatedly.
Consider the following program;
/* This program checks whether a given number is a palindrome or not */
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf(” Enter a number to check if it is a palindrome or not\n” );
scanf(” %d” , &n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse +temp%10;
temp = temp/10;
}
if ( n == reverse )
printf(” %d is a palindrome number. \n” , n);
else
printf(” %d is not a palindrome number. \n” , n);
return 0;
}
Output:
Enter a number to check if it is a palindrome or not
12321
12321 is a palindrome
Enter a number to check if it is a palindrome or not
12000
12000 is not a palindrome
do-while Loop
The body of the do-while executes at least once. The do-while structure is similar to the
while loop except the relational test occurs at the bottom (rather than top) of the loop. This
ensures that the body of the loop executes at least once. The do-while tests for a positive
relational test; that is, as long as the test is True, the body of the loop continues to execute.
The format of the do-while is
do
{ block of one or more C statements; }
while (test expression)
The test expression must be enclosed within parentheses, just as it does with a while
statement.
Consider the following program
// Cprogram to add all the numbers entered by a user until user enters 0.
#include <stdio.h>
int main()
{ int sum=0,num;
do /* Codes inside the body o f do...while loops are at least executed once. */
{
printf(” Enter a number\n” );
scanf(” %d” , &num);
sum+=num;
}
while(num!=0);
printf(” sum=%d” ,sum);
return 0;
}
Output:
Enter a number
3
Enter a number
-2
Enter a number
Page 4
ITERATIVE STATEMENTS
w h ile statement
The while statement is used when the program needs to perform repetitive tasks. The
general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:
main( )
{ int p, n, count;
float r, si;
count = 1;
while ( count < = 3 )
{
printf ( "\nEnter values o f p, n and r ") ;
scanf(“%d %d %f', &p, &n, & r) ;
si=p * n * r /100 ;
printf ( "Simple interest = Rs. % f, si) ;
count = count+1;
}
}
Some outputs of this program:
Enter values o f p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values o f p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values o f p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
The program executes all statements after the while 3 times. These statements form what
is called the ‘body’ of the while loop. The parentheses after the while contain a condition.
As long as this condition remains true all statements within the body of the while loop
keep getting executed repeatedly.
Consider the following program;
/* This program checks whether a given number is a palindrome or not */
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf(” Enter a number to check if it is a palindrome or not\n” );
scanf(” %d” , &n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse +temp%10;
temp = temp/10;
}
if ( n == reverse )
printf(” %d is a palindrome number. \n” , n);
else
printf(” %d is not a palindrome number. \n” , n);
return 0;
}
Output:
Enter a number to check if it is a palindrome or not
12321
12321 is a palindrome
Enter a number to check if it is a palindrome or not
12000
12000 is not a palindrome
do-while Loop
The body of the do-while executes at least once. The do-while structure is similar to the
while loop except the relational test occurs at the bottom (rather than top) of the loop. This
ensures that the body of the loop executes at least once. The do-while tests for a positive
relational test; that is, as long as the test is True, the body of the loop continues to execute.
The format of the do-while is
do
{ block of one or more C statements; }
while (test expression)
The test expression must be enclosed within parentheses, just as it does with a while
statement.
Consider the following program
// Cprogram to add all the numbers entered by a user until user enters 0.
#include <stdio.h>
int main()
{ int sum=0,num;
do /* Codes inside the body o f do...while loops are at least executed once. */
{
printf(” Enter a number\n” );
scanf(” %d” , &num);
sum+=num;
}
while(num!=0);
printf(” sum=%d” ,sum);
return 0;
}
Output:
Enter a number
3
Enter a number
-2
Enter a number
0
sum=1
Consider the following program:
#include <stdio.h>
main()
{
int i = 10;
do
{
printf(” Hello %d\n” , i);
i = i -1;
}while ( i > 0 );
}
Output
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
Program
8. Program to count the no of digits in a number
#include <stdio.h>
int main()
{
int n,count=0;
printf(” Enter an integer: ” );
scanf(” %d” , &n);
Page 5
ITERATIVE STATEMENTS
w h ile statement
The while statement is used when the program needs to perform repetitive tasks. The
general form of a while statement is:
while ( condition)
statement ;
The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:
main( )
{ int p, n, count;
float r, si;
count = 1;
while ( count < = 3 )
{
printf ( "\nEnter values o f p, n and r ") ;
scanf(“%d %d %f', &p, &n, & r) ;
si=p * n * r /100 ;
printf ( "Simple interest = Rs. % f, si) ;
count = count+1;
}
}
Some outputs of this program:
Enter values o f p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values o f p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values o f p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
The program executes all statements after the while 3 times. These statements form what
is called the ‘body’ of the while loop. The parentheses after the while contain a condition.
As long as this condition remains true all statements within the body of the while loop
keep getting executed repeatedly.
Consider the following program;
/* This program checks whether a given number is a palindrome or not */
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf(” Enter a number to check if it is a palindrome or not\n” );
scanf(” %d” , &n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse +temp%10;
temp = temp/10;
}
if ( n == reverse )
printf(” %d is a palindrome number. \n” , n);
else
printf(” %d is not a palindrome number. \n” , n);
return 0;
}
Output:
Enter a number to check if it is a palindrome or not
12321
12321 is a palindrome
Enter a number to check if it is a palindrome or not
12000
12000 is not a palindrome
do-while Loop
The body of the do-while executes at least once. The do-while structure is similar to the
while loop except the relational test occurs at the bottom (rather than top) of the loop. This
ensures that the body of the loop executes at least once. The do-while tests for a positive
relational test; that is, as long as the test is True, the body of the loop continues to execute.
The format of the do-while is
do
{ block of one or more C statements; }
while (test expression)
The test expression must be enclosed within parentheses, just as it does with a while
statement.
Consider the following program
// Cprogram to add all the numbers entered by a user until user enters 0.
#include <stdio.h>
int main()
{ int sum=0,num;
do /* Codes inside the body o f do...while loops are at least executed once. */
{
printf(” Enter a number\n” );
scanf(” %d” , &num);
sum+=num;
}
while(num!=0);
printf(” sum=%d” ,sum);
return 0;
}
Output:
Enter a number
3
Enter a number
-2
Enter a number
0
sum=1
Consider the following program:
#include <stdio.h>
main()
{
int i = 10;
do
{
printf(” Hello %d\n” , i);
i = i -1;
}while ( i > 0 );
}
Output
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
Program
8. Program to count the no of digits in a number
#include <stdio.h>
int main()
{
int n,count=0;
printf(” Enter an integer: ” );
scanf(” %d” , &n);
do
{
n/=10; /* n=n/10 */
count++;
} while(n!=0);
printf(” Number o f digits: %d” ,count);
}
Output
Enter an integer: 34523
Number o f digits: 5
for Loop
The fo r is the most popular looping instruction. The general form of fo r statement is as
under:
fo r ( initialise counter ; test counter ; Updating counter )
{
do this;
and this;
and this;
}
The fo r allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
(c) Updating the value of loop counter either increment or decrement.
Consider the following program
int main(void)
{
int num;
printf(” n n cubed\n” );
fo r (num = 1; num < = 6; num++)
Read More