3

I am looking at some example codes and I saw someone did this

for (;;) {
// ...
}

Is that equivalent to while(1) { } ?

And what does while(condition); do? I don't get the reason behind putting ';' instead of {}

PTN
  • 1,658
  • 5
  • 24
  • 54
  • The code is like `for(;;) { while(condition); }`. So those 2 are just pretty much one `while(condition) { }` loop? – PTN Aug 14 '15 at 23:05
  • 2
    `while(condition);` makes more sense when you consider than "condition" could include increments, decrements, function calls, etc... – Dmitri Aug 14 '15 at 23:08
  • yes the loop is like this `while((buff[n++] = getchar()) != '\n'); ` – PTN Aug 14 '15 at 23:11
  • If you have a question about cryptic code, it's best to quote the code literally. – m69's been on strike for years Aug 14 '15 at 23:16
  • 1
    @m69 ...or rather `for(;;) { condition; }`, which keeps going if `condition` is false. – Dmitri Aug 14 '15 at 23:16
  • 2
    @PTN In `while((buff[n++] = getchar()) != '\n');`, the condition itself does something, and its value will change as it goes becoming false eventually (it reads characters into a buffer until a newline is read, then the loop ends). – Dmitri Aug 14 '15 at 23:21
  • 1
    "*Is that equivalent to `while(1) { }`?*" -- Yes – Spikatrix Aug 15 '15 at 02:06
  • Please clarify whether you are asking about an empty loop , or a loop that contains some statements – M.M Aug 15 '15 at 03:35

4 Answers4

8

yes,

for(;;){}

is an infinite loop

orestisf
  • 1,396
  • 1
  • 15
  • 30
  • 2
    Note that this loop [causes undefined behaviour](http://stackoverflow.com/questions/2178115/are-compilers-allowed-to-eliminate-infinite-loops/) in C11. (In C99 it was unclear whether or not it did). `while(1) {}` does not cause undefined behaviour, so those two loop constructs are not the same. – M.M Aug 15 '15 at 03:31
8

And what does while(condition); do? I don't get the reason behind putting ';' instead of {}

Well, your question is what happens if you put or you do not put a semicolon after that while condition? The computer identifies the semicolon as an empty statement.

Try this:

#include<stdio.h>

int main(void){
    int a = 5, b = 10;

    if (a < b){
        printf("True");
    }


    while (a < b); /* infinite loop */
        printf("This print will never execute\n");

    return 0;
}
Michi
  • 5,175
  • 7
  • 33
  • 58
  • 1
    Right, the empty statement from the semicolon is the loop body. But sometimes that's intentional if the work is done in the *condition*. – Dmitri Aug 14 '15 at 23:29
  • 1
    The print may execute because `while (a < b);` causes undefined behaviour. [See here](http://stackoverflow.com/questions/2178115/are-compilers-allowed-to-eliminate-infinite-loops/) for further discussion – M.M Aug 15 '15 at 03:32
2

for(;;) and while(1) are both infinite loops, and compile to the same opcodes:

L2:
    jmp     L2

Which means there is no speed difference, as the disassembly is exactly the same.

  • 2
    Did you mean `for(;;){}` and `while(1){}` ? Assuming so, your assembly code is only one possible result, other systems may differ. – M.M Aug 15 '15 at 03:35
1

while just loops though a single statement until the condition is false. It doesn't have to be a compound statement (this thing: {}), it can be any statement. ; is a statement that does nothing.

while(getchar() != '\n');

will loop until you hit enter, for example. Though, this is bad practice since it will hog the thread; adding a call to a sleep method in the loop is better.

Bobby Sacamano
  • 540
  • 4
  • 15