[go: up one dir, main page]

0% found this document useful (0 votes)
132 views14 pages

Iterative Statements

notes

Uploaded by

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

Iterative Statements

notes

Uploaded by

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

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.

C language supports three types of iterative statements, They are:

 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.

The general form or syntax and Flowchart of the while statement is :

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.

Properties of while 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.

Example of the while loop in C language:

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

2. Write a program to read a number and find sum of digits of a numbers.


#include <stdio.h>
int main()
{
int n,i=0,sum=0;
printf("Ënter the value of n:\n");
scanf("%d",&n);
while ( i<= n )
{
sum = sum + i;
i++;
}

printf("The sum is %d",sum);


return 0;
}

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 :

Enter the value of n156


Reverse = 651

4. Write a program to read a number and check whether it is palindrome or not.


#include <stdio.h>
void main()
{
int n,rev=0,temp,org;
printf("Enter the value of n:");
scanf("%d",&n);
org=n;
while ( n > 0 )
{
temp = n%10;
rev = rev * 10 + temp;
n = n/10;
}
if( org == rev)
{
printf("Palindrome");
}
else
{
printf("Not Palindrome");
}
}

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.

The syntax and flow chart of the do...while loop is:

do-while loop is an exit controlled loop and checks its condition at the end of the loop.

How do...while loop works?

 The body of do...while loop is executed once. ...


 If test Expression is true, the body of the loop is executed again and test Expression is evaluated
once more.
 This process goes on until test Expression becomes false.
 If test Expression is false, the loop ends.
Example of the do while loop in C language:

1. Write a program to find sum of first n natural numbers using do..while


#include <stdio.h>
void main()
{
int n,i,sum=0;
printf("Enter the value of n");
scanf("%d",&n);
i=1;
do
{
sum = sum + i;
i++;
}while(i<=n);
printf("Sum = %d",sum);
}

OUTPUT :

Enter the value of n 9


9
Sum = 45

2. Print numbers from 1 to 5


#include <stdio.h>
int main() {
int i = 1;
do
{
printf("%d ", i);
++i;
}while (i <= 5) ;

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.

Basic syntax of for loop is as follows:

for( expression1; expression2; expression3)


{
Single statement or Block of statements;
}

In the above syntax:

 expression1 - Initializes variables.


 expression2 - Conditional expression, as long as this condition is true, loop will keep
executing.
 expression3 - expression3 is the modifier which may be simple increment of a variable.
How for loop works?

 The initialization statement is executed only once.

 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.

Example of the for loop in C language:

1. Write a program to find sum of first n natural numbers?


#include<stdio.h>
void main()
{
int n,i,sum=0;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf("Sum = %d",sum);
}
OUTPUT :
Enter the value of n: 10
Sum = 55

2. Write a program to find sum of first n natural numbers?


#include<stdio.h>
void main()
{
int n,i,fact=1;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = fact * i;
}
printf("Factorial = %d",fact);
}

OUTPUT :
Enter the value of n:5
Factorial = 120

*For more program refer textbook*


Nested loops
It is possible to have a loop inside another loop. Any loop can be enclosed inside any
other loop. The enclosing loop is called outer loop and the other loop is called inner loop.

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.

Example of the nested loop in C language:

1. Write a c program to print the following pattern

*****
*****
*****
*****
*****

#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;
}

2. Write a c program to print the following pattern

*
**
***
****
*****
******
#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;
}

3. Write a c program to print the following pattern

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;
}

4. Write a c program to print the following pattern


*****
****
***
**
*
#include <stdio.h>
int main() {
int size = 5;
for (int i = 0; i < size; i++) {

for (int j = 0; j < i; j++) {


printf(" ");
}

for (int j = size; j > i; j--) {


printf("*");
}
printf("\n");
}
return 0;
}
5. Write a C Program to display the following by reading the number of rows as input

#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*

You might also like