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 specifying the number of times or until the given condition is 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
What is do-while loop in C
do-while loop in C is an exit control loop. It executes program statements once at first then only the condition is checked. Thus, the body of the loop will execute for a single time if the condition is not satisfied at the very first attempt. If the condition is found true then it executes the program statements again, otherwise, it gets out from the loop structure.
Syntax of the do while loop
|
do { Body
of loop; Increasing/decreasing; } while(condition); |
Flow chart of do 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 do while loop first, the loop starting point is evaluated. This process is called the initialization of 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.
Ø After evaluating the starting point the body of do while loop will be executed.
Ø Finished executing of the body once time, the loop checks the condition. Condition is an 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.
Ø At, first, the do while loop executes the body of the loop then it checks the condition if it is true again 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.
Example of do while loop
C program to print "C is best" 10 time using do while loop
|
#include<stdio.h> #include<conio.h> main() { int
i=1; do { printf("\n
C is best"); i++; } while(i<=10); 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 the body of loop printf("\n C is best"), will be executed and second-time i++ increase the value of i 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 the condition as 3<=10, and the body of loop printf("\n C is best"); is executed. 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
Write a C program to print 1 to n using do while loop
|
#include<stdio.h> #include<conio.h> void main() { int
i=1, n; printf("\n
Enter the value of n:"); scanf("%d",&n); do { printf("%d\t",i); i++; } while(i<=n); getch(); } |
Output
Also, check
for loop
while loop



0 Comments
Please do not enter any spam link on comment box