View as PDF

COMPUTING AND DIGITAL LITERACY

Conditional Statements

Difficulty level: Easy

Computing and Digital Literacy

This document was made by AI and there may be mistakes. If a question or answer looks wrong, use the Flag a mistake button to let Brett know so he can fix it.

Overview of the concept

This is how a program or an app does different things in different situations. It shows one screen if you are logged in and another if you are not, or warns you if your battery is low. Conditional statements are the if-then decisions inside every app, and they are how software responds to what is actually happening.

Rule

An if statement runs its block only when its condition is True.

When the condition is False, the if block is skipped and the else block runs instead. Once one branch runs, the rest are skipped.

Worked examples

Example 1

score = 62. If score >= 50, the program prints 'Pass', else it prints 'Fail'. What prints?

Solution

score = 62

if score >= 50:

print("Pass")

else:

print("Fail") -> Output: Pass

Test the condition: is 62 >= 50? Yes, True.

Because the test is True, run the block under if and print Pass.

Skip the else block entirely; else runs only when the test is False.

Example 2

score = 85. If score >= 80, the program prints 'A', elif score >= 50, it prints 'Pass', else it prints 'Fail'. What prints?

Solution

score = 85

if score >= 80: print("A")

elif score >= 50: print("Pass")

else: print("Fail") -> Output: A

Test the first condition: is 85 >= 80? Yes, True, so print A.

Stop there: once one branch runs, later elif and else branches are skipped.

If the first test had been False, the program would have tried the elif test next.

Practice questions

1

Score is 45, pass mark 50. What prints if score >= 50 print Pass else print Fail?

2

Score is 70, pass mark 50. What prints if score >= 50 print Pass else print Fail?

3

Age is 16. What prints if age >= 18 print Adult else print Child?

4

Age is 21. What prints if age >= 18 print Adult else print Child?

5

In example 2, score 65 prints what?

6

In example 2, score 30 prints what?

7

if score >= 90 print A elif score >= 60 print B else print C. Score 75 prints what?

8

if score >= 90 print A elif score >= 60 print B else print C. Score 95 prints what?

9

if score >= 90 print A elif score >= 60 print B else print C. Score 40 prints what?

10

When does the else block run?

ANSWER KEY

1. Fail

2. Pass

3. Child

4. Adult

5. Pass

6. Fail

7. B

8. A

9. C

10. Only when the if condition is False