You have no curly braces!
You should be sure to include curly braces. For one, it prevents mistakes when you want to go back and add some logic into one of these if
statements in the future, and for two, since we're talking about Objective-C and you're almost certainly using Xcode, it makes the code collapsable.
I know, for some reason, that main
drops its opening curly brace to the next line. I don't know why, and usually, I could care less because honestly, we spend almost no time in main
when developing iOS/OSX projects, however... by default the @autoreleasepool
leaves its opening brace on the same line, and the autocomplete on for
statements do this as well.
It's a style thing, yes, but in Objective-C, we prefer the opening brace on the same line. And there's actually a good reason for this... If you notice in the left-hand margin of your code, just to the right of the line numbers, there are differing shades of gray. Clicking these shades of gray collapses different sections of your code.
Compare the difference of collapsing a for
loop with the brace on the same line versus the brace on it's own line. To me, the latter looks silly and it takes up an extra line!
So all in all this looks a bit better:
int main(int argc, const char* argv[]) {
@autoreleasepool {
for (int i = 1; i <= 100; ++i) {
if (i % 15 == 0) {
NSLog(@"FizzBuzz");
} else if (i % 3 == 0) {
NSLog(@"Fizz");
} else if (i % 5 == 0) {
NSLog(@"Buzz");
} else {
NSLog(@"%d", i);
}
}
}
}