In the following Objective-C code, when first inner ‘if’ statement is satisfied (true), does that mean the loop terminates and go to the next statement?
Also, when it returns to the inner ‘for’ statement after executing once, does the value of p is again 2, why?
// Program to generate a table of prime numbers
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int p, d, isPrime;
for ( p = 2; p <= 50; ++p ) {
isPrime = 1;
for ( d = 2; d < p; ++d )
if (p % d == 0)
isPrime = 0;
if ( isPrime != 0 )
NSLog (@”%i ", p);
}
[pool drain];
return 0;
}
Thanks in advance.
Your code is equivilant to this:
The contents of
ifandforcontrol statements is the next statement or statement block in braces.As daveoncode said, you really should use braces.