While Loop in C Language

                     While Loop in C Language




while loop in C:

while loop in C language executes a statement or group of statements until condition satisfies.


Syntax of while loop:

while(test condition)

{

statement(s);

}


Working of while loop:

The while keyword followed by a pair of parentheses where the test condition is written.  The statement written in a while loop is evaluated again and again until the test condition satisfies. The process goes until the condition is true. Once the test condition becomes false the loop terminates or ends. 


For example,

i=1;

while(i<=10)

{

printf(“%d”,i);

i=i++;

}


In this example, the value of i will be displayed on screen starting from 1 and incremented by one in each iteration of the loop. This process will continue until the value of ith  does not become 10. In the last iteration the value of i will become 11 and the test condition becomes false  and hence iteration will stop or terminate.


Flowchart of while loop Click here👉👉👉




C program to display first 10 natural numbers using while loop


/*****************************************************

 C program to display first 10 natural numbers using while loop

 ***************************************************************/

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

     int i;

    clrscr();

    i=1;

    printf("First 10 natural numbers are\n");

    while(i<=10)

    {

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

        i++;

    }

    printf("End of program");

    getch();

 }


First 10 natural numbers are

1

2

3

4

5

6

7

8

9

10

End of program



The value of i is initialised by 1. In each iteration the value of i is checked with test condition and value of i is incremented by one. Once the condition becomes false the iteration will stop and will get the first 10 natural numbers. Don't forget to to increase the value of i.


टिप्पणी पोस्ट करा

0 टिप्पण्या