While loop statement:
The while loop statement is considered one of the simplest and most commonly utilized looping structures in the C programming language.
In this statement, while it is a reserved word or keyword, the condition can be any constant, variable, expression, and statement can be a single statement or a group of statements. In the while loop, we have to perform three steps:
- Initialization
- Test Condition
- Increment and decrement
Setting Initial Value: Within a while loop, the initial step involves setting the starting value of the loop counter. This counter can function as either an increment or decrement counter within the while loop.
For Example: i = 1;
After the setup phase, the subsequent step within the while loop involves evaluating the test condition to determine if the loop should proceed. When the condition evaluates to true, the loop's block of code is executed; if the condition is false, the loop does not execute.
For Example: while (i<=10)
After evaluating the condition in the while loop, the loop counter is adjusted using increment and decrement operations to modify its value accordingly.
For Example: i = i + 1;
Syntax of while loop:
Initialization;
while (condition)
{
Block of statements;
}
Statement-x;
In a while loop construct, the while keyword is employed, followed by a condition that needs to be evaluated within parentheses. This condition can encompass various logical operators. Following the condition, a set of curly brackets delineates the block of statements to be executed as long as the condition remains true.
Example:
#include <stdio.h>
#include <conio.h>
Void main ()
{
int i;
clrscr();
i = 1;
while (i<=10)
{
print ("hello");
i = i + 1;
}
getch();
}
Flowchart of while loop statement:
do-while loop statement:
In the C programming language, the do-while loop statement is also similar to a while loop in the C programming language. In this, first of all, the loop's body is executed then the condition is checked. In the do-while loop, we have to perform three steps:
- Initialization
- Test Condition
- Increment and decrement
Setting initial value: Within a do-while loop, the initial phase involves setting the starting value of the loop counter. This counter can function as either an increment or decrement counter.
For Example: i = 1;
After completing the initialization step, the subsequent action in the do-while loop involves evaluating the test condition to determine if the loop should continue executing.
For Example: while (i<=10);
After verifying the condition in the do-while loop, the subsequent action involves adjusting the loop counter by either increasing or decreasing its value.
For Example: i = i + 1;
Syntax of do-while loop:
do
{
Statements;
}
while (condition);
Statement-x;
Here, the do-while loop statement utilizes two keywords, namely do and while. When the condition is true within the do-while loop, the statement is executed again. Conversely, when the condition evaluates to false, the execution halts, and control moves to the subsequent statement.
Example:
#include <stdio.h>
#include <conio.h>
Void main ()
{
int i;
clrscr();
i = 1;
do
{
printf("hello");
i = i + 1;
}
while(i<=10);
getch();
}
Flowchart of do-while loop statement:
while loop vs do-while loop in C
Let's explore a Comparison between the while loop and do-while loop in the C programming language.
| SR.NO | while loop | do-while loop |
|---|---|---|
1. |
While the loop is an entry control loop because firstly, the condition is checked, then the loop's body is executed. | The do-while loop is an exit control loop because in this, first of all, the body of the loop is executed then the condition is checked true or false. |
2. |
The statement of while loop may not be executed at all. | The statement of the do-while loop must be executed at least once. |
3. |
The while loop terminates when the condition becomes false. | As long as the condition is true, the compiler keeps executing the loop in the do-while loop. |
4. |
In a while loop, the test condition variable must be initialized first to check the test condition in the loop. | In a do-while loop, the variable of test condition Initialized in the loop also. |
5. |
In a while loop, at the end of the condition, there is no semicolon.Syntax:while (condition) | In this, at the end of the condition, there is a semicolon.Syntax:while (condition); |
6. |
While loop is not used for creating menu-driven programs. | It is mostly used for creating menu-driven programs because at least one time; the loop is executed whether the condition is true or false. |
7. |
In a while loop, the number of executions depends on the condition defined in the while block. | In a do-while loop, irrespective of the condition mentioned, a minimum of 1 execution occurs. |
{
Block of statements;
}
Statement-x; | Syntax of do-while loop:do
{
statements;
}
while (condition);
Statement-x; |
| 9. | Program of while loop:Program of while loop:
The given code snippet demonstrates the usage of a do-while loop in programming:
include#includeVoid main
{
int i;
clrscr;
i = 1;
do
{
printf("hello");
i = i + 1;
}
while(i<=10);
getch;
}
while (condition);
while (condition)
{
Block of statements;
}
Statement-x;
do
{
statements;
}
while (condition);
Statement-x;
Program of while loop:
#include#includeVoid main()
{
int i;
clrscr();
i = 1;
while(i<=10)
{
printf("hello");
i = i + 1;
}
getch();
}
#include#includeVoid main()
{
int i;
clrscr();
i = 1;
do
{
printf("hello");
i = i + 1;
}
while(i<=10);
getch();
}