CS U111 · Lab 4 · drill
Predict the output
Six small loops per sheet, freshly generated each time. Write exactly what each one prints, then reveal: the real output, a pass-by-pass trace, and the one rule the loop was testing. This is the skill the sheet's dry runs and debugging exercises actually examine — reading a loop, not writing one. Six problems is about fifteen minutes; do a sheet a day for a week.
For each program, trace on paper first — a two-column table of the loop variable and what gets printed — then type the output into the box exactly, spaces included. Write ⏎ where a new line starts if a program prints several lines (or just press Enter in the box). Reveal marks each answer, shows the trace, and names the rule. A wrong answer with a right trace is a typing slip; a wrong trace is the thing to reread in the lesson.
A. The loop runs while the condition is true; < and <= differ by exactly one pass, and the step decides which values are visited. B. break leaves the loop; continue skips the rest of this pass — and the order of the if and the printf decides whether the current value prints. C. An accumulator starts at 0 (or 1 for products) and is only as good as its initialisation. D. Inner loop finishes completely for every outer pass; the newline or separator is printed where it sits. E. % 10 is the last digit, /= 10 drops it; the loop ends when the number is 0; leading zeros vanish. F. while tests first and can run zero times; do-while runs the body once before its first test.