OBJECTIVE-C Study Notes (10)--use of circular statements for and Do-while

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.