C/C++ Arena

Step 5 of 6

do-while

A do { } while (cond); loop checks its condition at the end, so the body always runs at least once. It's perfect for "ask until valid" input and for menus.

int x;
do {
    scanf("%d", &x);
} while (x < 1 || x > 5);

Note the semicolon after while (...).

Your turn: read numbers until one is between 1 and 5 (inclusive). For every invalid number print invalid N. Then print chose N.

Previous: break and continue Next: Nested loops