C while loop- Definition, Concept, syntax, flow chart, example

C while loop- Definition, Concept, syntax, flow chart, example

 

Before getting knowledge of while loop, we have to knowledge of what is a loop

 Loop is the process of executing the same program statement or block of program statements repeatedly for specified a number of times or until the given condition satisfied. A loop executes when the given condition is true when a condition becomes false the program control will be out of the loop.

For example

We want to display C is the best 10 times, one of to get the desired output is we type printf(“\n C is best”); 10 times.However, it increases the length of the program and consumes a lot of time. Another way to perform this is by using a loop structure. With a loop structure, we do not need to type the same program statement repeatedly. The loop structure is also known as an iteration control structure.

Let discuss what is C while loop

while loop is an entry control loop where the condition is evaluated at the beginning. It checks the condition at first; if it is found true then it executes the statements written in its body part otherwise it just gets out from the loop structure. It has used the keyword while.

Syntax of while loop:

Initialization

while(condition)

{

        Body of loop;

        Increasing/ Decreasing;

}

Flow chart of while loop

 

Flow chart of while loop

Part of Syntax

Initialization: -

             Initialization defines the loop starting point.

Condition:-

            Condition defines the loop stopping point. If the condition becomes true then the body of the loop will be executed otherwise program, the statement will be out of the loop.

 Increasing/decreasing: -

           Increasing and decreasing helps to increase or decrease the counter.

 

How does while loop work?

Ø  In while loop first, the loop starting point is evaluated. This process is called the initializationof the loop. For example the loop start with 1 then the initialization point is 1. Similarly, the loop starts with 0 the initialization point is 0.

Ø  Second step condition is evaluated. Condition is in the loop is the ending point of the loop. For example, i<=5. It means the loop is repeated until the value of i is reached 5. When the value of i becomes 6 then program control will be out of the loop.

Ø  After condition is evaluated, increasing and decreasing of the loop is defined.

Ø  At, first, the while loop checks the condition with an initial point if it is true then it executes its body. When it becomes false, program control will be out of the loop. After one time its body executes, at the second time it checks the condition with the value of the loop is increasing or decreasing given in the increasing/decreasing part. This process goes continuous until the given condition is satisfied.

 

For example of while loop
    Write a C program to print “C is best” 10 times using while loop

 #include<stdio.h>

#include<conio.h>

void main()

{

                int i=1;

                while(i<=10)

                {

                                printf("\n C is best");

                             i++

                }

                getch();

}

 Output

 


 

In the above example we evaluate initial point i=1 and give condition i<=10 because we have to print C is best 10 times.

At first, the loop is stat with initial point 1 and check condition as 1<=10 which is obviously true then the body of loop printf("\n C is best"), and second-time i++ increase the value of and check condition as 2<=10 which is also a true and execute body of loop printf("\n C is best");. Similarly, at the third time, it checks condition as 3<=10 and body of loop printf("\n C is best");is execute. Continuously, it executes until condition 10<=10 when the condition becomes 11<=10 then program control will be out of the loop and it prints “C is best” 10 times.

 Let see another example

C program to print 1 to n using while loop.

 #include<stdio.h>

#include<conio.h>

void main()

{

                int i=1, n;

                printf("\n Enter the value of n:");

                scanf("%d",&n);

                while(i<=n)

                {

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

                                i++;

                }

                getch();

}

 Output


Also, check
for loop
do while loop
Click here



Post a Comment

0 Comments