Thursday 16 February 2017

The do_while Statement

-It is exit-controlled loop statement.
-do-while loop will execute at least one time even if the condition is false initially.
-The do-while loop execute until the condition beccomes false.

Syantax:

do
{
       Body of the loop
}
while(test-condition);

Program :


#include<stdio.h>
#include<conio.h>

void main()
{
int sum=1,index=0;
do
{
index+=1;
sum=2*sum;
}
while(index>9);
printf("%d%d",sum,index);
getch();
}

Output :
2 1

while loop :

-It is an entry controlled loop statement.
-The test condition is evaluated and if the condition is true,then the body of the loop is executed.
-The execution process is repeated until the test condition becomes false and the control is transferd out of the loop.
-On exit,the program continue with the statement immediately after the body of the loop.
-This process continue until the test condition becomes false and the control is transferred out of the loop.

Syantax : 
while( test expression )
{
       Body of the loop
}
statement -x;


Program :

#include<stdio.h>
#include<conio.h>

void main();
{
int no=1,sum=0;
while(no<=100)
{
sum=sum+no;
no=no+1;
}
printf("\n sum of no's between 1 and 100:%d",sum);
getch();
}

Output :
sum of no's between 1 and 100 is:5050

Nested "for" loop :

-one for statement within another for statement is allowed in C.
-In nested for loops one or more for statement are included in the body of the loop.

Syantax :

for(initialize;test condition;updations)
{
        for(initialize;test condition;updation)
{
Body of loop;
}
}

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
   int i,j;
   for(i=1;i<=5;i++)
{
             for(j=1;j<=10;j++)
          {
             printf("\n Computer");
           }
}
getch();
}

Output:
50 times
Computer

For Loop

-The for loop is 3 actions.
(i)   Initialization Part :
(ii)  Test Conditional Part :
(iii) Increment Part :

-The Expression are separted by semi-Colons (;).

Syantax:

for( initialization expressions;test conditions;increment )
{
       Statement - 1;
       Statement - 2;
}



for loop Flowchart :























(i) The initialization sets a loop to an initial value.This statement is executed only once.

(ii) The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop.

(iii) The updations ( increment or decrement operations ) decides how to make changes in the loop.