ItP2 - Conditionals Flashcards
(101 cards)
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.