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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Answering from a C perspective here:
No,
;does not translate into a no-op instruction. No-op instructions (such asnop) 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 aforloop 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:
so that I could write my infinite loops as:
I wouldn’t do that nowadays of course.
(a) A “true” empty statement along the lines of:
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.