OBJECTIVE-C Study Notes (eight)--advanced Jump statement Goto use method

Source: Internet
Author: User



When we learn C language, encountered a program jump, called Goto,goto can jump to any place in the program. Again later, learned the programming methodology, I do not know which computer predecessors (seemingly Dijkstra), that Goto makes the program jump too arbitrary, so that the logic of the code becomes chaotic, so it is not recommended to use Goto. Now comes OC, in OC also has Goto, in order to learn the integrity, we also come to learn this relatively advanced jump.



(a) Code one:







int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int a=0;
        
    start:{
        a++;
        NSLog(@"a=%d",a);
    }
        if (a<5) {
            goto start;
        }
        
    }
    return 0;
}

Output Result:







Results Analysis:



In fact, the program logic is still relatively clear, first start: As a lable, using {}, as a block of code, using Goto can jump to that place. The value of a is judged in the IF statement, and if a is less than 5, it continues to goto start. Result in a cyclic execution.






(b) Code two:







int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int a=0;
        
    start:{
        a++;
        NSLog(@"a=%d",a);
    }
        
        if (a<5) {
            goto start;
        }
        else{
            goto end;
        }
        
    end:{
        NSLog(@"a is greater than 5");
    }
        
    }
    return 0;
}

Output Result:









Results analysis: Goto can use different labels to jump to different blocks of code to achieve logical control of the program. This example jumps to start and end, respectively. Thus, loop statements can also be implemented using conditional judgment +goto statements.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



OBJECTIVE-C Study Notes (eight)--advanced Jump statement Goto use method


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.