I was doing some programming in Cocoa Touch and Objective-C, and now I have a really frustrating problem. I have a method in which there is a for-loop. Yet, every time I run the app in the iOS Simulator, the code in the loop isn’t run, and it doesn’t stop on any breakpoints within the loop. At first, I thought it was just in the method, but it appears now that it happens anywhere in the code. No for-loops work anywhere in any methods. Here is an example of one of my loops, and if you see anything wrong, I would appreciate the help.
for (int i = 0; i == 3; i++) {
NSLog(@"This is a test.");
}
This might be something really dumb that I am missing, but I can’t see anything that could be causing this. If you need more code, just ask, and thanks in advance!
The condition on a for loop causes the loop to run as long as the condition is true.
In the example you cited, the loop will never execute because i started off equal to zero, with the test condition i == 3. Since i == 3 is immediately false, the loop does not run even once.
If your intention was to run the loop until i was three, then the test condition should be i < 3 making the entire for
Another way to think about this is that the loop will continue to run “while i < 3”.
I hope this helps.