Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Do While Loop | While Loop
course content

Зміст курсу

C++ Loops

Do While LoopDo While Loop

There is another loop in C++ called do while loop and as other loops it allows to get rid of code repetition. Understanding when to use each type of loop is essential in writing efficient and correct programs. But to do so at first we should know the difference between them.

  • While loop: checks the condition before running the loop. If the condition is false initially, the loop will not run at all;
  • Do-While loop: first runs the code inside it and then checks the condition. It guarantees that the code runs at least once, even if the condition is false initially.
cpp

main.cpp

Note

Even though the condition is false, code inside the loop still executes, but only once.

A while loop can accomplish everything a do-while loop can, and if you need to ensure that a piece of code executes at least once, you can achieve that by duplicating it before the while loop. However, utilizing a do-while loop is typically a more straightforward and convenient approach in such cases.

Click👇

What happens if the condition in a do-while loop is never met?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 3
course content

Зміст курсу

C++ Loops

Do While LoopDo While Loop

There is another loop in C++ called do while loop and as other loops it allows to get rid of code repetition. Understanding when to use each type of loop is essential in writing efficient and correct programs. But to do so at first we should know the difference between them.

  • While loop: checks the condition before running the loop. If the condition is false initially, the loop will not run at all;
  • Do-While loop: first runs the code inside it and then checks the condition. It guarantees that the code runs at least once, even if the condition is false initially.
cpp

main.cpp

Note

Even though the condition is false, code inside the loop still executes, but only once.

A while loop can accomplish everything a do-while loop can, and if you need to ensure that a piece of code executes at least once, you can achieve that by duplicating it before the while loop. However, utilizing a do-while loop is typically a more straightforward and convenient approach in such cases.

Click👇

What happens if the condition in a do-while loop is never met?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 3
some-alt