Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Well, that depends. Some software needs to have infinite loops because it is running continuously and should never stop. Imagine software that is monitoring a

Well, that depends. Some software needs to have infinite loops because it is running continuously and should never stop. Imagine software that is monitoring a patients vital signs, a nucelar power plant control system, or a program that senses and reports the weather at a number of airports. Those programs have loops that are infinite, because they should never stop. In fact, if they do stop, it is an error that a human should be alerted to.
The way to deliberately cause an infinite loop is to use a condition that will never become false, and then break out of the loop when something extremely unusual happens, using the break statement - something like the following:
while True:
# do the thing that should continue forever
# such as checking temperature and water pressure
# log the temperature
# log the pressure
if temp > temp_threshold or pressure > pressure_threshold:
break;
# if we ever get here it's a problem
# sound alarm
print("ALARM! The loop should have never stopped!")
print("All personnel evacuate immediately!")
In this class you will never need to deliberately write an infinite loop, and you never should.
Out in the wild you may see ordinary software that has infinite loops with while True: where the loop is broken from inside the body of the loop with a break statement instead of putting the stopping condition in the while statement where it should be. This is poor programming practice when the loop is not intended to run forever.
The reason it is poor practice is because it hides the stopping condition inside the body of the loop, and remember, when there are several ways to do something, the most important feature to consider is understandability for a reader, not shortcuts for the software writer.
Examine the code below, and without running it, try to determine what it does. What will be printed? How many times will something print? When will it stop?
import random
while True:
rand_num = random.randint(1,101)
if rand_num >75:
print("Getting out of the loop")
break
else:
print(rand_num)
Now run it with the Try It button and use the Visualizer to see where the control flows.
Code Visualizer
TRY IT
Now look at this code and without running it, read and answer the same questions. What does it do? What will be printed? How many times? When does it stop?
import random
rand_num = random.randint(1,101)
while rand_num <=75:
print(rand_num)
rand_num = random.randint(1,101)
print("Got out of the loop")
You can also run this code and use the visulaizer, and you can see that they do exactly the same thing. But think about which code made it easier to determine what the loop is supposed to do, and when it will stop. Imagine what would each loop look like if the body of the loop contained 30,50, or 100 more statements. The fact that the loop stopping condition is buried in the body of the loop makes it a bad practice.
Inexperienced programmers sometimes think shorter code is better.and dont consider the future of the software once theyre done with it. Again, you should never deliberately use an infinite loop in this class. If you can not write your loops without while True: and break, you do not understand while loops. If you bring code with these constructs to the helproom, the TAs will tell you to rewrite it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions

Question

Why do you think little babies are more susceptible to cold?

Answered: 1 week ago