Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EX.NO.

2
ITERATION STATEMENTS

2.a) FOR STATEMENT TO PRINT TEXT

AIM : To write a C program for printing the text using FOR


statement.
ALGORITHM :
Step 1 : Start the program .
Step 2 : Declare variable a.
Step 3 : Intialize the value 0 to a.
Step 4 : for a < 5 go to step 5.
Step 5 : print the text “GRD”
Step 6 : End the program.

SYNTAX :
for ( init; condition; increment )
{
statement(s);
}

PROGRAM
#include <stdio.h>
void main()
{
int a = 0;
for ( a=0; a<5; a++)
{
printf(“GRD\n”);
}
getch();
}

OUTPUT
GRD
GRD
GRD
GRD
GRD
RESULT : Thus the program completed successfully.

2. b) WHILE STATEMENT TO PRINT TEXT

AIM : To write a C program for printing the text using WHILE


statement.
ALGORITHM :
Step 1 : Start the program .
Step 2 : Declare variable a.
Step 3 : Initialize the value 0 to a.
Step 4 : while a < 4 go to step 5.
Step 5 : print the text “GRDIAN”
Step 6 : End the program

SYNTAX :
while(condition)
{
statement(s);
}

PROGRAM :
#include <stdio.h>
void main()
{
int a = 0;
while (a<4)
{
printf(“GRDIAN\n”);
a++;
}
getch();
}

OUTPUT
GRDIAN
GRDIAN
GRDIAN
GRDIAN

RESULT : Thus the program completed successfully.

2. c) DO…..WHILE STATEMENT TO PRINT TEXT

AIM : To write a C program for printing the text using


DO…WHILE statement.
ALGORITHM :
Step 1 : Start the program .
Step 2 : Declare variable j.
Step 3 : Initialize the value 0 to j.
Step 4 : print the initial value of j.
Step 5 : while j <= 3 go to step 6.
Step 6 : print the incremented value of j.
Step 7 : End the program

SYNTAX :
do
{
//statements;
} while(condition test);

PROGRAM :

#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}

Output:

Value of variable j is: 0


Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3

RESULT : Thus the program completed successfully.

You might also like