Watch the video and take notes, pay attention to how if statements are used to alter the execution path of a program while running.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
== Equal to 1 == 1 is True "a" == "A" is False
!= Not equal to 1 != 2 is True "a" != "a" is False
> More than 2 > 1 is True "a" > "b" is False
>= More than or equal to 2 >= 1 is True "a" >= "b" is False
< Less than 1 < 2 is True "b" < "a" is False
<= Less than or equal to 2 <= 2 is True "b" <= "a" is False
# Code example (if statement SELECTION)
colour = str(input("Enter a colour :")) # user inputs a colour
if colour == "blue": # if statement checks colour to see if content is equal to "blue"
print("The sea is blue") # True then this additional indented code runs
if colour == "green": # if statement checks colour to see if content is equal to "green"
print("The grass is green") # True then this additional indented code runs
if colour == "red": # if statement checks colour to see if content is equal to "red"
print("The sun red") # True then this additional indented code runs
# REMEMBER a computer has to check all if statements one after the other in the order they are written
Apply your knowledge and use an if statement to alter the path of a program if the conditional statement is True.
Write a program that will ask the user for two integer numbers, add the numbers together and if the number is equal to 10 then add 100 to the total before printing it on the screen.
Input two numbers from a user.
Add both numbers together and create a total.
if total is equal to 10 then add 100 to the total.
output the total on the screen.
num1 = int(input("Enter a first number :"))
num2 = int(input("Enter second number :"))
total = num1 + num2
if total == 10:
total = total + 100
print("The total is ",total)
Describe how an if statement alters the path of a programs execution.
Describe how indents used in an if statement.
Describe the process a computer undertakes when it encounters an if statement.
Give 5 conditional expressions that would evaluate True when checked by the computer
Give 5 conditional expressions that would evaluate False when checked by the computer
Explain why "Bacon" == "Crispy" would evaluate as False when checked.
Describe how an if statement alters the path of a programs execution.
When the program comes to execute/run the if statement instruction if the conditional statement is True then one path is taken (indented code) if the conditional statement is False then the intended code is ignored.
If True the path taken has more instructions to execute/run.
Describe how indents used in an if statement.
Indents are used under the if statement as this shows the code that is to be executed/ran when the if statement is True. The indent shows the structure of the if statement and clearly shows the instructions that belong to the if statement.
Describe the process a computer undertakes when it encounters an if statement.
The computer executes/runs the if statement by looking at the conditional expression and evaluating the conditional statement. If the evaluation is True the computer moves to the indented code, if False then it ignores the indented code and continues executing/running the remainder of the program.
Give 5 conditional statements that would evaluate True when checked by the computer
See examples at top of page
Give 5 conditional statements that would evaluate False when checked by the computer
See examples at top of page
Explain why "Bacon" == "Crispy" would evaluate as False when checked.
"Bacon" is a string value due to using "quotation marks" and so is "Crispy". When just looking at the strings and not thinking about what they are or represent we can understand that the two words are clearly different so therefore not equal to each other.