Page 21-55
1. This program needs an integer number as input. Thus, before execution,
that number (n) is in stack level 1. The program is then executed.
2. A zero is entered, moving n to stack level 2.
3. The command DUP, which can be typed in as ~~dup~, copies
the contents of stack level 1, moves all the stack levels upwards, and places
the copy just made in stack level 1. Thus, after DUP is executed, n is in
stack level 3, and zeroes fill stack levels 1 and 2.
4. The piece of code → n S k stores the values of n, 0, and 0, respectively
into local variables n, S, k. We say that the variables n, S, and k have
been initialized
(S and k to zero, n to whatever value the user chooses).
5. The piece of code 0. n START identifies a START loop whose index will
take values of 0, 1, 2, …, n
6. The sum S is incremented by k
2
in the piece of code that reads: k SQ S +
7. The index k is incremented by 1 in the piece of code that reads: 1. k +
8. At this point, the updated values of S and k are available in stack levels 2
and 1, respectively. The piece of code ‘k‘ STO stores the value from stack
level 1 into local variable k. The updated value of S now occupies stack
level 1.
9. The piece of code ‘S‘ STO stores the value from stack level 1 into local
variable k. The stack is now empty.
10. The particle NEXT increases the index by one and sends the control to the
beginning of the loop (step 6).
11. The loop is repeated until the loop index reaches the maximum value, n.
12. The last part of the program recalls the last value of S (the summation), tags
it, and places it in stack level 1 to be viewed by the user as the program
output.
To see the program in action, step by step, you can use the debugger as follows
(use n = 2). Let SL1 mean stack level 1:
J2[‘] @@@S1@@ ` Place a 2 in level 2, and the
program name, ‘S1’, in level 1