ItP2 - Conditionals Flashcards
A conditional statement is one that allows us to make..
The programme can change what it does - (2)
a decision when the program is running.
next depending on what is happening now.
A conditional statement allow the program to execute different blocks of code depending on whether
a condition is true or false.
The conditional statement is expressed in Python using key word
if
The conditional statement is expressed using the keyword if.
In other words,
if is used to start a conditional statement in Python
In Python, ‘if’ keyword can be extended with
else and elif
‘else’ keyword is used in conjunction with
‘if’
‘else’ provides an
alternative block of code to execute when the condition specified with if is false.
If you mentally expand elif to else if, these statements almost translate to
what they would mean if you read them out in natural language.
If you mentally expand elif to else if, these statements almost translate to what they would mean if you read them out in natural language.
e.g. ‘elif’ means
“else if”
‘elif’ allows you to check- (3)
for multiple conditions sequentially after an initial if statement.
If the condition associated with if is false, it checks the condition associated with elif and executes the corresponding block of code if true.
You can have multiple elif statements.
Write a piece of that that:
variable ‘a’ is storing number 10
Create if statement where if a > 10 it prints:
I’m in the first ‘if’ block
a is greater than 10
Create another if statement that if a is 10 then print
I’m in the second ‘if’ block
a is equal to 10
Create another if statement that if a is less than 10 then prints
I’m in the third ‘if’ block
a is less than 10
What is output of code below if a is 10?
What do we see on line 1?
On line 1, we set a variable a to the value 10.
What do we see on line 3? - (2)
On line 3, we ask the question “is a greater than 10”?
This is represented by the conditional if and the comparison a > 10 which returns a bool of False
Explain this code - (4)
a = 10: This line assigns the value 10 to the variable a.
if a > 10:: This line starts the first if statement. It checks if the value of a is greater than 10. If this condition is true, the code block below it is executed. However, since a is equal to 10, this condition is false, and the code block inside this if statement is skipped.
if a == 10:: This line starts the second if statement. It checks if the value of a is equal to 10. Since a is indeed equal to 10, this condition is true, and the code block below it is executed. The statements inside this if block are printed.
if a < 10:: This line starts the third if statement. It checks if the value of a is less than 10. Since a is not less than 10, this condition is false, and the code block inside this if statement is skipped.
‘if’ statement in Python evaluates - (2)
a condition, which can be any expression that results in a boolean value (True or False).
This condition determines whether the subsequent code block associated with the if statement is executed.
The general form of ‘if’ statement in Python - (2)
if CONDITION.
CONDITION can be anything which evaluates to a bool - i.e. True or False.
The general form of if is: if CONDITION. CONDITION can be anything which evaluates to a bool - i.e. True or False. Here we are - (2)
we are comparing numbers
e.g., ‘if a > 10’: this condition evaluates whether value of ‘a’ is greater than 10
What do we find with line 3 of code?
a is not greater than 10.
Lines 4 and 5 of code below are…
We note that lines 4 and 5 are indented:
As with for loops, blocks of statements which are inside an if need to be
indented
Because a is not greater than 10, we do not run…
we do not run lines 4 and 5; instead, we skip straight down to line 7.
On line 7, we check whether a is equal to 10 (a == 10: try it in a console and you will get False). Because this is
False, we jump to the last block…
Since we skip to last block,
We then continue down to line 11 where we check whether a is less than 10. It is! So we go into the block and
execute the code there.
ften we use if statements to check for a ‘special’ condition. For example, - (2)
e normally want our code to do one thing but there is a special case where it should do some other thing.
Here, the else statement becomes useful:
Example image of else statement used in conjunction with if CONDITION
You do not have to have a
else statement (i.e., with if statement or in any loops like for and while)
If you do use an ‘else’ statement then it means:
“if nothing else matched, run this code”.
The else statement, if it exists, must always be the final part of the
if block
What is output of this code? - (2)
Test subject with age 9999 ignored
The total of all the real ages is 155
A final keyword that comes handy in ‘if’ statements is
‘elif’ statements
Mentally expand ‘elif’ to
‘else if’
‘elif’ statement is a neat way of stringing together lots of
statements in a row
Write a code:
storing number ‘66’ into variable called subjectAge
if age of pp is greater than 65 it prints: “Age is too big”
if age of pp is less than 18 then print “Age too small!”
Otherwise, it would print subject age okay!
Explain this code - (4)
This code checks the value of the variable subjectAge.
If subjectAge is greater than 65, it prints “Age too big!”.
If subjectAge is less than 18, it prints “Age too small!”. Otherwise, if neither condition is met, it prints “Subject age okay!”.
The elif statement allows for checking another condition within the same block as the initial if statement.
What is output of this code? - (6)
Output: Age too big!
This code evaluates the value of the variable subjectAge, which is initially set to 66.
Since subjectAge is greater than 65, it prints “Age too big!”.
The elif statement checks for additional conditions, but they are not met because subjectAge does not fall below 18.
Hence, it doesn’t print “Age too small!”.
Finally, the else block is bypassed, and the code prints “Subject age okay!”.
The combination of if, elif and else provides us with
the flexibility to make decisions and have our code adapt to the data.
You can nest multiple ‘if’ statements nested inside each other to produce
more complex conditions
Write a code that
variable subjectAge stores number 19
numberofFunctional Eyes variable is equal to 1
Code checks if subject age is greater than 18 years old (YNiC , only allowed adults over ages of 18) and has 2 functional eyes then prints : Subject is valid
Else print right age, wrong number of eyes
Otherwise, print under age, did not check eyes
What would be output?