LoginLogin

How To Program #3 - Variables and Math

Root / Submissions / [.]

haloopdyCreated:
That last tutorial had like ONE command. Wow, so amazing. This one has a whopping TWO commands ^.^ Next tutorial (part 4)

Variables

Remember how I said computers had memory and it let us have state? Now we're going to see how SmileBASIC lets us access this memory. SmileBASIC (and basically all other programming languages) allow you to store and read values from memory through things called variables. They're like super mini files: you can save a few values and read them later. Unlike files, however, variables last at the most until the program is finished. There are several types of variables for holding different types of data. Whole numbers are stored in integers, real numbers (numbers with a decimal portion) are stored in floats, and sequences of characters are stored in strings. For organization, there's also a type which holds a group of values called an array. We saw a string in the last tutorial; anything enclosed in double quotes is a string.

Diving in

Start up a new program and type the following in the editor:
DIM COOKIES%=5
PRINT "I have ";COOKIES%;" cookies!"
If you run this, you'll get the output I have 5 cookies!. There's a lot of new stuff, so let's break it down. The first line uses the DIM command to create a new variable. This is a memory operation: we're asking for a named place to store a value. The name of the variable is COOKIES, and the % that follows it makes it an integer type. % is one of the type symbols; when you place it after a variable name, it gives the variable that type. It's a good practice to put the proper type symbol on all your variables, even though it's not necessary. Remember, a type is just a way to distinguish different types of data, like integers, floats, and strings. You might find it strange that we separate whole numbers (integers) from numbers with a decimal portion (floats), but we'll get to that later. In the same line that we created the variable COOKIES%, we also assign the integer 5 to the variable. The = symbol is the assignment operator, and it stores the value on the right into the variable on the left. The left side of an = operator must always be a variable. The right side can be anything that is a value. So, after this statement is run, the variable COOKIES% is created and the value 5 is stored inside it.
Extra Content

The difference between a statement and a command

You might notice that I called DIM a command, but called the whole first line a statement. That's because a command is a single direct order for the computer, but a statement is a declaration of an event (which may contain commands). Thus, the command in the first line is just the DIM COOKIES% part, but because I also assigned a value to it, the entire event of creating the variable and then assigning a value to it became a statement. A statement is basically any single event specified by the program. In SmileBASIC, this is usually one line. Thus, even without the =5 part, DIM COOKIES% would still be a statement (it just so happens to also be a command). Once again a command orders the computer to do something, and a statement is an overall action for the computer to perform.
There's a difference between the COOKIES% and the 5, even though they're both technically integers. COOKIES% is a variable, but 5 is called a literal. A literal is a single unchanging value that is hardcoded into the program. In this case, since you typed out the number 5, it won't change unless you go back and type a different number (thus changing the program itself). This is in contrast with a variable, where you can assign different values to it whenever you want and use it in multiple places. The second line prints out two string literals and your COOKIES% variable. Now that we've assigned a value to COOKIES%, we can use this variable to recall that value. Notice that we still include the % after COOKIES even though we're just reading it; this is so that the things that use the variable (in this case, PRINT) know what type it is. PRINT is a special command that can take a bunch of different types and slap them all together in the same output. Here, we start with the string literal "I have", then we have our COOKIES% integer, then we end with another string literal " cookies!". Everything is separated by a semicolon (;). PRINT is one of the only commands to separate parts with a semicolon, so keep that in mind when we start to get to OTHER commands with separated parts. To review, this code just assigns 5 to the new variable COOKIES%, then outputs a nice thing that tells us how many cookies we have. Now that we can store and recall values, a whole new world opens up to us.

Math

Basic operations

Programming languages can perform basic arithmetic on variables or literals. Math can be inserted anywhere that a value could be used. For instance, try:
PRINT 5+5
PRINT "2 times 2 is: ";2*2
PRINT "Order of operations: ";(2+1)*4+1
PRINT "Division: ";5/2
We can use the four basic arithmetic operations, but remember that multiplication uses *, not X. Math expressions follow order of operations; use parenthesis just like you would in math. Performing math on literals is pretty boring though, because we can just do that on a calculator. What we REALLY want to do is stick some variables in there:

Variable Math

DIM NUMBERONE%=2
DIM NUMBERTWO%=5
PRINT "Variables: ";NUMBERONE%,NUMBERTWO%
PRINT "Variables multiplied: ";NUMBERONE%*NUMBERTWO%
NUMBERONE%=NUMBERTWO%+3
PRINT "Now NUMBERONE is: ";NUMBERONE%
PRINT "But NUMBERTWO is still: ";NUMBERTWO%
So we can substitute variables wherever we want in a math equation. We also see that we can change the variable's value at any time in our program, like we did on line 5. Remember, the assignment operator (=) takes the value on the right side and stores it into the variable on the left. The important thing to take away here is that math and values are interchangeable, because the programming language will evaluate the math and use the final value when it runs. Also note that NUMBERTWO% doesn't change even though it's in the assignment; remember, only the variable on the LEFT gets assigned the value. Also notice that we used a comma instead of a semicolon on the first print statement. The semicolon is just a separator for different values in PRINT, but the colon separates AND adds some space. In the case of PRINT, it's just like a "padded" semicolon. Again, PRINT is one of the only commands to do this; we'll see later how this changes with other commands.

Increment and Decrement

An EXTREMELY common operation is to increment (add to) or decrement (subtract from) a variable. Since we usually don't know what the value within the variable is at any given time, we would have to do something like this:
DIM NUMBER%=5
PRINT "First it's ";NUMBER%
NUMBER%=NUMBER%+1
PRINT "Now it's ";NUMBER%
The right side of the assignment operator is always evaluated first before storing the value into the left side. Here, we're taking the value of NUMBER%, adding one, then storing it back into NUMBER%. Since NUMBER% was 5, after the assignment, NUMBER% will contain 6. You can think of this as adding one "into" NUMBER%. I think this is a little clunky though, and luckily, there's a better way to add or subtract stuff "into" a variable:
DIM NUMBER%=5
INC NUMBER%,1
This code (except for the missing print statements) does exactly the same thing as the previous one. The INC command adds the second value INTO the first variable. This is a "standard" command, so it uses commas to separate the parts it needs. The parts given to a command are called parameters, so in our example, NUMBER% is the first parameter, and 1 is the second parameter. Most commands need the parameters in a specific order; in INC's case, the first parameter HAS to be the variable you're changing, and the second command is the value you want to increment by. There is also a command for subtracting an amount from a variable called DEC. It works in the same way:
DIM NUMBER%=5
DEC NUMBER%,2
PRINT "Number is now: ";NUMBER%
Subtract 2 from the NUMBER% variable. It should be 3 afterwards.

Conclusion

Variables are how we create state in a program. They're a named location in memory which stores a value. Variables can be one of three types: integer, float, or string. We used the integer type for this tutorial, which is indicated by the % after the variable name. We create variables with the DIM command, and we assign values to variables with the assignment operator, =. We can also insert math operations into our program wherever it expects a value. We can easily add or subtract from a variable using the INC or DEC commands. Next tutorial (part 4)

Why use an integer for the COOKIE variable? If someone had 3 cookies and a half-a-cookie, it'd round to 3! [/pointless_questioning] should've mentioned VAR too.

Replying to:Perska
Why use an integer for the COOKIE variable? If someone had 3 cookies and a half-a-cookie, it'd round to 3! [/pointless_questioning] should've mentioned VAR too.
I'll mention VAR when it's useful. Right now, it's easier to just use something consistent, and DIM will let you create variables and arrays. Plus it's how it was done in regular BASIC, so I'm trying to keep it kind of "standard".

Just a heads-up, INC's second argument is optional. Left alone it defaults to 1. Same with DEC. I get the feeling you already knew this but I thought I'd tell you anyway in case you wanted to mention it. Also INC works on strings.
VAR S$="AYY"
INC S$,"LMAO"
PRINT S$
Obviously you have to pass an argument and you can't use a number as the value.

Replying to:snail_
Just a heads-up, INC's second argument is optional. Left alone it defaults to 1. Same with DEC. I get the feeling you already knew this but I thought I'd tell you anyway in case you wanted to mention it. Also INC works on strings.
VAR S$="AYY"
INC S$,"LMAO"
PRINT S$
Obviously you have to pass an argument and you can't use a number as the value.
Shhhh I'm trying to make things simple for now! But yes, these are all things that work. I didn't know about the string one though. I was going to go over "optional" parameters in a later tutorial. I don't like overloading people with information that's not necessary for the current tutorial (even if it seems like I actually do lol).

One thing that is important to bring out is that SB is automatically integer declared when declaring variables. so you don't need to use DIM to declare a variable. The same thing is done when declaring strings. Just put $ at the end of the variable to notify SB that that variable is a string. I know you're trying to keep things simple, but there's a way to make variable declaration mandatory. Just type OPTION STRICT on the first line. Then you just use VAR to define your variables.