A few days ago I saw that for ( ; ; )
results in an infinite loop. That made me wonder about two things.
- Is the empty statement ( ; ) no-op in assembler
- Why is it evaluated as "true" in the for example given above?
A few days ago I saw that for ( ; ; )
results in an infinite loop. That made me wonder about two things.
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).
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.