Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте The Do-While Loop in C | While Loop
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Loops for Beginners

bookThe Do-While Loop in C

Note
Definition

A post-test loop, like the do-while loop, checks its condition after executing the loop body. This is different from a pre-test loop, such as the while loop, which checks the condition before running the body.

The do-while loop in C allows you to run a block of code at least once before checking the condition. Unlike the while loop, which checks the condition before entering the loop body, the do-while loop executes its block first, then checks if it should repeat.

This makes the do-while loop useful when you want to ensure a section of code runs at least once — for example, when performing an action that must happen before a condition can be verified.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> int main() { // Try to change `number` to any positive value int number = -3; // Starts with a negative `number` do { printf("Current number: %d\n", number); number++; } while (number <= 0); printf("Loop finished, final number: %d\n", number); return 0; }

The do-while loop is ideal when you need to guarantee execution before validation. In this example, the loop prints and increments the variable at least once, even though the condition (number <= 0) is checked only after the body runs.

It's perfect for tasks that require one guaranteed action before rechecking, such as repeating an operation until a valid state or condition is reached.

Note
Note

Post-test loops always run the body at least once, while pre-test loops might not run at all if the condition is false from the start.

question mark

Which situation best requires a do-while loop instead of a while loop?

Select the correct answer

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookThe Do-While Loop in C

Свайпніть щоб показати меню

Note
Definition

A post-test loop, like the do-while loop, checks its condition after executing the loop body. This is different from a pre-test loop, such as the while loop, which checks the condition before running the body.

The do-while loop in C allows you to run a block of code at least once before checking the condition. Unlike the while loop, which checks the condition before entering the loop body, the do-while loop executes its block first, then checks if it should repeat.

This makes the do-while loop useful when you want to ensure a section of code runs at least once — for example, when performing an action that must happen before a condition can be verified.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> int main() { // Try to change `number` to any positive value int number = -3; // Starts with a negative `number` do { printf("Current number: %d\n", number); number++; } while (number <= 0); printf("Loop finished, final number: %d\n", number); return 0; }

The do-while loop is ideal when you need to guarantee execution before validation. In this example, the loop prints and increments the variable at least once, even though the condition (number <= 0) is checked only after the body runs.

It's perfect for tasks that require one guaranteed action before rechecking, such as repeating an operation until a valid state or condition is reached.

Note
Note

Post-test loops always run the body at least once, while pre-test loops might not run at all if the condition is false from the start.

question mark

Which situation best requires a do-while loop instead of a while loop?

Select the correct answer

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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