Conditional Execution: if Statements
if statement chooses one or another branch of code to execute based on the value of a condition. We can use an
or operation in a condition, for example if we’re asking the user a question and there are several ways he or she could provide an answer that we want to accept for one particular path or another.
So, if we look in this top function called
answer(), we’re going to ask the user a yes or no question. We’re going to allow that the user might type the word
yes or might just type the letter
y. We want both of those to be acceptable, so we’re going to produce a lowercase version of the string so that we don’t have additional
or statements, although that’s certainly possible.
So what we’re going to do is we’re going to see did they type, when converted to a lowercase string, the word
'yes'? Or did they type a
'y'? And we’ll allow both of those to indicate an affirmative response.
And then whatever action we want to take within this block, we can carry out. Here, we’re just using a simple print statement for illustrative purposes. Otherwise, we might want to check if they said
'no'. And again, we’ll allow two possible ways that they could say no—either they typed the word
no or they just typed the letter
n. I don’t know of other things we might anticipate a user typing, but if you can think of those, you can add additional
or operands and more expressions to check for either of these. But if they said
'n', in this particular function, we’re just going to take whatever action we want for a negative response, and here we’re just going to print out that they replied with a negative answer.
I can call that function again and just give a
y. And because it’s in the
or statement and that makes the second operation
True, the whole thing evaluates to
True and we record a positive answer.
n, because neither one of these is
'y', it goes to the
elif part, and now it’s checking to see if it was
'n'. And since it was
'n' and that makes the second
True, we go down to the negative answer. And if we respond in some other way,
maybe, there’s nothing to check for that
So, entering a number between
10 would not be useful, so we want them to enter a number less than
5 or a number more than
10. And that sentence that I just read used the word “or” in it, and that should also suggest that we would want an
or operation to control that logic.
And so here is another illustrative example that we want to determine if the user has typed a number inside or outside of the range from
40. And so if they type a number and that number is less than
20 or that number is greater than
40, we’ll observe that we are outside of our range, and otherwise we are inside of our range. And here the way we’ve defined these is that the endpoints are inside of the range.
Whether we use a less than (
<) or less than or equal to (
<=), greater than (
>) or greater than or equal to (
>=) in these operations is something specific to the actual problem and has nothing to do with whether the word
or is being used here.
Similarly, if I type in a larger number, say
80, that’s going to make the first inequality
False. So then we check the second condition, and
80 is greater than
40, and so the entire
or statement evaluates to be
True and we will get the result that we are outside of that range.
On the other hand, if I type a number that is in the range, say
35, the first condition evaluates to
35 is not less than
35 is also not greater than
40, and so this
or statement has the result
False, and so we go into the
else branch and our program is going to print
'Inside'. And one more, because we used
> and not
>= in the inequalities in the
if statement, if either one of the numbers is equal to
40, it’s still going to return
False, and we are inside that range.
Become a Member to join the conversation.