Can anyone explain the usage of while(1)? I am a beginner in c++ & want to know when should I use while(1)? And how can I use it? Please give an example to explain. Thanks in advance.
Asked
Active
Viewed 1.7k times
-3
-
3When you want an infinite loop – Rakete1111 May 05 '16 at 17:43
-
Please tell me how to use it?? – Therese May 05 '16 at 17:44
-
It's an infinite loop, and you should practically never use it. When you are an expert C++ programmer, you will know when it's right to use it. – Rob K May 05 '16 at 17:45
-
1@Therese Well, it's often used for the main loop of embedded bare metal code. – πάντα ῥεῖ May 05 '16 at 17:53
-
2You can use it to loop indefinitely, but it does not necessarily mean it will loop "forever". You can have conditional statements in the loop that call break; to exit the loop when some event has occurred. – Ben May 05 '16 at 17:54
-
Can anyone give an example to understand more?! – Therese May 05 '16 at 17:58
-
1This question is not a duplicate of the question it was closed as a duplicate of. This question is asking when you should use `while(1)` at all. The linked question is asking which endless loop form you should use once you've decided to use an endless loop. They're two different decisions subject to completely different criteria, ad the other question is much more subjective than this one. – David Schwartz May 05 '16 at 18:05
-
1It's quite common for app-lifetime threads. Such threads are expected to run 'forever' and are only terminated by the OS upon process termination. – Martin James May 05 '16 at 18:23
3 Answers
4
When one wants to implement a loop that exits from some mid-point of the loop body, a typical implementation would be an "infinite" loop with a conditional break
(or return
, throw
etc.) somewhere in the middle of the loop's body.
How you express that "infinite" loop is a matter of your own preference. Some people would use
while (true) // same as while (1)
{
...
if (exit condition is true)
break;
...
}
Others would opt for
for (;;)
{
...
if (exit condition is true)
break;
...
}
I'd personally use
do
{
...
if (exit condition is true)
break;
...
} while (true);
Choose whatever looks best to you.

AnT stands with Russia
- 312,472
- 42
- 525
- 765
2
Well, for embedded bare metal code it's often written like this:
int main() {
while(1) {
handle_interrupts();
poll_sensors();
}
// If you come here some processor exception occurred
}

πάντα ῥεῖ
- 1
- 13
- 116
- 190
1
This is a basic usuage of an infinite loop:
while(1)
{
// several examples:
// keep running my motor
// keep checking temprature
// keep broadcasting messages
// keep listening on a channel
// etc
}
You can use an infinite loop but exit it once it meets a certain condition. Example:
while(1)
{
// keep asking user to insert some string inputs
//..
//but if he enters "exit", "break" out of the loop
if(UserInput == "exit")
break;
}
Also know that the below are all considered the same:
while(1)
, while(1==1)
, while(true)

Khalil Khalaf
- 9,259
- 11
- 62
- 104