Flow Control

IF, IF-ELSE, and ELSE-IF Conditional Test with Logical Operators

In the above examples, we used comparison operators (<, >, and ==).  We can also use logical operators with the if, if-else, and elif statements. Additionally, we can define compound test conditions involving comparison operators and logical operators (called Boolean).

Below is a program using the if-else condition test. The program uses the logical operator ‘AND’ with comparison operators.

Home Thermostat Example: if-else Conditional Test

Let us assume you feel comfortable if the home temperature is between 68°F and 73°F.  In this case, you do not need either the heating or the cooling to turn on.  If the temperature drops below 68°F, you would like to turn on the heating. On the other hand, if the home temperature goes above 73°F, you would like to turn on the cooling.

In this example, the if statement tests to see whether the home temperature is greater than 68°F AND is less than 73°F. (This is logical AND that we came across in the Operators and Operands chapter.) If the result of this test condition is true (yes), then the thermostat does not turn ON either heating or cooling. 

The code then tests if the temperature is below 68°F (comparison test if temp_home < 68 ).  In case the test result is true, the thermostat sends a command to the system to turn ON the heat.  On the other hand, if the temp_home < 68°F comparison test is false, the program checks if the temp_home is > 73°F. If the result of the test is true, the thermostat sends a command to the system to turn ON cooling.

Test if temp_home is Between 68°F and 73°F

Program 1: In the first part of the code, we initialize the variable “temp_home” to 70°F.  This part of the code tests if temp_home is between 68°F and 73°F. Since temp_home is greater than 68°F AND less than 73°F, the code takes the if branch and prints “Do not turn on the heating or air conditioning”. The thermostat control sends a command to the A/C system to not turn ON either heat or cooling. The code does not go to the else-if-elif branch, since the if condition was true.

Test if temp_home is Outside the 68°F and 73°F Range (Test Branch to Turn ON Cooling)

Program 2: To test the other branches of the code, let us now assume the temp_home is 75°F. Since temp_home of 75°F is outside the 68°F to 73°F range, the if test condition evaluates to false (no), so the program bypasses the if statement and executes the else: branch. In the else branch, the code first checks if the temp_home < 68°.  Since temp_home is 75°F, this test fails (false), the program takes the elif branch to test if the temp_home is greater than 73°F. This condition is true, so it prints ‘Turn on air-conditioning’ and sends a command to the air-conditioning system to start the cooling.

Test if temp_home is Outside the 68°F and 73°F Range (Test Branch to Turn ON Heating)

Program 3: Program-2 tests the branch of the code to test program branches to turn ON cooling. Now we test the branch of code to turn on heat by setting the temp_home to 60°F.  The flow of the code is very similar to the one in Program 2.

PROGRAM EXAMPLE: if, else-if STATEMENTS WITH LOGICAL OPERATORS
>>> # PROGRAM-1
>>> temp_home = 70				#1
>>> if temp_home > 68 and temp_home < 73:	#2
	print('Do not turn on heating or air conditioning')
else:						#3
	if temp_home < 68:
		print('Turn on heating')
	elif temp_home > 73:			#3a)
		print('Turn on air conditioning')

		
Do not turn on heating or air conditioning
>>> # Program 2.  Use the same program, but initialize temp_home to 75 degrees
>>> # to test if the program takes the air conditioning branch.
>>> temp_home = 75				#4
>>> if temp_home > 68 and temp_home < 73:
	print('Do not turn on heating or air conditioning')
else:	
	if temp_home < 68:
		print('Turn on heating')
	elif temp_home > 73:
		print('Turn on air conditioning') #5

		
Turn on air conditioning
>>> # Program 3: Use the same program, but assume it is a cold day and temp-home to 60 degrees
>>> # to test if the program takes the heating branch.
>>> temp_home = 60				#6
>>> if temp_home > 68 and temp_home < 73:
	print('Do not turn on heating or air conditioning')
else:
	if temp_home < 68:
		print('Turn on heating')	#7
	elif temp_home > 73:
		print('Turn on air conditioning')

		
Turn on heating
>>> 

Line-by-line explanation of the program:

1) Set the variable temp_home to 70°F, which is within the range of 68°F and 73°F. In step 2), the program tests if the code will give the command to NOT turn on the heat or cooling

2) The if statement checks if temp_home is within the 68°F and 73°F range using a logical AND operation.   If it is > 68°F AND < 73°F, it prints ‘Do not turn on heating or air conditioning’. In case the if test fails, the code branches to the else statement to step 3).

3) In the else branch, the if statement tests the condition that temp_home < 68°F.  In case this if test returns true, the code prints ‘Turn on heating’.  Else, if temp_home < 68°F test returns false, the code takes the elif branch (3a) and tests if temp_home > 73°F.  In case this test condition returns true, the program prints ‘Turn on air conditioning’ and sends a command to air-conditioning system to turn ON cooling.

4) PROGRAM 2: In this part of the code, we set the variable temp_home is 75°F to check if the code takes the correct branch to turn on air conditioning if the temp_home is 75°F.

5) The operation is the same as described in step 3 and step 3a).

6) PROGRAM 3: In this step, we initialize the variable temp_home to 60°F. This part of the code tests if the code will turn on the heating by taking the else-if branch.

7) The logic of the code is the same as in step 3.

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights