Practice 5
Practice 5
Practice 5
CONDITIONALS
Page |1
Take Control with Conditionals
In this lesson, you will learn how to create your very own calculator using conditional
statements!
At this point we can give a computer a series of instructions and it will carry them out for
us. But what if we don't want to perform the same instructions every time we run a
program? For instance, say I want to display a special message on my birthday. The
following program will do just that:
Conditions are statements that either evaluate to True or False. For example, “I have a
twin sister” is a condition. If I do have a twin sister, then this condition is True, otherwise
it is False.
In the program above the condition is “The current date is 2/10/2017” or in Small Basic:
Clock.Date = "2/10/2017"
Page |2
If the current date is 2/10/2017 Then we want the program to say Happy Birthday.
We use the If/Then keywords to accomplish this.
The keyword If will take a condition and evaluate it to True or False. You use Then to
specify the instructions you want executed if the condition is True. If the condition is
False, then the program will skip to the next line and execute those instructions. EndIf
tells the program to perform the following instruction regardless of whether the
condition was True or False.
What is the condition in this program? “If the current month is February (2) AND the
current day is the 10th.” Or, in Small Basic:
Compare this with the following program that will let me know if it’s the weekend:
Notice that here the two sub-conditions are combined using a different logical operator:
Or. If we use the Or keyword, then as long as at least one of the sub-conditions is True,
then the entire condition is True. If it’s Saturday, then it’s the weekend and likewise if it’s
Sunday, it’s the weekend.
Page |3
Let’s look at a more complicated example where you can specify an action if a condition
is false:
In this example we specify an action to take if the condition, input > 0, is satisfied and
a different action to take if another condition, input < 0, is satisfied. This code will
work just fine, but there’s a better way to write this program using a new keyword, Else.
This keyword allows us to specify an action to take if the condition is False. In this
program the user will input a number and the condition “input is greater than zero” will
be evaluated. What do you think will happen if you input zero?
But what if we want to be a bit more selective with our program? Here’s one more
example that’s even more involved:
Page |4
ElseIf temperature < 50 Then
TextWindow.WriteLine ("It's chilly today")
ElseIf temperature < 80 Then
TextWindow.WriteLine ("It's nice outside today")
Else
TextWindow.WriteLine ("It's really hot today")
EndIf
What this program does is print out different messages for different ranges of
temperatures. For a temperature between 32 and 50 degrees, it will print “It’s chilly
today” or for temperatures between 50 and 80 it will print “It’s nice outside today.” We
can accomplish this with the introduction of the keyword ElseIf. This will allow us to
nest if statements together. For example, the first few conditions above could also be
written as:
Notice that with this approach we end up with multiple If/EndIf blocks. With the first
approach our code has less repetition and is much cleaner.
Greet the user and ask them what operation they want to perform
(addition, subtraction, multiplication, or division)
Save this operation as a variable
Remember, Ask them what the first operand (number) is
you can use Ask them what the second operand is
comments
to help you
write the Page |5
code!
Set your conditional statements for how the system should address
each type of operation (which is now a variable)
Give the user the answer
Hint: Make sure you are using TextWindow.ReadNumber() to read in the operands,
that way the computer knows the input is a number instead of text, and you can add
them. The difference is 2 + 2 = 4 (the computer thinks they are numbers and computes
the mathematical result) vs. “2” + “2” = “22” (the computer thinks they are strings and
combines them into one larger string).
Discussion Questions
o Can you think of some more examples of using conditional logic in real life?
o What are some limitations associated with conditional logic? Can you think of
something that would involve long or complicated conditionals?
o What can you do if you want to have a set of conditions be analyzed only if
another condition is true?
o If the first condition of an And logical operator is False, do you need to analyze
the other parts of a complex conditional?
o If the first condition of an Or logical operator is True, do you need to analyze the
other parts of a complex conditional?
Page |6
Additional Resources
Small Basic Tutorial: Conditions and Loops
o https://aka.ms/sbcurriculum1.4
Video: If then, elseif, and else
o https://youtu.be/q80byQV_qww
Logical operator short circuiting
o https://aka.ms/logicaloperator
Page |7