This is a very simple command, and for me one of the most useful. The if/then command.
Starting off easy
Let’s first look at the if/then command:
if r = 1 then print “Yeap, its equal to one”
In this command, we are telling JB if r is equal to one then to display “Yeap, its equal to one”
So, plug this in to JB:
Print “Type in my favorite number” Input r If r = 1 then print “Yeap, its equal to one”
Type in any number. If you type in 1 it will print ‘Yeap, its equal to one’ If you didn’t type 1 in it will not print anything.
Easy, huh?
Now say if we want to have our program say ‘Nope, it was equal to one’ when the user typed in a number besides 1. We can add the ‘else’ command at the end like so:
Print “Type in my favorite number” Input r If r = 1 then print “Yeap, its equal to one” else print “Nope, it was equal to one”
Go ahead and try it. If you type in 1 it will say ‘Yeap, its equal to one’ but if you type a different number in (like 5) it will say ‘Nope, it was equal to one’.
If we want to have multiple if/thens add them like so:
Print “Type in my favorite number” Input r If r = 1 then print “Yeap, its equal to one” If r = 2 then print “That’s my other favorite number!”
And of course add the ELSE after the last IF/THEN:
Print “Type in my favorite number” Input r If r = 1 then print “Yeap, its equal to one” If r = 2 then print “That’s my other favorite number!” else print “Nope, it was equal to one”
Yeap, its time for homework! Don’t worry, this should be easy.
*Please understand strings$, print and input before taking this.
--Write a program. Make sure it follows these guidelines:
-The program should ask you the question “Do you want to learn about ‘my cat’ or ‘my dog’?”
-The user can then type in something to an input. If the user types ‘my cat’ then have it print something about the someone’s cat (just make up a print statement!). Then if the user types ‘my dog’ have it say something about the someone’s dog.
-If the user types in a bad command (ie. ‘my hamster’) have it print “Bad command”
-Here’s hint, string variables and if/then commands work like this:
if something$ = “response” then print “something”
So go ahead and use your imagination to create your own program. Here’s mine, it doesn’t have to be exactly like this, but it should work the same way.
Print “Do you want to learn about ‘my cat’ or ‘my dog’?” Input answer$ If answer$ = “my cat” then print “My cat is white. Her name is Leah” If answer$ = “my dog” then print “My dog is very small. His name is buddy” else print “Bad Command”
Say if we want our program to display 2 lines of text each. How would we do it? We add a colon between the things we want JB to do like this:
If answer$ = “my cat” then print “My cat is white. Her name is Leah” : print “She is fluffy”
Simple! We can do the same for else commands.
So say if we want BOTH a$ and b$ to equal something for it to print. We simply add ‘and’ between the two variables.
Print “Are you like me?” Print “Tell us your name” Input name$ Print “…and your favorite month” If name$ = “Andrew” and month$ = “July” then print “Yeah, your like me! Sorry about the bad news!” else print “Nope, your not like me” Let’s say we want to print ‘Yeah, your like me!’ if the user has the same name as me OR (not and) likes the same month. Simply do as before but instead of and put or. Print “Are you like me?” Print “Tell us your name” Input name$ Print “…and your favorite month” If name$ = “Andrew” or month$ = “July” then print “Yeah, your like me! Sorry about the bad news!” else print “Nope, your not like me”
Now say we want to make things more complex. Let’s have JB print something if both a = 1 and b = 2 or a = 2 and b = 3.
Syntax is exactly like before:
Print “Open the vault. There’s 2 combos that work” Print “First Num to spin?” Input a Print “Second Num to spin?” Input b If a = 1 and b = 2 or a = 2 and b = 3 then print “You opened it” else print “Nope”
And finally, remember, you can do more than print for the then/else command. Here’s and example with the goto command:
If a = 1 then goto [one]
Good job! You passed my IF/THEN tutorial without falling into a deep sleep!
Happy Programming and thanks for reading my tutorial!