Do/While Loop
As an alternative to while
loop you can use do/while
loop. The difference is that the do/while
loop will execute the code in the block once before checking if the condition returns true.
Syntax:
do {
// The code block
} while (condition);
The example of using do/while
loop:
12345int i = 0; do { cout << i << endl; i++; } while (i < 3);
The first iteration will be firstly executed and i
becomes 1
. By the third iteration, i
becomes 3
and the condition returns false after that, and do/while
loop stops.
Pay attention, that the first iteration will be executed even if the condition is always false:
12345int i = 0; do { cout << i << endl; i++; } while (i < 0);
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Запитайте мені питання про цей предмет
Сумаризуйте цей розділ
Покажіть реальні приклади
Awesome!
Completion rate improved to 3.33
Do/While Loop
Свайпніть щоб показати меню
As an alternative to while
loop you can use do/while
loop. The difference is that the do/while
loop will execute the code in the block once before checking if the condition returns true.
Syntax:
do {
// The code block
} while (condition);
The example of using do/while
loop:
12345int i = 0; do { cout << i << endl; i++; } while (i < 3);
The first iteration will be firstly executed and i
becomes 1
. By the third iteration, i
becomes 3
and the condition returns false after that, and do/while
loop stops.
Pay attention, that the first iteration will be executed even if the condition is always false:
12345int i = 0; do { cout << i << endl; i++; } while (i < 0);
Дякуємо за ваш відгук!