How to get our program to make decisions according to several conditions Pro Preview

You should already know how your program can make a decision based on a given condition, but sometimes, the decisions it has to make require more than one condition.

This can be done in several ways and we will look at how to do it using logical operators.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #1632
  • 20 Jun 2020

The "if" logical operator can contain code that includes more "if" logical operators. Here's what your program should look like if you check for conditions using nested logical operators:

if (first condition):
    if (second condition):
        (conditionally executed code)

If the first condition is not met, the nested "if" will not be checked at all. If the nested condition is not met, the conditionally executed code will not be executed. This way the code is executed when both conditions are met.

This method can be applied for more than two conditions.

Pay attention to the levels of code indentation. If both "if" operators are on the same indentation level, they won't be codependent:

if (first condition): 
if (second condition): 
    (conditional code)

This is an example of incorrectly written code.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Python with LEGO Mindstorms EV3 - Level 2
  • 39
  • 19:58
  • 93
Image for Lesson 3 - Crane movement control panel
  • 3
  • 4
  • 6
  • 3d_rotation 2