5

A few days ago I saw that for ( ; ; ) results in an infinite loop. That made me wonder about two things.

  1. Is the empty statement ( ; ) no-op in assembler
  2. Why is it evaluated as "true" in the for example given above?
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
codd
  • 612
  • 1
  • 7
  • 15
  • `for` arguments are expressions, not statements. – SK-logic Aug 30 '11 at 08:07
  • you missed a ; at the end of the for :) – thumbmunkeys Aug 30 '11 at 08:08
  • @SK-logic: in my first point I was no longer talking about expressions but about the general case of the empty statement. Sorry for the confusion. x0r: I was just giving an idea, you could also say that I was missing a main function etc. Thanks for the notes in any way. – codd Aug 30 '11 at 08:36
  • @codd, you're missing the difference between statement and expression. Expression yields a value. Statement may (or may not) change the state, but does not return any value. In `for(A;B;C) S`, `A`, `B` and `C` are expressions with default value `1`, `S` is a statement and the whole `for ...` is a statement. – SK-logic Aug 30 '11 at 08:58
  • SK-logic: `f() { ; // this }` I was talking about the empty statement in general as showed in `f()`, have I used the word statement incorrect in this example? Am I correct in saying that in `f()` the empty statement is a statement but in the `for ( ; ; )` it is an expressions? – codd Aug 30 '11 at 09:22
  • @codd, you asked '*why is it evaluated as "true"*' - statement can't be evaluated into a value, only expressions can. See any C standard you can grab a hold of, like, e.g., par. 3.6.5.3 in ANSI C draft: http://flash-gordon.me.uk/ansi.c.txt – SK-logic Aug 30 '11 at 09:55

1 Answers1

6

Answering from a C perspective here:

No, ; does not translate into a no-op instruction. No-op instructions (such as nop) are explicit assembly level instructions which tend to actually do something (in that they consume time, though not necessarily affect any stored state within the CPU).

The for(;;) snippet is a for loop with defaults for each of the three sections. You can think of the ; in this case as not being an empty statement but a separator for the sections (a).

  • The first section (initialisation) has a default of "do nothing".
  • The second section is a condition under which the loop will continue. Its default is to continue forever.
  • The third section, the steps to take before beginning a subsequent iteration, is also "do nothing".

I have, in the past, been guilty of the heinous crime of using things like:

#define ever ;;
#define forever for (;;)

so that I could write my infinite loops as:

for(ever) { ... }
forever { ... }

I wouldn't do that nowadays of course.


(a) A "true" empty statement along the lines of:

if (condition) {
    a = b;
    ;
}

will also probably not translate to a no-op. More than likely it will not result in any code at all.

Keep in mind this is based on fairly common behaviour. In terms of C, ; can generate any lower level code it wants as long as it doesn't affect the "virtual machine" that is the C environment. It may, for example, increase a hidden line number variable and update coverage statistics if you have profiling enabled.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Just to augment this answer: [here](http://stackoverflow.com/questions/234906/whats-the-purpose-of-the-nop-opcode/235007#235007) is a nice list of other uses for the nop instruction. – Christian.K Aug 30 '11 at 08:16
  • `nop` will normally be only used as a placeholder for debugging. – SK-logic Aug 30 '11 at 09:04
  • I love that forever{...} construct! Although defines may well be a nightmare to debug :D – AnyOneElse Sep 05 '13 at 07:59
  • Visual Studio seem to have an [`__asm nop; extension`](http://stackoverflow.com/questions/10736275/is-there-asm-nop-equivalent-in-java), and GCC [`asm("nop")`](http://stackoverflow.com/questions/4567220/gcc-nops-being-compiled-away) – Ciro Santilli OurBigBook.com Mar 16 '15 at 10:15