Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda The Do-While Loop in C | While Loop
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookThe Do-While Loop in C

Deslize para mostrar o menu

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt