138795
138795
Vikram Singh
Assist ant Manager - Cont ent
Updated on Sep 16, 2024 11:56 IST
The most important difference between while and do-while loops is that While loops
check the condition before executing the loop body, potentially skipping it entirely if
the condition is initially false. Do-while loops execute the loop body once before
checking the condition, guaranteeing at least one execution regardless of the
condition. Keep scrolling to understand the difference between while and do-while
loop in detail.
Table of Content
Dif f erence between While and Do-While
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
What is a Do-While Loop?
Use cases:
While is better when you might not want to execute the loop at all
based on the initial condition.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Aspect While Loop Do-While Loop
Variables are initialised Variables may initialise
Variable
bef ore the execution bef ore or within the
Initialization
of the loop. loop.
Exit Control Pre-test Post-test
Execution
No guarantee At least once
Guarantee
Required af ter
Semicolon Not required af ter }
condition
Can be within the loop
Initialization Bef ore loop
body
When you need to When you want to
Use Case check the condition ensure at least one
bef ore any execution execution
Checking if a f ile
Getting user input at
Example Scenario exists bef ore
least once
processing
Once the body of the Loop gets executed, control again goes back
to the beginning and then checks the condition.
Must Check: Top Java Online Courses and Certifications
Read Also: Loops in Java
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Syntax
while (boolean condition)
{
loop statements…
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Must Check: Top C++ Online Courses and Certifications
Example
Using while Loop, print the f irst ten positive integers.
Code in C Programming Language
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Copy code
int main() {
int number = 1; // initializing the variable with value 1
while (number < = 10) { // while loop with condition
printf("%d\n", number);
number++; // incrementing operation
}
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
The body of the Loop is always executed (at least once)
Syntax
do
{
statements….
}
while (condition);
Check out the best-rated job-centric courses after the 12th. Explore how
online degree programs can prepare you for the job market
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Example
Print the table of 5 in C Programming Language
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Copy code
int main() {
int num = 1; // Initializing the variable with value 1
do {
printf("%d\n", 5 * num);
num++; // Incrementing operation
} while (num < = 10);
return 0;
}
Output
5
10
15
20
25
30
35
40
45
50
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
Do-while Loop uses a semicolon in the syntax, whereas While Loop
does not use a semicolon.
While Loop evaluates the condition f irst and then executes the
statement, whereas Do-While f irst executes the statements and then
evaluates the condition
If the condition in a while loop is f alse, then not a single statement
inside the Loop will be executed, whereas in a do-while Loop, if the
condition is f alse, then the body of the Loop is executed at least
once.
In the While loop, the condition statement is at the beginning,
whereas the Do-While loop condition statement is at the end of the
Loop.
Conclusion
The body of a while loop is executed after the given condition is evaluated, whereas
in a do-while loop, the loop body is executed, and then the given condition is
checked. In this article, we have briefly discussed the difference between a while and
a do-while loop with examples.
Keep Learning!!
Keep Sharing!!
Related Reads
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
are used to write pro grams in the C pro gramming language. These keywo rds canno t be used as...re ad
m o re
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.
FAQs
In what scenario would you pref er to use a do-while loop over a while
loop?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 17-Sep-20 24.