Note: I would suggest running the programs posted before continuing so you know what the program does.
In SmileBASIC, we can get user input from the keyboard, touchscreen, buttons, and microphone. We're going to focus on keyboard input for now, since it's easier. This is called "console input"; let's check it out:
INPUT "What is your name";NAME$
PRINT "OK, your name is ";NAME$
We're using a new data type here: the string. Strings are indicated by an ending $ symbol (remember that integers use %). The INPUT command outputs a prompt using the first parameter, then takes stuff you type on the keyboard (until you press enter) and stores it into the second parameter. In our example, you should see the prompt "What is your name?". If you type some stuff and press enter, it'll store whatever you typed into the NAME$ variable. Then we print out the thing you typed. Notice that a question mark is added even though we didn't put one in the prompt. This is just how INPUT works.
Again, it's important to remember that using a semicolon to separate parameters is uncommon. It's just an unfortunate thing you'll have to memorize; for now, we're just going to use it for PRINT and INPUT.
You can also get numbers from the user:
INPUT "How old are you";AGE%
PRINT "Oh OK, so you're ";AGE%
If you type in something that isn't a number, it'll ask you again automatically. This is just a feature of INPUT.
Up to this point, we've only worked with a basic list of statements for the computer to process. What a bore; programming is all about controlling the flow of data and working with state. We can use conditional logic to make things happen IF a condition is met. Conditional logic is probably THE most important part in a program.
INPUT "What is your age";AGE%
IF AGE%>90 THEN
PRINT "DANG son, you're old!"
PRINT "You entered ";AGE%
We start by doing the same thing we did before: asking for an age. The next part then says IF the age entered is greater than 90 THEN print out a silly saying. If you run this code and enter anything that's not greater than 90, you won't see the "DANG son, you're old!" output. This is called conditional logic, because it allows you to run statements conditionally (meaning only if a condition is met). The IF statement will run everything between the THEN and the ENDIF when the statement after the IF is true.
We can use various comparison operators to produce a true or false statement. We used greater than (>), but there's also less than (<), less than or equals (<=), greater than or equals (>=), equals (==), and not equals (!=). For instance, if we wanted to see if the age was EXACTLY 16, we could say IF AGE%==16 THEN. This reads "If age is equal to 16, then (run code in IF statement)". We'll see more uses of these comparison operators later, just remember that they're used to check values.
Let's look at another example:
INPUT "How many monkeys do you have";MONKEYS%
IF MONKEYS%>9000 THEN
PRINT "There's no way that can be right!"
ELSEIF MONKEYS%>=10 THEN
PRINT "You probably have too many monkeys"
ELSEIF MONKEYS%>=5 THEN
PRINT "That's a good amount of monkeys"
PRINT "You don't have enough monkeys"
PRINT "End of program"
Wow, that's a lot of monkeys. Let's take a look (but you should definitely run the program a few times to get an idea first). We start again with asking for a value (number of monkeys). Then we have a big giant conditional statement. The first part works the same as before: if the MONKEYS% variable is greater than 9000, we print a DBZ reference. OTHERWISE if MONKEYS% is greater than or equal to 10, we print "You probably have too many monkeys". That IF and ELSEIF can be read just like the English statement: "If monkeys is greater than 9000, that can't be right. Otherwise if monkeys is greater than or equal to 10, you have too many monkeys (etc.)". Just like in English, the ELSEIF is only even checked IF the original didn't come true. We can stack up as many of these ELSEIFs as we want. The program will fall through the IF statements until one comes true, then it will run the code inside. If none are true, the ELSE statement runs. When something in the IF/ELSEIF chain comes true, the REST of the statements are skipped. It basically jumps down to the ENDIF statement.
For example, if we enter 9999, the first IF statement comes true. We print "There's no way that can be right!", then skip down to the ENDIF statement. No other statements in the IF/ENDIFblock are checked For now, think of a block of code as grouped together instructions. In this case, everything between the IF and the ENDIF is part of the same conditional. In terms of real code blocks, this one is kind of a bad example as it's not exactly one block. We'll learn about different kinds of (actual) blocks later., and we jump right down to the ending PRINT statement. If we enter 100, the first IF statement is false, so it falls to the second check. The second check turns out to be true though (100 is indeed greater than or equal to 10), so we print "You probably have too many monkeys" and skip to the ENDIF. Let's say we enter 1. The >9000 check will fail, the >=10 check will fail, and the >=5 check will fail. Since the last part of the mega IF statement has no conditional (it's just ELSE), it catches it, and we print "You don't have enough monkeys".
An IF statement can have any number of ELSEIF statements. It must have a single IF statement at the beginning, and can have an optional ELSE at the end. The following are all valid conditionals. These aren't full programs, just snippets (small sections of code to demonstrate a point):
IF NAME$=="randomouscrap" THEN
PRINT "Wow, that's my name!"
PRINT "Oh, that's a nice name I guess"
ENDIFIF CODE%==1234 THEN
PRINT "Nice counting"
ELSEIF CODE%==1337 THEN
PRINT "R3411y, m4n?"
ENDIFIF PASSWORD$=="secret" THEN
PRINT "Welcome, master!"
INPUT "What do you want to do today";ACTION$
Importance of Logic
This is important stuff right here
We're at a pretty good point here, so let's talk about some programming concepts. Programming is about using basic tools (commands and statements) and combining them to make things. You shouldn't be memorizing the set of commands required to do a complex task; instead, you should be breaking down the problem so you can construct the program from individual commands. The only thing you should be memorizing are the commands themselves, NOT how to put them together into a specific program.
For example, if I were to ask you to get a user's height and then check to see if they're a giant, you shouldn't be copying the code from earlier in the tutorial. You should be breaking down the problem based on the available commands and logic. Start from the top: asking for a user's height. Asking means getting information from the user, so we'll use INPUT. From here, we know that INPUT has to store the information somewhere, so we'll need a variable. What are we storing? Height, of course. Now we know that we'll have to create a variable to hold height, which will be our DIM command. OK, so we have everything we need to get the height. What was the next part of the problem? Checking to see if they're a giant. A giant is probably taller than some large height, so we need to check if their height is greater than some large number. We know checks like these are performed with conditional logic, so we can come up with an IF-ELSE structure to fit the second part of the problem.
It might seem like we were just pulling stuff out of the air, and it might seem like it's impossible to just know what you need. But all you have to do is break the problem down into smaller and smaller pieces until you can make the pieces fit into the language constructs. We broke the height problem down into user input and then a check, then we broke down user input into the variable creation and user input commands. There's no set way to do this; it's a logical puzzle with no set solution. You're given all the pieces (the commands and such), and you're given a problem (a program you want to write), and all you have to do is fit the pieces together so it solves the problem. This is why it's EXTREMELY important to know all these basic commands VERY WELL; without knowing how the pieces of a puzzle work, you won't be able to put anything together.
In future tutorials, I'm going to continue to focus on this concept of constructing a program from a problem. This is probably the hardest part about programming, and I'm going to do my best to make it all make sense. There's only so much that you need to know about the commands themselves; the rest of programming is construction and logic.
We can read typed user input using the INPUT command. We can designate commands to run only at certain times using IF statements. This conditional logic lets us run commands only when some condition is true (or false).
Programming is about logical construction, not memorization. Break a problem up into pieces and fit commands into the pieces.