

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Memory Management
incomplete
2: C Program Structure
incomplete
3: Interpreted Quiz
incomplete
4: C Is Compiled
incomplete
5: Comments
incomplete
6: Basic Types
incomplete
7: Strings
incomplete
8: Printing Variables
incomplete
9: Compilation: Types
incomplete
10: Variables
incomplete
11: Constants
incomplete
12: Functions
incomplete
13: Void
incomplete
14: Unit Tests
incomplete
15: Math Operators
incomplete
16: If Statements
incomplete
17: Logical Operators
incomplete
18: Ternary
incomplete
19: Type Sizes
incomplete
20: Sizeof
incomplete
21: For Loop
incomplete
22: While Loop
incomplete
23: Do While Loop
incomplete
24: Pragma Once and Header Guards
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A do while loop in C is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.
Unlike the while loop, the do while loop checks the condition after executing the loop body, so the loop body is always executed at least once.
do {
// Loop Body
} while (condition);
do while Looptrue, execute the body again.false, terminate the loop#include <stdio.h>
int main() {
int i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
return 0;
}
// Prints:
// i = 0
// i = 1
// i = 2
// i = 3
// i = 4
#include <stdio.h>
int main() {
int i = 100;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
return 0;
}
// Prints:
// i = 100
The do while loop guarantees that the loop body is executed at least once, even if the condition is false initially.
The most common scenario you will see a do-while loop used is in C macros – they let you define a block of code and execute it exactly once in a way that is safe across different compilers, and ensures that the variables created/referenced within the macro do not leak to the surrounding environment.
If you end up looking at any source code for macros, you will probably see a few do-while loops. For example, here's a simplified version from our munit testing library we're using:
#define munit_assert_type_full(T, fmt, a, op, b, msg) \
do { \
T munit_tmp_a_ = (a); \
T munit_tmp_b_ = (b); \
if (!(munit_tmp_a_ op munit_tmp_b_)) { \
munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix \
" %s " "%" fmt "): %s", \
#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_, msg); \
} \
} while (0)
It creates a do-while loop, creates a few new variables and then checks that the assertion is valid. If it is not, then it errors and formats a (complicated) error message (If this code doesn't make any sense, that's fine too! I just wanted to show you where they most often occur).
There is no semi-colon after while(0) in the loop above. This lets the macro be used in a block of code without causing syntax errors.
When writing a normal do-while loop in your C code (not in a macro), you must include the semicolon after the loop.
Run the code. Notice that it prints numbers from 5 to 1 in descending order. However, when the starting number is less than the ending number, it doesn't print anything because the condition of the while loop is never true. Modify the print_numbers_reverse function to use a do-while loop so that it always prints the starting number at least once, even if the condition is initially false.