0% found this document useful (0 votes)
57 views

Chapter 11: Iterative Statement

The document discusses different loop statements in C programming like while, do-while and for loops. It provides examples of using these loops to iterate through a code block repeatedly based on a condition. It also covers nested loops, where one loop is placed inside another to perform multiple iterations. Some key examples include using loops to calculate simple interest over multiple iterations, find digit count of a number, print patterns like numbers counting down from 10 to 1, and determine if a number is a palindrome.

Uploaded by

Map ayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Chapter 11: Iterative Statement

The document discusses different loop statements in C programming like while, do-while and for loops. It provides examples of using these loops to iterate through a code block repeatedly based on a condition. It also covers nested loops, where one loop is placed inside another to perform multiple iterations. Some key examples include using loops to calculate simple interest over multiple iterations, find digit count of a number, print patterns like numbers counting down from 10 to 1, and determine if a number is a palindrome.

Uploaded by

Map ayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 11: Iterative

Statement
while 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 of 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 of p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values of p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values of p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000
Seatwork
Do a program checks whether a given number is a palindrome or not
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.
Example:
program to add all the numbers entered by a user until user enters
0
Output:
 Enter a number 3
Enter a number
-2
Enter a number
0
sum=1
#include <stdio.h>
int main()
{ int sum=0,num;
do /* Codes inside the body of 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;
}
Program a file that print hello 10 down to Hello 1
Output 
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
#include <stdio.h> main()
{
int i = 10; do
{
printf("Hello %d\n", i ); i = i -1;
}while ( i > 0 );
}
Seatwork
Write a program e is compiled and executed, it produces the following result

• Program to count the no of digits in a number


Output
Enter an integer: 34523
Number of digits: 5
for Loop
The for is the most popular looping instruction. The general form of for statement is as
under: 
for ( initialise counter ; test counter ; Updating counter )
{
do this; and this; and this;
}
The for allows us to specify three things about a loop in a single line:
Setting a loop counter to an initial value.
Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
Updating the value of loop counter either increment or decrement. 
Example
The program prints the integers 1 through 6 and their
cubes.
n n cubed

1 1

2 8

3 27

4 64

5 125

6 216
int main(void)
{
int num;
printf(" n n cubed\n");
for (num = 1; num <= 6; num++)

printf("%5d %5d\n", num, num*num*num); return 0;


}
Seatwork
• Program to find the reverse of a number
Output:
 
Enter any number: 123
Reversed of number: 321
NESTING OF LOOPS
C programming language allows using one loop inside another loop. Following section
shows few examples to illustrate the concept.  
Syntax:
The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment)
{
statement(s);
}
statement(s);
}
The syntax for a nested while loop statement in C programming language is as
follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);

• The syntax for a nested do...while loop statement in C programming language is as
follows:
• 
• do
•{
• statement(s); do
•{
• statement(s);
• }while( condition );

}while( condition );
Seatwork
program using a nested for loop to find the prime numbers from 2 to 20:
Output
 
1 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
Seatwork
*
***
*****
*******
*********
Seatwork
• Program to print series from 10 to 1 using nested loops.
Output: 
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
End of Module 1

You might also like