Console InputIn 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:
DIM NAME$ 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:
DIM AGE% 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.
Conditional LogicUp 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.
DIM AGE% INPUT "What is your age";AGE% IF AGE%>90 THEN PRINT "DANG son, you're old!" ENDIF 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.
Complex ConditionalLet's look at another example:
DIM MONKEYS% 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" ELSE PRINT "You don't have enough monkeys" ENDIF 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/ENDIF block are checked
, 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".
asideFor 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.
Conditional StructureAn 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!" ELSE PRINT "Oh, that's a nice name I guess" ENDIF
IF CODE%==1234 THEN PRINT "Nice counting" ELSEIF CODE%==1337 THEN PRINT "R3411y, m4n?" ENDIF
IF PASSWORD$=="secret" THEN PRINT "Welcome, master!" INPUT "What do you want to do today";ACTION$ ENDIF