Who can explain the meaning of the While True loop in Python?

How can I understand what While True means: ...? I only realized that the loop will be infinite in this case until I write the break command, but what is the meaning of the expression While True? And is there a way out of such a loop without a break, for example if "something" becomes False :D

Maybe the question is stupid, but thanks to the one who answers :D

Author: Никита Смирнов, 2020-07-04

3 answers

A typical situation for using such a loop is when the completion condition becomes known only in the middle of the iteration.

For example, our program should read numbers from the console, and calculate their product, until 0{[8 is entered]}

total = 1

while True:
    x = int(input())

    if x == 0:
        break

    total *= x

print(total)

Of course, you can take the first input out of the loop, and transfer the completion check to the loop header.

total = 1
x = int(input())

while x != 0:
    total *= x
    x = int(input())

print(total)

But in this case, the program ceases to satisfy the so-called principle. DRY.

In python 3.8, there is a new construction that will allow you to get rid of while True in such cases

total = 1

while (x := int(input())) != 0:
    total *= x

print(total)

But this is also not an ideal solution, and it will come into use only in a year or two, when most systems will use 3.8.

In addition, do not forget that some cycles can be absolutely infinite, and in such cases, you can not do without while True

def count(x):
    while True:
        yield x
        x += 1


for z in count(1):
    for y in range(1, z + 1):
        for x in range(1, y + 1):
            if x**2 + y**2 == z**2:
                print(f'{x}**2 + {y}**2 == {z}**2')
 2
Author: extrn, 2020-07-04 23:30:57

This means that the cycle is constantly true. That is, while true= = true to perform. It is possible without a break, but then instead of True, you will need to use a boolean variable that will be equal to true. For example:

a = True
while a:
  #Цикл
   a = False

(I answer from the phone, Soryan)

 1
Author: Ycea, 2020-07-04 22:26:59

The person on the link answered ingeniously: this is the same as saying While (6>5), which is essentially While True. An infinite loop, for the truth is always true: D There, each line of the answer is very interesting and understandable, so I will attach his answer here, so that those who are interested can read it later:

Everything inside the () of the while statement is going to be evaluated as a boolean. Meaning it gets converted into either true or false.

Consider in the statement while(6 > 5)

It first evaluates the expression 6 > 5 which is true so is the same as saying while(true)

Anything that is not FALSE, 0, an emptry string "", null, or undefined is likely to be evaluated to true.

When I first started programming I used to do things like if(foo == true), I didn't realise that was virtually the same thing as if(foo).

So when you say while(true) its like are saying while(true == true)

So to answer you question: While TRUE is True.

 0
Author: Никита Смирнов, 2020-07-04 21:37:35