Pseudocode Runner and Tracer

Run programs written in the exam's pseudocode and check how variables change, one line at a time. The tool supports if, while and for, arrays, functions and recursive calls. Enter the program from a question and compare it with your own trace (the result you followed by hand). English keywords such as integer, for (i from 1 to n step 1), length(a) and output can be used.

Program

Supported syntax
Declare  integer: x ← 0, y      real / string / boolean also work
Array    integer array: a ← {1, 2, 3}   elements start at a[1]
Length   length(a)                Append  aの末尾に x を追加する
Assign   x ← expression           Output  output x, y
If       if (condition) … elseif (condition) … else … endif
Loops    while (condition) … endwhile    do … while (condition)
         for (i from 1 to n step 1) … endfor   (from n down to 1 also works)
Function ○integer: f(integer: n), with the body indented on the lines below; return expression
Procedure ○g(integer: n)
Operators + - × ÷ mod (* / also work)   comparison = ≠ < > ≦ ≧   logic and or not
         Storing into an integer variable drops the fractional part of a ÷ result
Strings  "abc", joined with +       Booleans true / false
The Japanese keywords of the exam (整数型, を 1 から n まで 1 ずつ増やす, 出力 …) also work.

Output


    

Trace

A simple learning-oriented interpreter that supports the main forms of the exam's pseudocode; details may differ from actual exam questions. The variable table shows the variables of the function being executed at that moment (variables declared outside functions can also be used inside them). Arrays are passed by reference.

Part B and tracing

Part B of Japan's Fundamental Information Technology Engineer Examination mainly asks you to read programs written in pseudocode and answer with the result of running them or the word that fills a blank. That requires the ability to trace: following a program line by line and writing down in a table how the values of variables change. With this tool you can compare your hand trace with the actual run, line by line.

Pseudocode basics

In the pseudocode, variables are declared with a type, as in “integer: x”, and values are assigned with an arrow, as in “x ← 1”. Branches are written if … elseif … else … endif, and loops while … endwhile or for … endfor. Arrays are written like {1, 2, 3}, and elements are numbered from 1, as in a[1]. Functions are declared like “○integer: f(integer: n)” and return values with return. Note that when the result of ÷ is stored in an integer variable, its fractional part is dropped.

Tips for tracing

When tracing, make a table with a column for each variable and write in the new value every time a line changes it. For loops, write down every check of the condition, including the last one where it becomes false and the loop ends. Off-by-one mistakes often come from whether array indexes start at 1 or 0, and whether a loop runs “up to” or “less than” a bound. For recursive calls, keep in mind that each level of the call has its own variables, and calculate the return values from the bottom up.

Programs that often appear

Common subjects in Part B include the sum, maximum and sorting of arrays (bubble sort, selection sort), binary search, the greatest common divisor (Euclidean algorithm), conversion to binary, recursion (factorial, Fibonacci numbers) and stacks or queues built on arrays. Pick an example and run it, check how the variables change in the trace, then change a condition or a loop bound slightly to see how the result changes.

Practice related questions

The two assignments in the following program were meant to swap the values of x and y. What are the values of x and y after it runs?

  1. Ax = 3, y = 3
  2. Bx = 7, y = 3
  3. Cx = 7, y = 7
Show answer and explanation

Answer: C. x = 7, y = 7

In the pseudocode, assignments are executed one at a time from top to bottom. x ← y makes x 7, and the original value 3 is lost at that point. The following y ← x then copies the new value of x, 7, into y, so x = 7 and y = 7. To swap values, first save the original value in a temporary variable, as in t ← x, x ← y, y ← t.

The following program passes values along three variables. What are the values of a, b and c after it runs?

  1. Aa = 2, b = 3, c = 1
  2. Ba = 2, b = 3, c = 2
  3. Ca = 3, b = 1, c = 2
Show answer and explanation

Answer: A. a = 2, b = 3, c = 1

t ← a saves the original value of a, 1, in t. a ← b makes a = 2, b ← c makes b = 3, and the final c ← t puts the saved 1 into c. The three values each move one place forward, giving a = 2, b = 3, c = 1. Writing c ← a without t would give c = 2, because a has already become 2 by then.

The following program has a condition inside a nested loop. What is the value of c after it runs?

  1. A4
  2. B6
  3. C10
Show answer and explanation

Answer: B. 6

The inner loop runs j = 1 to i and increments c only when j is odd. For i = 1 that is j = 1 (once), for i = 2 j = 1 (once), for i = 3 j = 1, 3 (twice) and for i = 4 j = 1, 3 (twice), for a total of 1 + 1 + 2 + 2 = 6. The inner body runs 1 + 2 + 3 + 4 = 10 times in all, but the condition holds in only 6 of them. This is a basic combination of control structures: a selection inside an iteration.

The following program was meant to compute the sum of all elements of array a. Array indices start at 1. What is the value of s after it runs?

  1. A20
  2. B21
  3. C24
Show answer and explanation

Answer: B. 21

length(a) is 5, so i runs from 1 to 4 and s = 4 + 7 + 1 + 9 = 21. The last element a[5] = 3 is not added, so the result is 3 less than the correct sum 24. Carrying over the pattern for languages whose indices start at 0 (from 0 to length − 1) to arrays that start at 1 causes this kind of off-by-one error.

FAQ

Does it support all of the exam's pseudocode?

It supports declarations of variables and arrays, assignment, if, while, do while and for, functions and procedures, array length and appending to arrays, and arithmetic, comparison and logical operators. Some forms that are defined per question, such as two-dimensional arrays, objects (classes) and bit-operation symbols, are not supported.

What does ÷ produce?

÷ is ordinary division, so 7 ÷ 2 is 3.5. Storing the result in an integer variable (or returning it from an integer function) drops the fractional part, so in “integer: mid ← (1 + 8) ÷ 2”, mid becomes 4. Use mod for the remainder.

What happens with an infinite loop?

When more than 20,000 lines have been executed, the run stops and an error is shown. Check that the variables used in the while condition change inside the loop.

Is my program sent anywhere?

No. The program runs only inside this page (in your browser) and is not sent anywhere.

Related terms

Pseudocode Recursion Stack

Related calculators

Semiconductor, Electronics and Computing Calculators →