The Objective-c control statements have the following three categories:
Branching statements: If-else,switch
Looping statements: While,do-while,for
Jump statement related to program transfer: Break,continue,goto.
Branch statements
Branching statements provide a control mechanism that allows the execution of a program to skip execution of certain statements and to execute specific statements.
1. Conditional statement: If-else
2. Multi-branch statement: switch
Usage: the section in switch (expression) {//"[]" can be omitted.
Case Value1:statement1;
Break
.....
Case VALUEN:STATEMENTN;
Break
[Default:defaultstatement;]
}
Attention:
2.1. The return value type of an expression must be an integer or a type that can be automatically converted to an integer. It cannot be a float and a double type.
The value Valuen in the 2.2.case clause must be a constant, and the values in all case clauses should be different. The default clause is optional. The break statement is used to cause the program to jump out of the switch statement after executing a case branch, terminating the switch statement execution. In some special cases, multiple different case values perform a set of identical operations, which can be done without a break.
Looping statements
1.while statements
2.do-while statements (less used, mostly while statements)
3.for statements
The For statement is in the following form:
for (initialization;termination (termination condition); Iteration (iteration)) {
body;//Loop Body
}
Jump statements related to program transfer
1.break and continue statements are mainly related to the loop, goto statement in C, it is unconditional jump can completely replace the break and continue statements, generally use caution.
2.break statements
The break statement was used in switch to terminate the execution of the switch statement.
The break statement can also be used in the loop body, terminating the current loop (jumping out of the inner loop) and executing at the first statement immediately following the loop block.
3.continue statements
The continue statement is used to end the loop, skipping statements that have not yet been executed in the loop body, and then judging the termination condition to determine whether to continue the loop.
4.goto statements
The goto statement is an unconditional jump that can completely replace break and continue. If it is a nested loop, you can use the Goto statement to specify the label to change the program's flow. Such as:
for (int j = 0; J <; J + +) {
for (int i = 0; i < ++i) {
if (i = = 3) {
Goto label;
}
NSLog (@ "i =%i", i);
}
}
Label
NSLog (@ "SDFSDF");
The output is:
Note: Do not jump back, otherwise the structure will become very chaotic. Can call back, but now our technology can't control.
Objective-c Control Statements