- The while loops
- The do-while loops
- The for loops
- The odd loops
Definiton of loops:
->Loops is use to repeat a program so that we do not need to re-write the coding again and again.
•There are
three methods by way of which we can repeat a part of a program. They are:
–(a) Using a while statement
–(b) Using a for statement
–(c) Using a do-while statement
The while loop:
initialize loop counter ;
while ( test condition )
{
do this ;
and this ;
increment loop counter ;
}
The do-while loop:
do
{
this
;
and
this ;
and
this ;
and
this ;
}
while ( this condition is true ) ;
Do-while loop will still run even the first time is an error.
The for loops:
for ( initialize counter; test counter; increment
counter
)
{
do this ;
and this ;
}
The odd loops:
example of odd loop
#include <stdio.h>
int main(void )
{
char another ;
int num ;
do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
int main(void )
{
char another ;
int num ;
do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
thats the end of chapter 4...
No comments:
Post a Comment