高階ICT-ALGO-016

How many times does the body of FOR i FROM 1 TO 5 run, and what is n after n ← 0 followed by n ← n + 1 inside that loop?

A. The body runs 4 times and n is 4.
B. The body runs 5 times and n is 5.✓ 答案
C. The body runs 6 times and n is 6.
D. The body runs once and n is 1.

解說

FROM 1 TO 5 includes both ends, so i is 1, 2, 3, 4 and 5. That is five additions, and n finishes at 5.

「演算法與偽代碼」全部 16 題 · 資訊及通訊科技 MCQ 題庫