When I run this code, it iterates, but then returns “The answer is 0”, when it should be “The answer is 10.”
Why is this?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int i;
for (int i = 0; i < 12; i++){
if (i % 3 == 0) {
continue;
}
printf("Checking i = %d\n", i);
if (i + 90 == i * i) {
break;
}
}
printf("The answer is %d.\n", i);
return 0;
}
You have two separate
is in your code, the inner one hiding the outer one. Moreover, the print statement causes undefined behaviour, because the outeriis read uninitialized.Say
for (i = 0; i < 12; i++)to use the outer variable instead of declaring a new variable.