Loops in Python

While Loop

As mentioned in the previous section, Python provides another way to loop through a block of code. This is called the while loop.

Why Do We Need while Loop, If We Have the for Loop?

for-range() function works very well if you know the number of times you want a statement (or a block of code) to be repeated.

There are situations where you do not know ahead of time how many times you want a block of code to be repeated. Instead, you want the program to continue repeating the block of code until an event occurs.

Therefore, a method to break out of the loop is needed when the loop count is not known.

In such situations, the while loop functionality helps the programmer. The while loop is essential when we do not know how many times the program should loop.  The while statement stops looping through the code block (also called breaking out of the loop) based on the result of some test condition or a keyboard/mouse input, or an external sensor input such as a thermostat reading.

A Case Example

Let us suppose it is a cold wintry day; the temperature in your home is 70°F.  You feel comfortable at 75°F. You would like the furnace to heat the house to 75°F. So, you set the temperature on the thermostat control to 75°F.

The electronics in the thermostat work like the following:

The thermostat measures the temperature of your home; if it is less than the setting in the thermostat, it turns on the heat. 

Then, it continues to monitor the home temperature.  As long as the temperature in the home is less than 75°F, it continues the furnace to heat.

On subsequent measurements, if the temperature reaches the set temperature (75°F in our example) or goes above 75°F, the electronics in the thermostat send a control instruction to shut off the furnace. 

Thus, the program continues to loop a block of code waiting for an event occurs. In this example, the event is the home temperature reaching the set temperature.

Below is a detailed description of the control of the home heating system.

Thermostat Control Using While Loop

As indicated above, we use a while loop when we do not know how many times the program should loop. Control of a home heating system by a thermostat is an example of the application of the while loop.

The looping of a block of code continues until an event occurs. In response to an external event, the looping of the while loop is stopped. The external input in the example below is the temperature read by the temperature sensor in the thermostat.

While Loop Program Example – Variables Definition and Set-up

Let’s assume the temperature in the home is 70°F. We define a variable and call it “temp_home” to represent the home temperature. We assign temp_home a value of 70.

Let’s also assume that the homeowner likes to set the home temperature to 75°F. We define a variable and call it “temp_set” to represent the desired temperature for the home. We assign the variable temp_set = 75. This is the temperature that you set on the thermostat.

While Loop Program Operation

In the program code below, the while loop continues to compare the home temperature read by the temperature sensor to the desired temperature “temp_set” set on the thermostat.

As a result of the furnace heating the home, the home temperature will rise.

To emulate the heating by the furnace, we increase “temp_home” by one degree in each iteration of the while loop.

The thermostat temperature sensor continually reads the home temperature “home_temp”.

The program continues to compare the “temp_home” read by the temperature sensor against the “temp_set” in each while loop.

If “temp_home” < “temp_set”, the program prints the following message:

“The temperature is less than the set temperature. Keep the furnace on.”

The thermostat sends a command to the furnace to keep the heat ON.

After one loop, the program execution returns to the program’s while loop at the top of the program for another loop. 

This loop keeps on repeating until the home temperature (variable “temp_home”) is equal to or greater than the thermostat temperature setting (variable “temp_set”).

As long as this test condition “temp_home” < “temp_set” is true, the thermostat will keep on sending a command to the furnace to keep the heating ON.

When the home temperature is equal to or greater than the thermostat temperature setting (“temp_set”), the program breaks out of the while loop and takes the else branch. The program prints the new temperature and prints the following message.

“The temperature has reached the set temperature. Shutting down the furnace”.

The program also sends a command to the furnace to turn off the heat.

The flow chart below explains the operation of the while loop.

while statement flow chart for Loops in Python.
while Flowchart operation for Loops in Python
PROGRAM EXAMPLE: while LOOP – THERMOSTAT CONTROL
>>> temp_home = 70
>>> temp_set = 75
>>> while temp_home < temp_set:
	temp_home = temp_home + 1
	if temp_home <= temp_set:
		print(temp_home, '''The home temperature is less than the set temperature.
Keep the furnace ON.''')
else:
	print(temp_home, '''The home temperature has reached the set temperature.
Shutting down the furnace.''')

	
71 The temperature is less than set temperature. 
Keep the furnace on.
72 The temperature is less than set temperature. 
Keep the furnace on.
73 The temperature is less than set temperature. 
Keep the furnace on.
74 The temperature is less than set temperature. 
Keep the furnace on.
75 The temperature is less than set temperature. 
Keep the furnace on.
75 The temperature has reached set temperature. 
Shutting down the furnace
>>>

Note that this program combines the while loop with if-else conditional branches that we learned in the Flow Control chapter and Comparison Operators in the Operators and Operands Chapter.

We reviewed a few functions in Python Functions 1 chapter. In the next Python Functions 2 chapter, we will discuss python built-in functions in more detail.  We will also learn how you can define your functions to suit your individual application’s needs.  The functions that you write yourself are called User Defined Functions.

Copyright © 2019 – 2022 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights