int main (int argc, const char * argv[]){
@autoreleasepool {
int x = 1;
for (x = 1; x <= 10; x++) {
NSLog(@"%i",x); //the answer here is 10.
}
NSLog(@"Number %i",x); //the answer here is 11.
}
return 0;
}
So my question is, why when I print ‘x’ outside the for loop it adds 1 to the initial 10?
thanks in advance.
The loop ends once x is greater than 10. Therefore, it goes through the loop 10 times, adds one, which is 11 and breaks out of the loop.