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.
Favorites and recently used calculators are stored only in this browser (localStorage).
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.
See it as a graph
Other inputs stay at their current values while the chosen input varies from 1/4 to 4 times its current value. The red dot is the current value.
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.
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.
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.
Writing x and n in a trace table shows that each pass changes x to 3, 9, 27, 81 and 243 while n increases by 1. When x = 81, x < 100 still holds, so the loop runs once more, giving x = 243 and n = 5. The next test of x < 100 is false and the loop ends. The condition is tested 6 times, but the body runs 5 times.
i takes the values 1, 3, 5, 7 and 9; the next value 11 exceeds the final value 10, so the loop ends. s = 1 + 3 + 5 + 7 + 9 = 25. A for loop exits as soon as the control variable exceeds the final value, so the body never runs with a value beyond it.
The inner loop runs j from i to 5, so it runs 5 times for i = 1, 4 times for i = 2, …, and once for i = 5. The total is 5 + 4 + 3 + 2 + 1 = 15. Because the inner initial value depends on the outer variable i, the count is not simply 5 × 5.
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.
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.
m is the index of the largest element seen so far. At i = 2, a[2] = 8 > a[1] = 3, so m = 2. At i = 4, a[4] = 8 equals a[2] = 8, so > does not hold and m does not change. When the maximum appears more than once, the index of its first occurrence, 2, remains. With ≧ the result would be the index of the last occurrence, 4.
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.