views:

311

answers:

3

When running this code under debug in the iPhone simulator, it correctly exits with r==4 after 5 iterations of the loop:-

int r;
for (r = 0;;r++)
{
    if (r == 4)
        break;
}

Without the braces, it exits with r==1 after 1 iteration:-

int r;
for (r = 0;;r++)
    if (r == 4)
        break;

Without the braces, but with a test expression which is true, in place of the permanently true default for a missing test expression, it correctly exits with r==4 after 5 iterations of the loop:-

int r;
for (r = 0; r > -100;r++)
    if (r == 4)
        break;

Am I missing something here, or is the second example a compiler bug?

+2  A: 

I don't have the simulator handy, but if that's really what's happening, then yes, something is going wrong. Your first and second example are identical as far as the language is concerned. Are you quite sure your tests are accurate? That the code you're showing us above is exactly what you're running in the simulator?

Edit: OK I tried it, and it works correctly in the simulator here. My best guess for a solution to your problem is that you have:

for (r = 0;r++;)

By accident in the case you think is failing. Note the empty loop statement is moved from what you wrote above. This for loop would give you the result of 1.

Edit 2: After reading some other answers here, I thought I should check out the debugger. Take a look at this screenshot! image

If I press "step over" again, it goes back into the loop though; just a little graphical hiccup.

Carl Norum
Three semicolons in a `for` would give a compile-time syntax error.
Pavel Minaev
Oops! edit time. Thanks, made my own typo.
Carl Norum
+1 for providing a screenshot for what I tried to describe :)
Felix Kling
A: 

Looks like a compiler bug to me. What compiler do you have set to, gcc or llvm-gcc?

I just tested this with gcc 4.2.1 (Apple Inc. build 5646) and get the right result in both cases.

AlBlue
+1  A: 

I don't know if your question should be considered as duplicate but maybe this is the answer:

http://stackoverflow.com/questions/1782263/are-loops-with-and-without-parenthesis-handled-differently-in-c/1782281#1782281

The debugger executes one statement at a time.

I was a bit mistaken in my previous answer. Of course the debugger also executes the loop 4 times. The difference is , that it looks like he exits after one iteration. When I debug the following code:

#include <stdio.h>

int main (int argc, const char * argv[]) {
    int r;
    for (r = 0;;r++)
        if (r == 4)
            break;

    printf("%i",r);
    return 0;
}

the debugger processes the for line, then the if line, the for line again and then highlights the printf line. So it looks like printf is going to be executed but in the next step, the debugger highlights the if line again, then going to for, thereby increasing r and then highlighting printf again.

This is the code I tried in Xcode and it is basically the same behavior as described above (I just added some lines of code in main.m in a fresh Cocoa application project):

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    int r;
    for (r = 0;;r++)
        if (r == 4)
            break;
    return NSApplicationMain(argc,  (const char **) argv);
}

Picture with Cocoa application:

Debugger

So the question is, have you really clicked through the loop step by step and is the loop really exiting after one iteration, or have you just stopped after one iteration?

Felix Kling
I don't think that's the same question; the question you link to is about debugger stepping; there is no semantic difference in execution of the whole loop.
Carl Norum
@Carl Norum: I don't now what you mean with *semantic difference* but the OP also speaks about *debugging* his code. So I am pretty sure that at least the answer can be also applied here.
Felix Kling
I'm going to try it; but any debugger that changes the control flow of the program is bad news.
Carl Norum
I checked the Xcode debugger; there are some weird graphical sideeffects of the second example, but the code runs as expected. Probably it is compiler optimization that is confusing the OP with stepping through the loop.
Carl Norum
That snippet would be expected to print 4 under a regular C compiler, but my equivalent code breaks from the loop with r==1 under Xcode Objective-C on my iMac.
@Carl Norum: Yes I realized it too. Sorry for my previous misleading answer. I tried to describe what is happening so lets see what the OP says.
Felix Kling
@christobrier: Well I am trying also with Xcode and a fresh cocoa application project and it is as I described.
Felix Kling
@christobrier: Felix is right; you're missing a step or jumping to conclusions somewhere.
Carl Norum
OK, Felix is right, I hadn't really exited from the loop, it just looked as if I had. Thanks, guys.
Make sure you are compiling with no optimizations.
Nicolás