4 – More Branches

🎯 Learning Objectives

Develop the Programming and DevelopmentAlgorithms Learning Strands:

  • Use multi-branch selection (if, elif, else statements) to control the flow of program execution
  • Describe how iteration (while statements) controls the flow of program execution
💬 Key Vocabulary
  • Multi-branch selection
  • relational operators
  • logical (or Boolean) expressions
  • conditions
  • iteration
  • execution

🔎 More Branches

Last lesson, you…

  • Used selection (if statements) to control the flow of program execution
  • Introduced elements of randomness into your programs

In this lesson, you will…

  • Explore how selection can handle more than two possible branches
  • Use iteration (while statements) to allow the flow of program execution to include loops

📝 Starter Activity – Strange Weather

  • How would you extend this program to give advice to the user on how to dress depending on the weather?
  • Provide a rough outline of what your Python code would look like.
    • Give advice for when the weather is cloudy, rainy, or snowy.
    • In any other case, display a generic message.

Discuss in pairs and write your answer in the starter worksheet.

📖 Multi-branch selection in Python

  • These versions of selection check one condition and select one out of two branches to follow.
  • What if there are more than two cases in the problem?
  • What if there are multiple branches ?

Think of the weather example:
“Give advice for when the weather is cloudy, rainy, or snowy.”

  • Multi-branch selection (if, elif, else) checks successive conditions and selects one out of multiple branches to follow.
  • You will need an if block with elif blocks: when there are more than two mutually exclusive paths for your program to follow.
  • You can use multiple elif blocks.
  • The else and its block are optional.
  • You can nest anything inside the blocks of statements, even more if statements.

📝 Activity 1 – People in Space

  • Download this lesson’s worksheet to your Home Drive.
  • Follow the instructions to complete the Silver/Gold task using the Activity 1 – People in Space – Silver/Gold in the repl.it window below.
  • If you manage to complete the Silver/Gold task move onto the Platinum task below.

Silver/Gold Task

Platinum Task

📖 Something Missing?

  • This program greets the user by name.
  • What if…
  • we wanted to repeat the process until a specific name is entered?
  • Many of our programs have sections that could be repeated
  • e.g. checking the weather for multiple locations, and a number guessing game with many guesses.

📖 Iteration

  • When your programs repeat actions, checking for a terminating condition at the beginning of each new loop this is called a while loop.
  • You will need a while statement: when your program needs to repeat actions while a condition is satisfied.

In this lesson, you…

  • Explored how selection can handle more than two possible branches
  • Used iteration (while statements) to allow the flow of program execution to include loops

Next lesson, you will…

  • Use iteration (while statements) to allow the flow of program execution to include loops
  • Use Boolean variables, operators, and expressions
  • Use variables as counters and flags

🏅 Badge it

🥈 Silver Badge
  • Mostly complete the Silver/Gold Task 1 – People in Space but your code does not meet all the requirements or there is an error and upload a screenshot of your code to bournetolearn.com.
🥇 Gold Badge
  • Complete the Silver/Gold Task 1 – People in Space and your code meets all the requirements and has no errors and upload a screenshot of your code to bournetolearn.com.
🥉 Platinum Badge
  • Complete the Platinum Task – Temperature on the worksheet and upload a screenshot of your code to bournetolearn.com.