When to Use While and Do While?

When to Use While and Do While?

Simple answer is while loop will execute only if condition inside of while statement is true. do while loop will execute once regardless of the while statement condition.

When would you use do while?

If the condition is false, the control breaks out of the loop. This means that the statements inside the loop are executed before the condition is tested. So the do while loop should be used in all scenarios where the loop body needs to be executed at least once.

What is difference between while and do while?

The difference lies in the place where the condition is tested. The while loop tests the condition before executing any of the statements within the while loop whereas the do-while loop tests the condition after the statements have been executed within the loop.

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.