While statement ::
Syntax :: (1) while ( condition )
statement ;
(2) while ( condition )
{
statement 1 :
"
"
}
For loop statement
Syntax :: for ( variable initialization ; condition ; variable increment or decrement )
{
statement 1 ;
"
"
"
}
Here as from the syntax property both of them having different syntax property. In case of while loop statement only condition of program is written with while loop statement and variable initialization is done outside the while loop statement and the variable increment or decrement is done inside loop of condition.
In case of for loop statement all of them is written within the statement. The working is similar to the while loop statement. First of all variable initialization is done and then conditional statement is execute then the variable increment or decrement is execute.
Program to difference while and for loop statement ::
Using for loop statement ::
#include <stdio.h>
void main ()
{
int n , a , i = 0 ;
clrscr () ;
printf ("\n Enter how many terms you want to print:") ;
scanf ("%d", &n) ;
for ( a = 2 ; i <= n : i++ )
printf ("\n %d", a) ;
a = a + 2 ;
getch () ;
}
Output :: Enter how many terms you want to print
Enter number = 10
Even number are = 2 , 4 , 6 , 8
Enter number = 10
Even number are = 2 , 4 , 6 , 8
Using while loop statement ::
#include <stdio.h>
void main ()
{
int n , a = 2 , i = 0 ;
clrscr () ;
printf ("\n Enter how many terms you want to print:") ;
scanf ("%d", &n) ;
while ( i < n )
{
printf ("\n %d", a) ;
a = a + 2 ;
i++ ;
}
getch () ;
}
Output :: Enter how many terms you want to print
Enter number = 10
Even number are = 2 , 4 , 6 , 8
No comments:
Post a Comment