Trace table for a loop with a branch

Trace the following pseudocode by hand. MOD gives the remainder after division, so x MOD 2 = 0 is true when x is even.


Plain text
x ← 5
count ← 0
WHILE x ≠ 1
    IF x MOD 2 = 0 THEN
        x ← x / 2
    ELSE
        x ← 3 * x + 1
    END IF
    count ← count + 1
END WHILE
OUTPUT count


For each pass through the loop, complete the table. In the branch column, type odd if the ELSE branch runs or even if the THEN branch runs, based on x at the start of that pass. In the last column, give x after the pass as a whole number.

PassBranch taken (odd or even)x after the pass
Pass 1
Pass 2
Pass 3
Pass 4
Pass 5

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Flowcharts Questions: