if starts a conditional statementin spoken language a conditional statement looks something
like:
“If my alarm goes off I will make it on time.”
It is assumed that if my alarm doesn’t go off I will not make it on
time.
Syntax:
#imaginary example relating the statement above
if alarm_goes_off: #TRUE or FALSE
will_be_on_time #if TRUE statement is executed; if FALSE statement is skipped
#what is happening
if what_to_evaluate: #semicolon
return output #indented
There must be a semicolon after what_to_do
and the next line must be indented
A few examples:
a = 10
b = 5
if a > b: #statement is TRUE
print('yes') #action is executed
## yes
if a == b: #statement is FALSE
print('yes') #action is skipped (not executed)
This is confusing, which statement produced the ‘yes’?
To clarify add else
else is executed if all other statements are false. It
has no conditionala = 10
b = 5
if a == b: #statement is FALSE
print('yes') #action is skipped (not executed)
else:
print('no')
## no
elif can only be used after an if
statement and can be used as many times as neededif what_to_evaluate: #semicolon here too
return output
elif evaluate_something_else: #semicolon here too
return different_output
elif Example:
letter = 'b'
if letter == 'a': #semicolon
print('Aardvark')
elif letter == 'b': #semicolon
print('Baboon')
elif letter == 'c': #semicolon
print('Caracal')
## Baboon