Answered step by step
Verified Expert Solution
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 tempthreshold or pressure pressurethreshold:
break;
# if we ever get here it's a problem
# sound alarm
printALARM The loop should have never stopped!"
printAll 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:
randnum random.randint
if randnum :
printGetting out of the loop"
break
else:
printrandnum
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
randnum random.randint
while randnum :
printrandnum
randnum random.randint
printGot 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 or 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started