While statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While statement

is "while true" a python statement or do i have to use the word "true" ?

11th Oct 2017, 5:49 PM
Aric Dunn
Aric Dunn - avatar
11 Answers
+ 2
Here is the wiki https://will.python.org/moin/WhileLoop It clearly states true, false and none are keywords in python3 as well as not( not in the wiki) It also gives an example on how using True as an argument can simplify your code.
11th Oct 2017, 7:22 PM
jason :)
+ 1
while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms. Note that most languages usually have some mechanism for breaking out of the loop early. for python u could use a break, or any statement that will produce the Boolean false at some point.
11th Oct 2017, 5:58 PM
jason :)
+ 1
True not true while 1==1 is okay too
11th Oct 2017, 6:00 PM
Oma Falk
Oma Falk - avatar
+ 1
ok great thanks.. apparently "true" has to be capitalized for it to work. maybe thats why it was not working
11th Oct 2017, 7:24 PM
Aric Dunn
Aric Dunn - avatar
0
so "while true" does something specific in python or would " while crap" do the same thing ?
11th Oct 2017, 6:07 PM
Aric Dunn
Aric Dunn - avatar
0
while {expression that evaluates to True or False} that means: while True while 1 while 2<3 while not False but you do not enter loop with: while False while 0 while 3>4 0 and 1 are special cases: 0 is a kind of False -- falsey 1 is a kind of True - truthy there are more special cases for truthy and falsey
11th Oct 2017, 6:14 PM
Oma Falk
Oma Falk - avatar
0
i guess im not clear... does the word "true" actually do something or is just a word to track what your doing ?
11th Oct 2017, 6:16 PM
Aric Dunn
Aric Dunn - avatar
0
True is a constant in python 3+2 evaluates to 5 3<5 evaluates to True You are right, it is a kind of magic: Normally while expects an expression to evaluate. for endless loops we do the work for the while loop and give directly the result. True is the easiest expression to evaluate: True is True
11th Oct 2017, 6:24 PM
Oma Falk
Oma Falk - avatar
0
ok.... i guess i get it.. thanks
11th Oct 2017, 6:35 PM
Aric Dunn
Aric Dunn - avatar
0
the code before can be hard, strong smelling and nasty, even wrong. True is True.
11th Oct 2017, 7:14 PM
Oma Falk
Oma Falk - avatar
0
It is not python-specific. one can find while true or similiar in many nearly all languages.
11th Oct 2017, 7:25 PM
Oma Falk
Oma Falk - avatar