+ 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?
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.
+ 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).
+ 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?