93 lines
3.3 KiB
Markdown
93 lines
3.3 KiB
Markdown
# Jumps and labels
|
|
Granite does not have conventional loops similar to a C-like programming language does. It instead is more alike to BASIC having _labels_ and jumps.
|
|
|
|
Labels are operations that have no direct function when ran in the program _but_ they mark where you can jump to! Jumps move where the program is being ran from to labels! These tools together allows you to create loops!
|
|
|
|
## New operations
|
|
* `:@(label)`, label, places a new label at this location
|
|
* `:<(label)`, unconditional jump, jumps to this label without any condition
|
|
* `:-(label),(variable),(check)`, jump if less than, jumps to this label if `(variable)` is less than `(check)` which can be a variable or an integer literal.
|
|
* `:a(left),(right),(output)`, add, performs `(left)` add `(right)` and stores the result into `(output)`
|
|
|
|
There are also `+` and `=` operators which function like the `-` operator but check if `(variable)` is greater than (for `+`) or equal to (for `=`) `(check)` instead.
|
|
|
|
There are also `s`, `m` and `d` operators which function like the `a` operator but perform subtraction, multiplication, and division respectively.
|
|
|
|
## Tutorial
|
|
Open up your program! It should look something like this:
|
|
|
|
```
|
|
:>0,starting_number
|
|
:^starting_number
|
|
:!starting_number
|
|
```
|
|
|
|
At the top of your program's source code add this operation:
|
|
|
|
`:>1,step`
|
|
|
|
This defines your counter's _step_!
|
|
|
|
Now _replace_ the increment operation with this add operation:
|
|
|
|
`:astarting_number,step,starting_number`
|
|
|
|
What this operation does is takes `starting_number` and `step` and adds them together and then stores that into `starting_number`!
|
|
|
|
If you run your program now, you should get the number 1 as an output. Wonderful!
|
|
|
|
Now to add _looping_!
|
|
|
|
Your program should look like this:
|
|
|
|
```
|
|
:>1,step
|
|
:>0,starting_number
|
|
:astarting_number,step,starting_number
|
|
:!starting_number
|
|
```
|
|
|
|
As we are _counting_ up, we want to loop where the counting happens and _not_ where the variable setting happens!
|
|
|
|
This means we want to _set a new label_ above the add operation so above the add operation, add this operation:
|
|
|
|
`:@count`
|
|
|
|
By itself, this won't create a loop but we can do that easily! At the bottom of your program, below the print operation, add a new unconditional jump operation!
|
|
|
|
`:<count`
|
|
|
|
Now if you start your program, you will see it spit out a stream of numbers as it quickly keeps counting from 0! If this happens, wonderful, you've just created your first loop!
|
|
|
|
Exit the program (this will most likely be with `Control + C`) and re-enter the source code! We want to count up to a certain number, let's say 10!
|
|
|
|
To do this, at the top of your program, initialise a new variable called `finishing_number` with the number 10 in it!
|
|
|
|
Now at the bottom of your program, _replace_ your unconditional jump operation with this conditional one!
|
|
|
|
`:-count,starting_number,finishing_number`
|
|
|
|
This conditional jump will continue looping until `starting_number` is not less than `finishing_number`!
|
|
|
|
If you run your program now you should see this output:
|
|
|
|
```
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
```
|
|
|
|
Congratulations! You've not only made loops but _also_ loops that break when a condition is met!
|
|
|
|
## Extension
|
|
Try messing with the `step` and `finishing_number` variables to see different outputs when you add more in one loop around!
|
|
|
|
## Continue the guidebook
|
|
* [Next page: Taking input from the user](taking-input.md)
|