0% found this document useful (0 votes)
6 views47 pages

9-Conditionals

programing conditional

Uploaded by

lawrencesadje02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views47 pages

9-Conditionals

programing conditional

Uploaded by

lawrencesadje02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

CPROG1: Conditionals

Mary Jezza M. Saballa-Marin


Chapter Topics

• If-Then • If-Then-Else Conditionals in Python


• If-Then-Else • If-Then-Else-If Conditionals in
• If-Then-Else-If Python

• Multiple Else-If
• Conditionals Recap
• If-Then Conditionals in Python
Objectives

By the end of this chapter, student will be able to:


• Describe the structure of different types of conditional statements and their usage
with mathematical, relational, and boolean operators;
• Implement different types of conditional statements with operators and analyze the
effect of conditionals on the scope of a variable;
• Write a script using conditional statements to control the turtle with user input.
Introduction

• The term “conditional” comes from the idea that sometimes we want to
run some code conditionally; in other words, we only want to run it if
something is true.
If-Then

• The most fundamental form of conditional is the simple if-then statement.


If something is true, then do something.
• You check if some condition is true, and if so, you take some action. The
“action” could actually be several actions.
If-Then-Else

• A slightly more complicated version includes a third part: an else.


• The else is a different series of actions to perform if the condition wasn’t
true in the first place.
• With an if-then-else structure, you’ll always do one thing or the other.
• The important thing here is that if-else structures create two alternatives,
one of which will always be
chosen.
If-Then-Else-If

• Like an else, an else-if only runs if the original if-then did not.
• Unlike an else, however, an else-if has its own conditions to check; if the
conditions aren’t met, it doesn’t run either.
Multiple Else-Ifs

• We must start with if, and we can have at most one else, but we can have
any number of else-ifs in between.
• Each else-if and else will only execute if no previous condition has executed.
Multiple Else-Ifs

• If we wanted to guarantee we check multiple things, we would just put


multiple if-then structures one after the other. But if the second statement
doesn’t begin with else, this could end up doing all the statements within.
• Just as we didn’t have to end an if-then statement with an else, we also
don’t have to end an if-then-else-if with an else. We can have an if-then-
else-if without having a final else.
Conditionals Recap

• Our basic conditional structure is the if-then structure; it checks if some


condition is true, and runs some code if so.
• We can augment our if-then structure with else-if and else.
• else-if checks additional conditions if the earlier ones were false.
• else always performs some actions if no previous if or else-if was true.
If-Then Conditionals in Python

• What is the output if the todaysWeather didn’t equal “raining”?


If-Then Conditionals in Python

• Take note of the colon symbol line 6. A colon (:) is Python’s sign that an
indented code block is beginning.
• The indented line of codes means that it is “under” or “controlled by” the
conditionals statement on line 6.
• The indented code is the “then” code; it’s the code that runs if the
conditional is true.
• Anything indented directly under that conditional statement will be
controlled by that statement.
If-Then Conditionals in Python

• Line of codes that are not indented are not controlled by the conditional.
• Lines that are not indented will be run regardless of the result of the
conditional.
• This is our fundamental if statement: the word if, some logical statement
that resolves to True or False, a colon, and some indented code.
If-Then-Else Conditionals in Python

• The else code runs if the conditional statement was False.


• The keyword else must be at the same level of indentation as the original if;
this is what tells Python which else corresponds to which if
If-Then-Else Conditionals in Python

• Logically, this makes sense: the else block code runs if the if block code did
not run; if else was part of the if block code (that is, indented under it), it
wouldn’t run either!
• else must also be followed by a colon, Python’s sign that an indented code
block is beginning.
If-Then-Else Conditionals in Python

What happens if the first if statement was true?

• Then the code under the if statement runs.


• The else code block only runs if the if code block did not run.
• The final line that prints “Done!,” however, lies outside either code block, so
it runs whether the conditional will result to either True or False.
If-Then-Else-If-Else Conditionals in Python

• Notice in between the if and the else, a keyword: elif.


• This is Python’s keyword for else-if. Other than the “el” at the beginning, it
perfectly matches the original if.
If-Then-Else-If-Else Conditionals in Python

• The only necessity for an elif statement is that it must come after an if and
before any else at that level of indentation.
• We can have more than one elif.
If-Then-Else-If-Else Conditionals in Python
If-Then-Else-If-Else Conditionals in Python

• The else-if will not run if the first if runs. Even if the condition in the elif
statement is true, it will not run if he first if ran. The else-if only runs as an
alternative to the preceding conditionals.
• All the unindented if statement’s conditional will be run because it is not an
elif for the other statements.
• Only use else-if if you want the conditional to be skipped if a previous part
of the structure was true.
Conditionals and Operators

• Conditional statements usually use logical expressions built around logical


operators to decide what to do.
• Sometimes we might store the result of a logical expression in a boolean,
and simply use that boolean inside the conditional instead of the expression
itself.
• Conditionals are often used with logical expressions in some way.
Relational and Mathematical Operators

• The equality operator (whether used mathematically or more generally with


strings), it is common to use the other relational and mathematical
operators with conditionals.
Boolean Functions and Operators

• Functions can return Booleans as well, which means we can use functions in
conditionals.
• Boolean operators allow us to take other operators and functions and
combine them into far more complex conditionals.
Boolean Functions and Operators

• We can check multiple different conditions, or multiple combinations of


conditions.
• We could have very complex statements, although in practice we generally
want to break complex conditionals down into multiple, simpler, nested
conditionals.
• Boolean operators would let us check either of those conditions within a
single line
Relational Operators

• The greater-than-or-equal-to operator returns True if the first number is


greater than or equal to the second, False if it is not.
Relational and Mathematical Operators

• Mathematical operators return other numbers, so they can’t be used on


their own in a conditional.
• However, we can use mathematical operators along with relational
operators.
Set Membership Operators

• You might remember that one of the things that makes Python unique is
easy access to functions that check if something is a member of another set.
• We could instead create lists, and easily check if the condition can be found
on the list.
• Python’s syntax is accessible enough that you might understand this just
based on the natural meaning of the word “in”
Boolean Functions

• If a function returns a boolean, then we can use it in a conditional


statement.
• In Python, there is a function (well, technically a method, but don’t worry
about the difference for now) called isdigit() that returns True if the string
represents a number, False if it does not.
Boolean Functions

• For now, just know that myNumericString.isdigit() returns True if myNumericString


is a number, False if it is not; and, any string can use .isdigit() the same way
• If you’re curious, there are similar methods for checking if a string is all letters
(.isalpha()), all letters or numbers (.isalnum()), all lowercase (.islower()), all
uppercase (.isupper()), or all whitespace (.isspace()).
Boolean Operators

• Our boolean operators—and, or, and not—can be used to combine any of


these logical expressions together.
• Note the syntax here: to check if one or the other is true, we simply put the
word or between the two logical expressions.
Boolean Operators
Boolean Operators
Boolean Operators

• Note the line 18 and 19. It ends in a slash, the next line is double-indented,
and the colon isn’t until after line 19.
• In Python, this is how we tell the computer, “Interpret these two lines as one
line.” Breaking the code between two lines makes it more readable for us as
humans, but the computer needs to see it as all one line.
• The slash says, “Copy the next line, and put it where this slash is.”
Boolean Operators

• Note that we put parentheses around this or expression on line 18 to force


the computer to evaluate it first.
• The computer will automatically evaluate logical operators from left to
right. However, it’s always good to use parentheses for human readability,
as well as for safety.
Nested Conditionals

• A nested conditional isn’t a special type of control structure like else-if or


else.
• Rather, it’s just one way of applying an existing control structure.
• If a conditional is true, it runs the code block that the conditional controls.
That code block can be anything we want it to be, which means that code
block can itself contain conditionals.
Nested Conditionals in Flowchart

• The above flowchart is flowchart of our previous sample problem.


• If all are true, we go one way; if one is false, we go a different way.
Nested Conditionals in Flowchart

• If one is True, we go on to the next decision; if one is False, we go to the


dedicated output for that decision.
• Each conditional governs whether we move on to the next conditional or
just exit.
• In some ways, this is similar to the else-if; however, where an else-if only
runs if the previous if was False, a nested if only runs if the previous if was
True because it’s part of the code block that only runs if the if statement was
True.
Nested Conditionals in Flowchart
Nested Conditionals in Python
Nested Conditionals in Python

• Ifs Within Ifs. Note that while one major benefit of nested conditionals is
that we can take care of more combinations of conditions, another benefit is
that in many ways, the code is more readable.
• With these nested conditionals, we no longer have to break one line of code
between two lines just for readability.
• Instead, we have three short, simple conditional statements, one under the
other.
Nested Conditionals in Python

• Each is indented under the previous one, meaning each only runs if the
previous one also ran.
• That means the purchase is only approved if the first conditional and the
second conditional and the third conditional are all True, which makes it
functionally equivalent to our original statement.
• However, with this structure, each individual conditional can have its own
dedicated else block, meaning we can print exactly why the purchase failed.
Ifs Within Elses

• This nesting applies on both sides of the structure as well.


• We can write code that is functionally equivalent to our last sample with a
completely different structure by nesting our conditionals in the else blocks
instead.
• The code to be shown can performs exactly the same, but all the nesting is
inside the else portions of the conditional.
• asdfb
Conditionals and Scope

• In Python, the scope of a variable starts when it is created, and ends when
one of a number of terminations happen.
• For now, the only termination you need to know is the program ending:
when the program ends and closes, the computer forgets the variables that
were created while it was running.
Accessing Variables within Conditionals

• Recall that the code above creates the variable result outside the
conditional.
• The scope of result is from line 4 until the program stops running.
• The scope of a variable is from the point at which it is created until the end
of the program.
Creating Variables within Conditionals

• If you create a variable within an if statement’s code block, but that code
block does not run. That means the variable was never created, and so if you
try to access the variable outside the code block, your code will crash.
Creating Variables within Conditionals

• The best way to resolve an error in variable scope, is to never create


variables inside a conditional that will need to be accessed outside the
conditional.
• If we really want to create a variable inside a conditional to use outside of it,
the least we can do is create it within each branch of the conditional,
including an else.

You might also like