do while in C
The loop that always runs once, which suits menus and retry loops.
int digits_in(int n) {
int digits = 0;
do {
digits++;
n /= 10;
} while (n != 0);
return digits;
}
int sum_until_zero(const int *values, int max) {
int total = 0;
int i = 0;
do {
total += values[i];
} while (++i < max && values[i] != 0);
return total;
}
How it works
- The condition is tested after the body, not before.
- It needs a trailing semicolon after the
while. - Even for zero the digit count comes out as one.
Keywords and builtins used here
constdigits_indointreturnsum_until_zerowhile
The run, in numbers
- Lines
- 17
- Characters to type
- 253
- Tokens
- 91
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 64 seconds.
Step 4 of 7 in Operators & flow, step 11 of 35 in Language basics.