Loop : while Loop

 Loop Control Statements

          It is the block of statements which executes repeatedly till the condition is true is known as loop.

Loops are divided I two types.

1)    Entry control loop

It is loop where to check the condition before starting the execution of

the loop is called entry control loop.

Example: - while, for loop

2)    Exit control loop

It is loop where to check the condition at end of

the loop is called exit control loop.

Example: - do-while loop

v while loop:

It is use to execute the block of statement repeatedly till the condition is True.

Syntax:

while(Condition)

            {          

                        Statement 1;

                        Statement 2;

                        .

                        .                                           Block of statements

                        .

                        .

                        Statement n;

                        Increment/Decrement loop counter;

             }

                        Statement x;

                        Statement y;

                       

Flowchart :



 

§  It is entry control loop which check the condition before starting the execution of loop.

§  It checks condition, if it is True then the body of loop get execute.

§  After execution of the body of loop, test condition again check. If condition is true the body of while loop executed once again.

§  This repeated execution of the body of loop continues until the test condition does not False.

§  When condition is False the control will transfer out of loop and then execute statement x, statement y…..

 

 

Program: Write a program to print number from 1 to 5.

            

/* program to print number from 1 to 5 */

#include <stdio.h>

#include <conio.h>

 

void main()

{

                int i=1;

                clrscr();

                while(i<=5)

                {

                        printf("\n %d",i);

                        i++;

                }

 

 

                getch();

}

 

Output:



Program: Write a Program to find factorial of given number.

 

/* Prog no-9 Program to find factorial of given number */

 

#include <stdio.h>

#include <conio.h>

 void main()

{

                int i,fact,num;

                clrscr();

                printf("\n\nEnter numbers : " );

                scanf("%d",&num);

 

                fact = 1;

                i = 1;

                while(i<=num)

                {

                        fact = fact * i;

                        i++;

                }

                printf("\nFactorial of %d = %d",num,fact);

                getch();

}

 

Output:

 


Program: Write a Program to find sum of first 10 natural numbers.

 

/* Prog no-10: Program to find sum of first 10 natural numbers */

 

#include <stdio.h>

#include <conio.h>

 

void main()

{

                int i,sum;

 

                clrscr();

                i=0;

                sum=0;

                while(i<=9)

                {

                        sum = sum + i;

                        i++;

                }

 

                printf("\nThe Sum of first 10 natural number = %d",sum);

 

                getch();

}

 

Output:



 

Post a Comment

0 Comments