CKTIS / C
loops and iteration — concept C23
`for`, `while`, and `do` loops repeat a statement while a scalar condition is true; `break` exits the nearest loop and `continue` advances to its next iteration step. Bounds must prevent array overrun and counter overflow, and the condition must ensure progress even for empty input.
Version: C23
Syntax
loops and iterationfor, while, and do loops repeat a statement while a scalar condition is true; break exits the nearest loop and continue advances to its next iteration step. Bounds must prevent array overrun and counter overflow, and the condition must ensure progress even for empty input.
To traverse an array, use size_t i = 0; i < length; ++i when length is trusted and describes that same object. If the loop can fail, return a status and retain the progress index instead of hiding a partial result.
The classic reverse loop for (size_t i = length - 1; i >= 0; --i) is wrong for an unsigned type and an empty array. Use for (size_t i = length; i-- > 0;); also check collection mutation inside the body and whether continue still performs the required progress step.
Examples
#include <stdio.h>
int main(void) { int sum = 0; for (int i = 1; i <= 4; ++i) sum += i; printf("%d\n", sum); return 0; }
Result
10
`for`, `while`, and `do` loops repeat a statement while a scalar condition is true; `break` exits the nearest loop and `continue` advances to its next iteration step. Bounds must prevent array overrun and counter overflow, and the condition must ensure progress even for empty input.