Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While True vs While False [duplicate]

Tags:

python

I understand that the following is an infinite loop (until you break out of it):

while True:

So then, what would be the need to ever use the following:

while False:

Or:

while not True:

Since the while loop only executes the code below if the while (condition): evaluates to the boolean True, is it correct to assume that the "code" show below would never execute?:

while False:
    code
like image 260
wildcard96 Avatar asked Feb 15 '26 00:02

wildcard96


1 Answers

while only executes the loop body if the expression evaluates to a true value.

As such, hardcoding the expression to False (or anything that always produces False or other false value), has no use other than to ensure the loop block is not executed.

like image 175
Martijn Pieters Avatar answered Feb 16 '26 12:02

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!