While
When we want to execute some code multiple 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
- checked each time, beforeWhile
body is executedbody
- list of commands executed whencondition
istrue
Let's write a program that prints numbers 1 to 5:
- declare an integer
x
with value 1 - add a While command with condition
x <= 5
- output
x
in While body - assign
x = x + 1
in While body
You 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? :)