When we want to execute some code many times, we can always copy-paste those commands. But that is not very efficient or readable, so we have commands like "While".
A While command has two parts:
condition
expression - checked each time before While executesbody
- list of commands executed when condition
is true
Let's write a program that prints numbers 1 to 5:
x
with value 1x <= 5
x
in While bodyx = x + 1
in While bodyYou should see numbers 1,2,3...5
printed in the output panel.
Notice the Assign command: x = x + 1
.
You can use x's previous value (before Assign was executed), when calculating its new value! How cool is that? :)