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