granite-docs/docs/finishing-touches.md
2024-08-16 14:48:06 +01:00

2.2 KiB

Finishing touches!

Now you have a perfectly working counter program! For your final lesson however, we will teach you about comments and explicitly exiting the program!

New operations

  • :0(comment), comment, an operation that does nothing but is helpful for a programmer!
  • :~, exit, explicitly calls an exit to your program
  • :*(left),(right),(output), string concatenation, concatenates (left) and (right) and stores it in (output)

Tutorial

Your program should look something like this:

:>"Enter some numbers!",message
:!message
:?starting_number
:?finishing_number
:?step
:@count
:astarting_number,step,starting_number
:!starting_number
:-count,starting_number,finishing_number

This program is perfectly readable, however it could do with some comments and an obvious exit point!

You can add comments as an operation and it wont affect your program! So you could do something like this!

:>"Enter some numbers!",message
:!message

:0 Prompt the user for their chosen numbers!
:?starting_number
:?finishing_number
:?step

:0 Keep counting until we hit the finishing number
:@count
:astarting_number,step,starting_number
:!starting_number
:-count,starting_number,finishing_number

:0 We're done! Quit!
:~

We can also add a concatenation! Before the explicit exit, initialise the variable msg with the string "I have counted to "! After this operation, add the operation :*msg,finishing_number,msg and print out message!

Now when your program finishes, it will tell you what your program counted to!

Now your output will look like this if you run your program!

Enter some numbers!
? 0
? 5
? 1
1
2
3
4
5
I have counted to 5

Continue the guidebook

Well done! You've finished the guidebook!

You can further your learning by going to the further concepts section on the top navigation bar!

After example

:>"Enter some numbers!",message
:!message

:0 Prompt the user for their chosen numbers!
:?starting_number
:?finishing_number
:?step

:0 Keep counting until we hit the finishing number
:@count
:astarting_number,step,starting_number
:!starting_number
:-count,starting_number,finishing_number

:0 We're done! Quit!
:>"I have counted to ",msg
:*msg,finishing_number,msg
:!msg
:~