Practice For Loop in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

A control structure that repeats a block of code a specific number of times or for each item in a collection.

A for loop is like 'do this for each...' โ€” for each student in the class, print their name. For each number from 1 to 10, add it to the total.

Example 1

easy
Trace: FOR i = 0 TO 4: OUTPUT i * i.

Example 2

medium
Convert this FOR loop to a WHILE loop: FOR i = 1 TO 5: OUTPUT i.

Example 3

medium
Write a FOR loop that calculates 5! = 5 \times 4 \times 3 \times 2 \times 1.

Example 4

medium
What does this code output? SET scores = [85, 92, 78, 95, 88] SET highest = scores[0] FOR i = 1 TO LENGTH(scores) - 1 IF scores[i] > highest THEN SET highest = scores[i] END IF END FOR OUTPUT highest