Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between while and do while loop

Difference Between while and do while loop

February 26, 2018 Posted by Lithmee

Key Difference – while vs do while loop
 

In programming, there can be situations that are necessary to execute a block of statements again and again. Most programming languages support different control structures such as for loop, while loop and do while loop to repeat a code. The loops allow executing a set of statements multiple times until the given condition becomes false. The statements belong to the loop are included inside a pair of curly braces. This article discusses the difference between two control structures: while loop and do while loop. The while loop is used to repeat a statement or a group of statements while a given condition is true. It checks the condition before executing the statements inside the loop. The do while loop is similar to the while loop. But the condition is checked at the end of the execution of the statements inside the loop. The key difference between while loop and do while loop is that, while loop checks the condition before executing the statements inside the loop while do while loop checks the condition after executing the statements inside the loop.

CONTENTS

1. Overview and Key Difference
2. What is while loop
3. What is do while loop
4. Similarities Between while and do while loop
5. Side by Side Comparison – while vs do while loop in Tabular Form
6. Summary

What is while loop?

The while loop executes the target statement or statements until the given condition is true. First, the while loop verifies whether the condition is true or not. If the condition is true, it iterates the loop till the condition is true. When the condition is false, the control is passed to the next line of code immediately after the loop. The while loop can contain one statement or multiple statements. Refer the below program.

Difference Between while and do while loop

Figure 01: while loop example

According to the above program, the variable x is initialized to 1. The statements of the while loop will execute until the x value is less than or equal to 5. Initially, the value is 1 and the condition is true. Therefore, x will print. Then the x value is incremented by 1. Now the x value is 2. It is less than or equal to 5. So, x will print. Again, the x value is incremented by 1. Now x is 3. It is less than or equal to 5. So, x will print again and it is incremented by one. Now x is 4. It is also less than or equal to 5. So, x will print.The value of x is incremented again. In the next iteration, the x value becomes 5. It is equal to 5. Still, the condition is true. Therefore, x will print. The x value is incremented again. It is 6. But now the condition is false because 6 is greater than 5. Execution of the loop terminates. If there is no increment in the program, the x value will always be 1. The condition will always be true because it is less than 5. Therefore, it will be an infinite loop.

What is do while loop?

The do while loop is similar to the while loop. But the condition is checked after the execution of the loop statements. Therefore, whether the condition is true or false, the loop will execute at least one time. The condition is checked after the loop execution. If the condition is true, the loop statements will execute again. This process repeats until the condition is false. Refer the below program.

Key Difference Between while and do while loop

Figure 02: do while loop example

According to the above program, the variable x is initialized to 1. The loop executes and prints the x value. Then the x value is incremented by 1. Now it is 2. The condition is true, so the loop executes. The x is printed and incremented. Now it is 3. The condition is true, so the loop executes. The x is printed and incremented again. Now it is 4. The condition is true. The loop executes. The x is printed and incremented. Now x is 5. Still, the condition is true because it is less than or equal to 5. So, the loop executes again and prints the x value. Then x is incremented by 1. Now x is 6. The condition is false. The loop execution terminates.

Assume that the x is initialized to 10 at the beginning. Still, the loop will execute and print x value because the condition is tested at the end of the loop. When checking the condition, it is false. Therefore, the loop execution terminates. Even the condition is true or false, the do while loop will at least execute once. That is the process of do while loop.

What is the Similarity Between while and do while loop?

  • Both are control structures in programming.

What is the Difference Between while and do while loop?

while vs do while loop

The while loop is a control structure that allows code to be executed repeatedly based on a given Boolean condition. The do while loop is a control structure that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block.
 Condition Statement
The condition statement of the while loop is at the beginning of the loop. The condition statement of the do while loop is at the end of the loop.
Execution
The while loop will execute only if the condition is true. The do while can execute at least once, even though the condition is false.

Summary – while vs do while loop

In programming, sometimes it is necessary to execute a set of statement again and again. Control structures are used for that. Two of them are while and do while loop. This article discussed the difference between while loop and do while loop. The while loop is used to repeat a statement or a group of statements while a given condition is true. In do while loop, the condition is checked at the end of the execution of the statements inside the loop. The do while loop is similar to while loop but do while loop can execute at least once even though the condition is true or false. The difference between while loop and do while loop is that, while loop checks the condition before executing the statements inside the loop while do while loop checks the condition after executing the statements inside the loop.

Reference:

1.“While loop.” Wikipedia, Wikimedia Foundation, 17 Feb. 2018. Available here
2.“Do while loop.” Wikipedia, Wikimedia Foundation, 17 Feb. 2018. Available here
3.Point, Tutorials. “C Loops.” Tutorials Point, 8 Jan. 2018. Available here

Related posts:

Difference Between Awhile and WhileDifference Between Awhile and While Difference Between During and WhileDifference Between During and While Difference Between When and WhileDifference Between When and While Difference Between Binary Search and Linear Search Difference Between JDO and Value Object

Filed Under: Programming Tagged With: Compare while and do while loop, do while loop, do while loop Definition, do while loop Execution, do while loop Function, do while loop Statement, While, while and do while loop Differences. while and do while loop Similarities, While Definition, while Execution, while Function, while Statement, while vs do while loop

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Gynecologist and Obstetrician

Difference Between Trophoblast and Inner Cell Mass

Difference Between Trophoblast and Inner Cell Mass

What is the Difference Between Canthaxanthin and Astaxanthin

What is the Difference Between Canthaxanthin and Astaxanthin

Difference Between HTC Sensation and Sensation XE

Difference Between LLDPE and Metallocene LLDPE

Difference Between LLDPE and Metallocene LLDPE

Latest Posts

  • What is the Difference Between Penta and Hexa Vaccine
  • What is the Difference Between Vital and Supravital Staining
  • What is the Difference Between Post Transcriptional and Post Translational Modification
  • What is the Difference Between Gingelly Oil and Sesame Oil
  • What is the Difference Between Patent Ductus Arteriosus and Truncus Arteriosus
  • What is the Difference Between Lagophthalmos and Ptosis
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.