Objective-C control statement and objective-c statement
Objective-C control statements have the following three types:
Branch statement: if-else, switch
Loop statement: while, do-while,
Jump statements related to program transfer: break, continue, goto.
Branch statement
The branch statement provides a control mechanism that allows program execution to skip some statements without executing them, but instead execute specific statements.
1. Condition Statement: if-else
2. multi-branch statement: switch
Usage: the part in switch (expression) {// "[]" can be omitted.
Case value1: statement1;
Break;
.....
Case valueN: statementN;
Break;
[Default: defaultStatement;]
}
Note:
2. 1. The return value type of expression must be an integer or be automatically converted to an integer. But not float or double.
2.2.case the value valueN in the 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 exit the switch statement after a case Branch is executed, that is, to terminate the execution of the switch statement. In some special cases, a group of identical operations must be performed for multiple different case values. In this case, break is not required.
Loop statement
1. while statement
2. do-while statements (usually while statements)
3. for statement
The for statement is as follows:
For (initialization; termination (termination condition); iteration (iteration )){
Body; // the body of the loop.
}
Jump statements related to program Transfer
1. The break and continue statements are mainly related to loops. The goto statement is available in C. It is an unconditional jump that can completely replace the break and continue statements, and should be used with caution.
2. break statement
The break statement is used in the switch to terminate the execution of the switch statement.
The break statement can also be used to terminate the current loop (jumping out of the inner loop) in the loop body and run immediately at the first statement of the loop block.
3. continue statement
The continue statement is used to end this loop. It skips the unexecuted statements in the loop body and then judges the termination conditions to determine whether to continue the loop.
4. goto statement
The goto statement is an unconditional jump, which can completely replace break and continue. If it is a nested loop, you can use the goto statement to specify the tag to change the process of the program. For example:
For (int j = 0; j <10; j ++ ){
For (int I = 0; I <10; ++ I ){
If (I = 3 ){
Goto label;
}
NSLog (@ "I = % I", I );
}
}
NSLog (@ "SDFSDF ");
Output result:
Note: Do not jump back, otherwise the structure will become very messy. Callback is supported, but our technology cannot handle it now.