Computer Science Grade 5 20 min

If Statements

If Statements

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Write an `if-else if-else` statement to handle three or more possible outcomes. Create a nested `if` statement to check a second condition inside a first one. Combine two conditions in a single `if` statement using the `AND` operator. Combine two conditions in a single `if` statement using the `OR` operator. Explain the flow of a program that uses nested or chained `if` statements. Debug a simple program that uses complex conditional logic. Ever played a video game where you need a key AND a special gem to open a treasure chest? 🗝️💎 How does the computer know to check for both items? In this lesson, we're going beyond simple `if-else` choices. You will learn how to make your programs check for many different conditions at once, creating smarter and...
2

Key Concepts & Vocabulary

TermDefinitionExample Else If (elif)A way to check a new condition if the first `if` condition was false. It lets you build a chain of checks.`if (score > 100) { print('Amazing!'); } else if (score > 50) { print('Good job!'); }` The second message only appears if the score is not greater than 100, but is greater than 50. Nested If StatementAn `if` statement that is placed inside another `if` statement. The inner `if` only runs if the outer `if` is true.`if (isWeekend) { if (isSunny) { print('Go to the park!'); } }` The code only checks if it's sunny after it already knows it's the weekend. AND operatorA logical operator that combines two conditions. The entire statement is only true if BOTH individual conditions are true.`if (hasKey AND hasGem...
3

Core Syntax & Patterns

The `if-else if-else` Chain if (condition1) { // Code block 1 } else if (condition2) { // Code block 2 } else { // Code block 3 } Use this when you have more than two possibilities. The computer checks the conditions in order. As soon as it finds one that is true, it runs that code block and skips all the rest in the chain. The Nested `if` Structure if (outer_condition) { // This code runs if outer_condition is true if (inner_condition) { // This code runs only if BOTH outer and inner are true } } Use this when you need to check a second, more specific detail only after a first, broader condition is met. It's like asking a follow-up question. Compound Conditions with `AND` / `OR` if (conditionA AND conditionB) { ... } if (conditionA OR...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A programmer wants to label numbers: numbers greater than 10 are 'Large', and numbers greater than 5 (but not greater than 10) are 'Medium'. They write this code: `if (num > 5): print("Medium") elif (num > 10): print("Large")`. What is the logical flaw?
A.It will print 'Medium' for a number like 12, but it should print 'Large'.
B.It should use an `AND` operator to define the range for 'Medium'.
C.The `elif` should be a separate `if` statement.
D.There is no flaw, the code works correctly.
Challenging
Variables `x = 0` and `y = 0`. A loop runs for each number `i` from 1 to 5. Inside the loop is this code: `if (i is even): x = x + 1 else: y = y + i`. What are the final values of `x` and `y`? (Assume 'even' means divisible by 2 with no remainder).
A.`x` is 2, `y` is 9
B.`x` is 3, `y` is 6
C.`x` is 2, `y` is 6
D.`x` is 3, `y` is 9
Challenging
To win a prize, a player must either collect a 'Gold Key', OR collect both a 'Silver Key' AND a 'Bronze Key'. Which `if` statement correctly represents this rule?
A.if (hasGoldKey AND hasSilverKey OR hasBronzeKey)
B.if ((hasGoldKey OR hasSilverKey) AND hasBronzeKey)
C.if (hasGoldKey OR (hasSilverKey AND hasBronzeKey))
D.if (hasGoldKey AND hasSilverKey AND hasBronzeKey)

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.