Conditional Statements

Sometimes you want your program to behave differently only under certain conditions or to branch into different cases. For example,

if number_of_people > 0:
  payment_per_person = total_amount / number_of_people

if weather == "sunny":
  go_for_a_walk
elif weather == "rainy":
  read_a_book_at_home

In this way, programming has a mechanism to execute processing when certain conditions are met. This is called conditional branching.

Examples

Try the following examples to see how conditional statements work in practice.

  1. Set number_of_people = 5 and total_amount = 100 to see how the payment per person is calculated when number_of_people > 0.
  2. Set weather = "sunny" to see the program suggest going for a walk, and set weather = "rainy" to see the program suggest reading a book at home.