The Do-While Loop in C
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
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.
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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 9.09
The Do-While Loop in C
Deslize para mostrar o menu
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
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.
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.
Obrigado pelo seu feedback!