+ 1

1==1

Is the purpose of the “1==1” bit purely to facilitate the “while” element of the code? And therefore should that be the way we always deal with a whole statement?

18th Feb 2018, 12:09 PM
Mohsin Khan
Mohsin Khan - avatar
3 Answers
+ 8
while (1==1) is equivalent to while (true) and this loop structure will run indefinitely, because the condition is always true. In normal cases, we will want to terminate the loop after an amount of cycles, e.g. while (n < 5) n += 1; I'm not sure if I properly understood your question, and addressed your doubts, though. The syntax for a while loop is: while (condition) { // do something } and the condition would be any boolean statement which can be evaluated.
18th Feb 2018, 12:16 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Whether or not we should would depend on what you want to do. Again, in normal cases, we don't want a loop to just run without an end. That said, if it happens to be the case, then yes. (while true is more commonly used in comparison to while 1==1, although the differences are subtle to none).
18th Feb 2018, 12:26 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Great thanks! I thought that was the case. So if we don’t have a terminating condition such as “while n<5”, we should always use “while 1==1:” or “while true:” in order to execute a loop?
18th Feb 2018, 12:21 PM
Mohsin Khan
Mohsin Khan - avatar