VariablesRemember 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 inStart 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.
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.
The difference between a statement and a commandYou 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.
Basic operationsProgramming 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/2We 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:
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 DecrementAn 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%,1This 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.