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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you give me a simple example of a do-while loop in C?
What are some common use cases for do-while loops?
How is a do-while loop different from a while loop in practice?
Fantastiskt!
Completion betyg förbättrat till 9.09
The Do-While Loop in C
Svep för att visa menyn
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.
Tack för dina kommentarer!