In OC, in addition to the while loop, there are additional for loops and do-while loops, which have different functions under different business logic. Can be compared with the C language and Java learning.
(a) Code one:
int main (int argc, const char * argv[]) { @autoreleasepool {for (int i = 0; i < 5; i++) { NSLog (@ "Hello, i =% D ", i); } } return 0;}
Output Result:
。
Result Analysis: The loop variable for the for loop is placed inside for (), and the individual variables for the for loop are variable definitions, variable ranges, variable values. The general for loop is used under conditions where the number of cycles is already clear. The output is in line with our expectations.
(b) Code two:
int main (int argc, const char * argv[]) { @autoreleasepool { int i = 0; do{ i++; NSLog (@ "Hello, i=%d", i); } while (i<5); } return 0;}
Output Result:
。
Results analysis: The difference between do-while and other loops is that Do-while is the code that loops inside the loop first, and then the loop condition is judged. The loop condition is met and then cycled. The while loop and for loop are the first to determine if the loop condition is satisfied, and the loop code is not executed if it is not satisfied. If the Do-while loop is not satisfied, it will execute at least one loop body. That's the big difference. Take a look at the next code:
(c) Code three:
int main (int argc, const char * argv[]) { @autoreleasepool { int i = 0; do{ i++; NSLog (@ "Hello, i=%d", i); } while (false); } return 0;}
Output Result:
。
Result Analysis: The loop condition is false, but a loop body is also executed. You can choose a loop type based on your business needs.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
OBJECTIVE-C Study Notes (10)--use of circular statements for and Do-while