Here we will learn the "If Then" and "If Then Else" statements that allow us to make decisions. But first, here is the code from the previous module - First Program where you were to add the code to ask the user what grade they are in. on mouseUp ask "What is your name?" put it into theName answer "Hello" & theName ask "What gender are you (f/m)?" put it into theGender end mouseUp
After running the above, we have 2 variables where wehave saved the answers that the user typed: theName - contains his/her name theGender - contains the gender they are Now let's have some fun with them. We want to greet them based on what gender they are. If the gender is "m" - then we want to say "Welcome Mr. (name)". If the gender is "f" we will say "Welcome Mrs.(name) For this we use an "if" statement. The form of it is:
1. The if ... then ... end if statement - one choice This allows you to ask a question and if it is TRUE, then do something special if (something is true) then (do this) end if so we change the code to read: on mouseUp ask "What is your name?" put it into theName answer "Hello" & theName ask "What gender are you (f/m)?" put it into theGender if theGender = "m" then answer "Welcome Mr." & theName end if if theGender = "f" then answer "Welcome Mrs." & theName end if end mouseUp
Change your code and try this yourself - it is a good way to learn LiveCode - Do It Yourself but we can do it in 1 statement using the more fancy form - the "If-then-else" statement where:
2. The if ... then ... else ... end if statement - 2 choices
This allows you to ask a question and if it is TRUE, do something special but if FALSE, then do something else. if (something is true) then (do this) else (do something else) end if So lets try the following changes: on mouseUp
ask "What gender are you (f/m)?" answer "Welcome Mr." & theName answer "Welcome Mrs." & theName
end mouseUp
Try this yourself - it is a good practice
3. Multiple if ... then ... else ... end if statements - more than 2 choices What happens if there are more than 2 choices? Suppose instead of typing "f" or "m", they type "x" (they are being funny - there is no gender 'x"). In our code it would address them as Mrs. and that is not right. Try this yoursel with your code now
So we need another if statement to handle incorrect choices.: ask "What is your name?" put it into theName answer "Hello" & theName ask "What gender are you (f/m)?" put it into theGender if theGender = "m" then answer "Welcome Mr." & theName else if theGender = "f" then answer "Welcome Mrs." & theName else answer "Welcome ??." & theName
ask "What is your name?" put it into theName answer "Hello" & theName ask "What gender are you (f/m)?" put it into theGender if theGender = "m" then answer "Welcome Mr." & theName else if theGender = "f" then answer "Welcome Mrs." & theName else answer "Welcome ??." & theName end if
end mouseUp
Again, change your code and try it...Time for another challenge: See if you can do this yourself 1. Change the following line in your program:
from:
ask "What gender are you (f/m)?"
to
answer "What gender are you ?" with "m" or "f"
So that it looks something like this: e.g.
on mouseUp ask "What is your name?" put it into theName answer "Hello" & theName answer "What gender are you ?" with "m" or "f" put it into theGender if theGender = "m" then answer "Welcome Mr." & theName end if if theGender = "f" then answer "Welcome Mrs." & theName end if end mouseUp
What is the difference? Does it work differently? What is good about using "ask" instead of "answer"? What is not good about using "ask" instead of "answer"?
Now you can control what they answer and you do not need to test for other things that they can type
Variations of IF-Then-Else Statements:
There are many ways to have multiple If statements: The following all do the same exact things. They are all "right", there is no wrong way. Look at them and be able to read and understand each of them. Then decide which way you like best and adopt that "style".
1. One if statement after the other: on
mouseUp answer "Welcome" answer "What
grade are you in" with "9" or "10" or "11" or "12" put it into
myGrade ask "What
is your name?" put it into myName if myGrade is "9" then answer "Ugh, Freshman, stay awaty from me " & myName end if if myGrade is "10" then answer "Ha ha, "& myName & " are you enjoying all that work?" end if if myGrade is "11" then answer "Are
you enjoying your junior year? " & myName end if if myGrade is "12" then answer "Welcome
" & myName & " Do you have a case of
senioritis? " with "Yes" or "Yes" end if end mouseUp
2. Nested if statements (indented): on
mouseUp answer"Hello
there!" ask"What
is your name?" put it into theName answer"What
grade are you in, " & theName & "?"with 9 or 10 or 11 or 12 put it into
theGrade if theGrade is 9 then answer"Hello
little Freshy" else if theGrade is 10 then answer"Yeah!
2017!!!" else if theGrade is 11 then answer"Cool,
I have some friends in you're grade." else if theGrade is 12 then answer"Don't
you have collages to be looking at???" end if end if end if end if end
mouseUp
3. Else-if Statements (more compact, easier to read):
on
mouseUp ask "What
is your name?" put it into nameS answer "What
grade are you in?" with "9" or "10" or "11" or "12" put it into grade if grade is "9" then answer "Welcome
to ERHS, "& nameS & ". My name is Evan Hansen. I may
not know any "&genderN&" named "&nameS&"." else if grade is "10" then answer "Ayye,
I am a sophomore as well! I may not know any "&genderN&"
named "&nameS&"." else if grade is "11" then answer "I
do not think I know any juniors named '"&nameS&".'" else if grade is "12" then answer "I
may know you, idk. Good luck with colleges." end if end
mouseUp
NEXT - Input/Output
|