The simplest of all the looping structures in is the while statement.
The basic format of while statement is
while (test condition)
{
body of the loop
}
include<stdio.h>
void main( )
{
int sum = 0;
int n = 1;
while (n<=10)
{
sum = sum + n*n;
n = n+1;
}
print f ("sum = %d/n" ; sum);
}
Do-while
The general form of do while loop:
initialization:
do{
statement
increment
} while (condition);
include<stdio.h>
include<conio.h>
void main( )
{
int i = 5;
do { print f ("value of i = %d ", i);
i++
} while (i<5);
print f ("/n current value of i =%d" , i);
getch ( )
}
The basic format of while statement is
while (test condition)
{
body of the loop
}
include<stdio.h>
void main( )
{
int sum = 0;
int n = 1;
while (n<=10)
{
sum = sum + n*n;
n = n+1;
}
print f ("sum = %d/n" ; sum);
}
Do-while
The general form of do while loop:
initialization:
do{
statement
increment
} while (condition);
include<stdio.h>
include<conio.h>
void main( )
{
int i = 5;
do { print f ("value of i = %d ", i);
i++
} while (i<5);
print f ("/n current value of i =%d" , i);
getch ( )
}
No comments:
Post a Comment