Friday, July 5, 2013

Tutorial #3 - Variables 101

Variables are essential for doing anything useful in Petit Computer. They are basically places to hold data, kind of like a file on a hard drive. You can read and write to a variable, and you can also have the user of your program do the same. A variable can hold (equal, =) either a number (ex: 132) or a string (ex: "Hey there!").

If you decide you want your variable to hold a number you simply have to set it like this.

  1. SOMEVAR = 132
If you decide you want it to be a string, you must end the variable name with $.
  1. SOMEVAR$ = "Hey there!"
There are plenty of things you can do with a variable, below are just a few examples.
  1. ANUMBER = 132
  2. ASTRING$ = "This is text"
  3. PRINT ANUMBER 'This will simply print: 132
  4. PRINT ASTRING$ 'This will simply print: "This is text"
  5. '-----------
  6. ASTRING$ = "Your number: " 'You can redefine a variable at any time
  7. ANUMBER = ANUMBER + 5 'You can also do math!
  8. PRINT ASTRING$;ANUMBER 'You can also print multiple variables at once, separate with ;
  9. 'The above will print: Your number: 137
  10. '----------
  11. STRINGA$ = "Hello "
  12. STRINGB$ = "there,"
  13. ASTRING$ = STRINGA$;STRINGB$ 'You can also join two strings together
  14. PRINT ASTRING$;" and thats all!" 'You can still manually add to your print command as well.
  15. 'The above will print: Hello there, and that is all!

I hope you understand this basic introduction into variables. If you have any questions feel free to comment and ask.

QR Code for this Tutorial


No comments:

Post a Comment