Which one is faster ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Which one is faster ?

while (1) Or while (true) ? (C++)

30th Mar 2021, 10:31 AM
Mani_K_A
15 Answers
- 11
Arsenic If you make a game then there is 3 steps First is skeleton means backbone of game Second is looping Third is events The second step is done by loops and main loop is "while True :" loop If you don't know just check on the google......
30th Mar 2021, 12:07 PM
Tirthen Patel
Tirthen Patel - avatar
+ 9
Mani_K_A Please mention the language because in some languages while(1) will give error.
30th Mar 2021, 11:47 AM
A͢J
A͢J - avatar
+ 8
Both works one and same there is no difference in 1 and true. The difference is in the statements inside the loop. If the statements are less it will execute faster otherwise it will be little bit slow.
30th Mar 2021, 11:31 AM
Aysha
Aysha - avatar
+ 8
There is no difference in while (1) and while (true) . In some languages 1 and 0 are also consider as Boolean value. Performance depends on many factors like too many if else, unnecessary condition, unnecessary loops, unused variable, unnecessary objects, database connection is open but didn't close and many more.
30th Mar 2021, 3:15 PM
A͢J
A͢J - avatar
+ 7
Aerith I didn't got your point. How does one can't use multiple loops and/or function calls inside the other loop ? And what is a "make game's loop" ? Never heard that term before.
30th Mar 2021, 11:20 AM
Arsenic
Arsenic - avatar
+ 5
Loop performance more likely depend on how "heavy" the workload put inside the loop body rather than the loop condition evaluation. For those two examples, there is no complexity in condition evaluation, I suppose.
30th Mar 2021, 11:11 AM
Ipang
+ 3
Continue...... Real time example: One of my colleagues used two loop of same collection for different purpose so if there thousands records are coming from database then think where should take 10 minutes for a single operation will take 10 minutes more.
30th Mar 2021, 3:35 PM
A͢J
A͢J - avatar
+ 2
Aerith Arsenic certainly knows and doesn't need to check on Google. You on the other hand, need to be less 'confidently incorrect'. Firstly, the OP never mentioned games. Why are talking about game loops here? Game loops are technically the same as any other loops, and there are zillions of scenarios where seemingly infinte while loops are used, not just games. Secondly, 1 is the same as true in some languages and a truthy value in others. So while(1) in place of while(true) is totally valid in most languages, and while( (bool) 1 ) will work fine in most others. Mani_K_A I can't sat for sure that while(1) and while(true) aren't different of performance, but I can say that in most cases, the performance difference won't really matter much. I still suggest you use while(true) as personally I feel that it is more self-explanatory, but you can decide yourself. As Ipang said, loop performance depends more on what's inside the loop.
30th Mar 2021, 12:44 PM
XXX
XXX - avatar
+ 2
some languages while (1) will be give error
31st Mar 2021, 1:59 PM
John
+ 1
Both are same
31st Mar 2021, 6:13 PM
Yawar Abbas
Yawar Abbas - avatar
- 1
The best way to solve this would be to create a variable such as boolean isRunning = true; Then when you make your loop while(isRunning) will always evaluate to true, and this with inside the loop if you ever want to exit you can evaluate isRunning = false; then you will leave the loop. To answer your question, they will all be the same speed unless you use a different type of format or loop.
31st Mar 2021, 5:10 PM
Joshua Grisewood
Joshua Grisewood - avatar
- 1
The two loops are exactly the samy, because your (good) compailer will replace true into 1 (like constexpr)
31st Mar 2021, 6:33 PM
Sebastian Ahlborn
Sebastian Ahlborn - avatar
- 3
While(True) loop is faster because if you want to take all the things one by one and you can also put more loops and function in it........ Also while True loop is used in make game's loop....
30th Mar 2021, 11:17 AM
Tirthen Patel
Tirthen Patel - avatar
- 3
And you can stop the while True loop whereas conditional while loop stops when the condition gets false....
30th Mar 2021, 11:18 AM
Tirthen Patel
Tirthen Patel - avatar
- 3
Thanks for giving my answer best
30th Mar 2021, 12:34 PM
Tirthen Patel
Tirthen Patel - avatar