Using and in Boolean while Loops
In this lesson, you’ll see an example of
and used in the condition for a
while loop. And as you’ll see, it’s another case of any time you have two expressions that you need to be true to continue executing, you’ll want to use an
00:16 In this example, taken from the tutorial, you see a section of code that deals with the control system for some environment. This segment looks at the pressure within that system and calls specific functions based on what that pressure is.
This segment is supposed to run until the pressure in the system is at or below
500 units, likely kilopascals. At those pressures, the system is considered safe and further checks aren’t necessary.
There are three
while loops in this program, but it’s the second one that has the
and operation in its condition. If the pressure gets a little too high, a function called
run_standard_safeties() needs to be used to try to reduce the pressure. The pressure range to use this function is from
So just as in the previous lesson, you need two expressions connected with the word
and to specify the exact condition necessary to use the standard safeties function For completeness, let’s briefly look at the other two loops.
The final loop is used when the pressure gets way high, over
700 kilopascals, where a more critical safeties function is used. And the first loop simply stops the repetition any time the pressure is considered safe.
In summary, the point of using
and is when you have two expressions that have to be true for either selection or repetition. Very often these involve ranges, but you’ll see in future lessons other situations.
Become a Member to join the conversation.