GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Modul 8 von 13 · Text

3.1.1.7 The if-else statement: more conditional execution

David Khieu

The if-else statement: more conditional execution 

By using this form of conditional statement, we can describe our plans as follows:

if the_weather_is_good:
   go_for_a_walk()
else:
   go_to_a_theater()
have_lunch()

If the weather is good, we'll go for a walk. Otherwise, we'll go to a theatre. No matter if the weather is good or bad, we'll have lunch afterwards (after the walk or after going to the theatre).

Everything we've said about indentation works in the same manner inside the else branch:

if the_weather_is_good:
   go_for_a_walk()
   have_fun()
else:
    go_to_a_theater()
    enjoy_the_movie()
have_lunch()

Nested if-else statements

Now let's discuss two special cases of the conditional statement.

First, consider the case where the instruction placed after the if is another if

REad what we have planned for this Sunday. If the weather is fine, we'll go for a walk. If we find a nice restaurant, we'll have lunch there. Otherwise, we'll eat a sandwich. If the weather is poor, we'll go to the theater. If there are no tickets, ew'll go shopping in the nearest mall. 

Let's write the same in Python. consider the code here: 

if the_weather_is_good:
    if nice_restaurant_is_found:
         have_lunch()
   else:
         eat_a_sandwich()
else:
    if tickets_are_available:
         go_to_the_theater()
   else:
         go_shopping()

Here are two important points: 

  • this use of the if statement is known as nesting; remember that every else refers to the if which lies at the same indentation level: you need to know this to determine how the ifs and elses pair up
  • consider how the indentation improves readability, and makes the code easier to understand and trace. 

The elif statement

The second special case introduces another new Python keyword: elif As you probably suspect, it's a shorter form of else if.

elif is used to check more than just one condition, and to stop when the first statement which is true is found. 

Our next example resembles nesting, but the similarities are very slight. Again, we'll change our plans and express them as follows: if the weather is fine, we'll go for a walk, otherwise if we get tickets, we'll change our plans and express them as follows: If the weather is fine, we'll go for a walk, otherwise if we get tickets, we'll go to the theater, otherwise if there are free tables at the restaurant, we'll go for lunch; if all else fails, we'll return home and play chess. 

Have you noticed how many times we've used the word otherwise? This is the stage where the elfi keyword plays its role. 

Let's write the same scenario using Python: 

if the_weather_is_good:
   go_for_a_walk()
elif tickets_are_available:
   go_to_the_theater()
elif table_is_available:
   go_for_lunch()
else:
   play_chess_at_home()

The way to assemble subsequent if-elif-else statements is sometimes called a cascade.

Notice again how the indentation improves the readability of the code. 

Some additional attention has to be paid in this case:

  • you mustn't use else without a preceding if
  • else is always the last branch of the cascade, regardless of whether you've used elif or not
  • else is an optional part of the cascade, and may be omitted
  • if there is an else branch in the cascade, only one of all the branches is executed
  • if there is no else branch, it's possible that none of the available branches is executed. 

This may sound a little puzzling, but hopefully some simple examples will help shed more light. 

 

Anleitung

Um im Kurs weiterzukommen, sende einen Versuch dieses Quiz ab. Du kannst ihn nach der letzten Frage absenden.

Zum gewählten Modul

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck