What for (;;) and while (); average value in C

I look at some code examples and I saw that someone did this

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

Is this equivalent to while(1) { } ?

And what does while(condition); ? I have no reason to put ';' instead of {}

+5
source share
4 answers

Yes,

 for(;;){} 

- endless cycle

+8
source

But for now (condition); make? I do not understand the reason laid by ';' instead of {}

Well, your question is what happens if you put or don't put a semicolon after that during a condition? The computer identifies the semicolon as an empty statement.

Try the following:

 #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; } 
+8
source

so far only loops, although one operator, until the condition is false. This should not be a compound statement (this thing: {}), it can be any statement .; it is a statement that does nothing.

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

will loop until you hit enter, for example. Although, this is bad practice, as it will lead the flow; adding a sleep method call in a loop is better.

+1
source

for(;;) and while(1) are both infinite loops, and compiled with the same opcode:

 L2: jmp L2 

This means that there is no difference in speed, since the disassembly is exactly the same.

0
source

All Articles