LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How To Program #4 - Input and Conditional Logic

Root / Submissions / [.]

haloopdyCreated:
Note: I would suggest running the programs posted before continuing so you know what the program does. Next tutorial (part 5)

Console Input

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:
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 Logic

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.

Simple Conditional

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 Conditional

Let'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
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.
, 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".

Conditional Structure

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!"
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

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.

Conclusion

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. Next tutorial (part 5)

Your tutorials are great! I like reading them :) "Programming is about logical construction, not memorization. Break a problem up into pieces and fit commands into the pieces." This concept is very important, good idea to mention it.

Replying to:Minxrod
Your tutorials are great! I like reading them :) "Programming is about logical construction, not memorization. Break a problem up into pieces and fit commands into the pieces." This concept is very important, good idea to mention it.
Thank you very much!

you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
it parses ' as the start of a comment What do you mean by this?

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
If you were to do something like this:
INPUT "TEST";ST$
PRINT ST$
and you passed something like hello 'world to the input, my understanding is that the parser would treat the 'world sequence as a comment and remove it from the input. It seems that the INPUT string is passed to a tiny SB parser to break down the text into properly-typed tokens, but in this case it slices out what it would see as comments.

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
'world is not removed from the input in that example.

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
Remember what I stated in lesson 1: this isn't a tutorial on how to use SmileBASIC, but rather how to program in general. I don't think it's wise to clutter people's learning with the minutia of the platform. Any details of the platform that I've delved into are in order to teach important programming concepts, like types and parameters. But yes, eventually I may go over all these differences, and I welcome comments like these. People who are more interested can look at these comments and get more specific information. I also didn't know about it parsing out comments. I'm still going to stick with INPUT in the tutorials for now, though. Oh, SF says that's not the case. That's good.

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
INPUT doesn't allow commas That's not accurate either, for INPUT A,B one is required, and this shows another key difference: LINPUT can only work with string variables, INPUT works with numeric variables too. I still don't understand it parses ' as the start of a comment.

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
12Me knows how to reproduce it, and I guess I don't. Wait for him to respond, I guess.

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
huh it worked before... what the heck... anyway, by "doesn't allow commas", I meant you can't input a string with commas in it. like, if you wanted to input "Hello, World!", you couldn't because it would count as 2 strings

Replying to:12Me21
you might want to mention the difference between INPUT and LINPUT (INPUT doesn't allow commas in the input strings, and it parses ' as the start of a comment)
Anything that is types into " " is considered a string. So when you type "Hello 'World" It will print Hello 'World.

You brought out some pretty good points. But i think that it's really important to bring out that constant use of IF statements leads to bad code duplication. Yea that is just terrible. There's also IF nesting. where you write IF statements like this:
IF .... THEN
      IF .... THEN
      END IF
END IF

these have been very helpful so far i am gaining intelligence

Replying to:codinginept
these have been very helpful so far i am gaining intelligence
hopefully you don't end up losing intelligence

Replying to:codinginept
these have been very helpful so far i am gaining intelligence
you'd better go catch it then

Replying to:codinginept
these have been very helpful so far i am gaining intelligence
tuna