-2

I saw in a lots of example a form of use for the while loop that I'm not sure it's ok to use in a code.

 while(1){

    // code lines

    if(condition){
        break;
    }

    // code lines
}

Is this ok to use? what does

while(1)  

exactly mean?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Marco
  • 161
  • 8
  • 4
    Yes, thats ok to use. And while(1) is equivalent to while(true) and means the while loop does not stop until broken out of with break; – Magisch Feb 05 '16 at 21:16
  • No one stops you using that, but you better be sure that, that while becomes TRUE, if not, ...well you got yourself with an infinite LOOP. – Michi Feb 05 '16 at 21:49

6 Answers6

2

is this ok to use? what exactly

Yes, and its commonly used in programming.

while(1) is the same as while(true), basically a condition thats always true, ensuring the while loop never stops until you manually break out of it with break;

There are many applications for this behavior. It could be that the exit condition is very complex, requires function calls, etc. It could be that you just want a infinite loop for your program (for a menu for instance) that only exits on very specific prompts. Wether or not you use it often is often also a stylistic choice.

Magisch
  • 7,312
  • 9
  • 36
  • 52
1

while(1) is the same thing as for(;;) -- they both mean loop forever.

This is used when the exit condition is complicated, or isn't known at the top of the loop, or as an alternative to a do loop where the exit condition is at the very bottom.

Some loops don't even have an exit; imagine a daemon that never terminates or a signal or other interrupt-driven application where the loop just does something like...

while(1) {
  listen_and_run();
  // or
  sleep(10);
}
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1

In conditions=> '1' is always considered as TRUE and '0' as False. So,

while(1) {

}

Means the condition is true and it will loop forever as '1' is a constant value and is TRUE. The 'if' condition in your code could only break it out of the while loop. Similarly all assignments and all expression that evaluate to >0 are considered TRUE

Ex: while(3*12){}, while(a=b){}, while(9/3){} These would loop forever unless there is a condition inside the loop that breaks the loop.

Similarly, all expressions that evaluate to '0' or FALSE never enter the loop. Ex: while(a=0){}, while(0){}, while(5-5){} ,etc

Anurag
  • 651
  • 4
  • 18
0

while(1) is an endless cycle condition. But in your example, the cycle ends when the if (condition) comes true, because it is a break in it.

bemul12
  • 413
  • 2
  • 4
  • 13
0

while (1) continues forever until interrupted. You can also use it by itself, such as in the following example.

int main(){
 signal(SIGINT, SIG_DFL);
 while(1);
}

This program continues forever until you do ctrl + c.

ajfbiw.s
  • 401
  • 1
  • 8
  • 22
0

Simply an alternative to meeting the break condition in the while loop. while(1) is just the programmer forcing an always true statement into the while loop, making this while loop continue until the if condition is met. It's convention to put the break condition in the while loop, but not required.

mather
  • 11
  • 4