Python Forum
Do break operators turn while loop conditions from True to False?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do break operators turn while loop conditions from True to False?
#1
Check out this very basic while loop in Python:

while True:
    response = input("Say something: ")
    if response == 'bye':
        break
    if response != 'bye':
        continue    
This script prompts the user for input. If the user would like to say hello (or really just about anything), the loop will continually prompt the user for a new response until the user finally enters: "bye". If the answer is "bye", the loop breaks.

My million dollar question for all of you: For the purpose of while loops in general, is it safe to say that the break operator turns a while condition from True to False, therefore exiting the loop?

I realize that lines 5 and 6 really aren't necessary because even without them the loop would continue to perform as I described above. I added the continue operator to demonstrate the distinction between break (turning the while condition to False) and 'continue' (allowing the while condition to remain True).

In case I need to refer back to the source or origin of this question, I got this idea from Andrei Neagoie's Udemy course content for "Complete Python Developer in 2019" (Section 4: Lecture 71: "While Loops 2").
Reply
#2
(Oct-20-2019, 03:22 AM)Drone4four Wrote: My million dollar question for all of you: For the purpose of while loops in general, is it safe to say that the break operator turns a while condition from True to False, therefore exiting the loop?
No.
It breaks out of the loop in which it is found. It does not change the conditional from any perspective. Trying to explain it that way will just confuse everyone involved.
Reply
#3
(Oct-20-2019, 03:22 AM)Drone4four Wrote:
while True:
    response = input("Say something: ")
    if response == 'bye':
        break
    if response != 'bye':
        continue    
[ ... ] is it safe to say that the break operator turns a while condition from True to False, therefore exiting the loop?
[ ... ] break (turning the while condition to False) [ ... ]

(Oct-20-2019, 03:28 AM)Mekire Wrote: No.
It breaks out of the loop in which it is found. It does not change the conditional from any perspective. Trying to explain it that way will just confuse everyone involved.
Hi!

I agree with Mekire. The loop breaks because the program reaches the line 'break' and executes it, not because as you might think, it has changed a previous condition. The previous condition remains True. The only thing that has happened is that the program reaches a line inside the loop, and executes it. It's like if you change the 'break' line to:
print(f"This is still inside the 'True' loop. This is still the condition being 'True'.")
It reminds me of words and maths games, where while applying logic, they try to trick the listener (or reader).

Let's consider a mathematical fallacy. A mathematically correct result derived by incorrect lines of reasoning. Such arguments (it doesn't matter if the final result is true), are mathematically invalid:
16/64 = 16/64 = 1/4

The 16/64 has been simplified to 1/4, not because you have crossed out the number '6' from the numerator and denominator of the fraction, but because you have divided both terms of the fraction by the number '16'.

https://en.wikipedia.org/wiki/Mathematical_fallacy

So, while you might get inclined to think that the reason for breaking the infinite 'True' loop, is because it has stopped being 'True', that reasoning is wrong, because, the loop continues being 'True', and as it is, it reads one line inside of it and executes it, be this line a command to finish (break), be this line any other command, like printing something.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#4
Mekire answered to your question. Just to add it's easy to check and you can (and should have) did it yourself:
spam = True
while spam:
    break
print(spam)
Output:
True
while True:
    print('before break')
    break
    print('after break')
print('break after while loop')
Output:
before break break after while loop
as you can see it exit the loop immediately at executing break statement, not finishing the current loop iteration, nor reaching while to evaluate supposedly changed condition.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Firstly, @newbieAuggie2019: I appreciate the time and care you put into answering my initial question. I really like your angle with the reference to the Mathematical fallacy on Wikipedia because, as a matter of fact, I did my undergrad in Philosophy with a specialty in rhetoric. Rhetoric means that my primary focus was on writing prose while managing and avoiding invocation of logical fallacies. There are literally hundreds of named fallacies and I had never encountered like the Mathematical one that you refer to. I enjoyed reading the Wikipedia entry. I now see my error in reasoning with the way the state or truth proposition of while loops remain unchanged with a break statement. Thanks for breaking it all down and making it relevant to my question about while loops.

@Mekire and @buran: Thank you for your patience with my novice question and for clarifying that the break statement exit loops without impacting a while condition.

I would have liked to have replied sooner but today right now is my first opportunity. Sorry for the delay.
Reply
#6
(Oct-24-2019, 06:26 PM)Drone4four Wrote: [ ... ] @newbieAuggie2019: [ ... ] Thanks [ ... ]

You're welcome! Wink
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 487 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 946 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,737 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  Mixing Boolean and comparison operators Mark17 3 1,404 Jul-11-2022, 02:20 AM
Last Post: perfringo
  break out of for loop? User3000 3 1,439 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,678 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  tkinter control break a while loop samtal 0 2,387 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 3,977 Feb-17-2021, 01:03 AM
Last Post: tester_V
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,311 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  While True loop help Nickd12 2 1,987 Oct-17-2020, 08:12 AM
Last Post: Nickd12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020