0% found this document useful (0 votes)
9 views12 pages

138795

The document outlines the key differences between while and do-while loops in programming. While loops check the condition before executing the loop body, potentially skipping execution, whereas do-while loops guarantee at least one execution by checking the condition after executing the body. It also provides syntax examples and use cases for both types of loops.

Uploaded by

pmanimegalai123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

138795

The document outlines the key differences between while and do-while loops in programming. While loops check the condition before executing the loop body, potentially skipping execution, whereas do-while loops guarantee at least one execution by checking the condition after executing the body. It also provides syntax examples and use cases for both types of loops.

Uploaded by

pmanimegalai123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Difference between While and Do-While Loop

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.

Must Check: Top Programming Online Courses and Certificates

Table of Content
Dif f erence between While and Do-While

What is 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.
What is a Do-While Loop?

Key dif f erences and Similarities between While and Do-While

Difference Between While and Do-While


Let's understand the key difference between while and do-while:

Execution guarantee: Do-while guarantees at least one execution


of the loop body, while a while loop may not execute at all.
Condition placement: While checks bef ore executing, do-while
checks af ter executing.

Use cases:
While is better when you might not want to execute the loop at all
based on the initial condition.

Do-while is usef ul when you always want to execute the loop at


least once.

Syntax: Do-while requires a semicolon af ter the condition, while


doesn't.
Let's go through this tabular comparison between them to understand it better:

Aspect While Loop Do-While Loop


Condition Check Beginning End
Minimum
0 1
Executions

Syntax (C-style) do { ... } while


while (condition) { ... }
(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

What is a While Loop?


Def inition: While Loop is an entry-controlled loop that evaluates the condition
before executing the body of the Loop, the body of the Loop will be executed only if
the condition is True.

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…
}

Working of While Loop


Initialization of Control Variable
Condition Check

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

Read Also: For Loop in C++

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

#include < stdio.h>

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

What is a Do-While Loop?


Def inition: A do-while Loop is an exit-controlled loop that evaluates the condition
after the execution of the body.

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)

Once the body is executed, then it checks the condition:

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

Working of Do-While Loop


Initialization of control variable
Loop Body gets executed f irst; then, the condition is evaluated

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

#include < stdio.h> // Include the standard I/O library header

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

Key Differences and Similarities between While and


Do-While Loops:
While Loop is an “Entry Controlled Loop,” whereas Do-While Loop is
an “Exit Controlled 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.
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.

Must Check: Top C Online Course and Certif ications

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.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

Related Reads

C programming Keywords: A list and Explanat ion of …


Usage
Keywo rds in C refer to a set o f reserved wo rds with predefined meanings that

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

Int roduct ion t o For Loop in C


A lo o p is o ne o f the mo st fundamental lo gical structures in co mputer
pro gramming. Defining lo o ps in co de allo ws co mputers to repeat specific tasks.
Defining the lo o p in a co mputer pro gram...re ad m o re

Malloc in C Programming: How t o Allocat e Memory…


Dynamically
This article co vers the co ncept o f dynamic memo ry allo catio n in C
pro gramming, which allo ws allo cating memo ry at runtime acco rding to the pro gram’s needs using functio ns
like mallo c(), callo c(), reallo c(), and...re ad m o re

Funct ion Prot ot ype in C


This article defines the sco pe o f functio n pro to type Impo rtance, examples o f
Functio n Pro to type and Difference between Functio n Pro to type and Functio n
Definitio n in C This article defines the sco pe o f functio n pro to type...re ad m o re

Learn t o Implement st rst r() Funct ion in C


Disco ver ho w to use the Strstr functio n in the C pro gramming language,
including its syntax, parameters, and practical applicatio ns. Explo re examples
and tips to effectively implement Strstr in yo ur co de...re ad m o re

Concat enat e St rings wit h st rcat () Funct ion in C


This article o ffers a co mprehensive explanatio n o f the syntax and
implementatio n o f the strcat() functio n, in additio n to pro viding several examples
to aid in understanding its o peratio n. By the end...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

When should I use a while loop instead of a do-while loop?

What's the main advantage of a do-while loop over a while loop?

In terms of perf ormance, is there a signif icant dif f erence between


while and do-while loops?

Can a do-while loop execute zero times?

In what scenario would you pref er to use a do-while loop over a while
loop?

Describe a real-world programming scenario where a do-while loop


would be more appropriate than 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy