[IOS development] Object, ios development object
1. if Condition Statement
If expression: The expression is an integer or boolean type. If the expression is 0 or FALSE, the value is FALSE. If the expression is greater than 0, the value is TRUE;
Sample Code:
/*************************************** * *********************************> File Name: 11-ifelse.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 2. 12/2 01:22:57 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {int a = 9; if (a> 20) {NSLog (@ "greater than 9 ");} else if (a> 20) {NSLog (@ "greater than 10");} else {NSLog (@ "less than or equal to 10");} if () {NSLog (@ "non-0 digits can also be TRUE ");}}}
Execution result:
Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 11-ifelse.m octopus-2: oc octopus $. /. out 01:49:12. 487. out [658: 507] is less than or equal to 102014-12-02 01:49:12. 490. out [658: 507] can be a non-0 Number or TRUE
2. switch branch statement
Switch Control expression: The control expression type in switch () is limited to char, short, int, long, long.
Sample Code:
--Code:
/*************************************** * *********************************> File Name: 11-switch.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/2 18:49:28 2014 Switch branch statement the switch can only be of the short char int long and long types ********************* **************************************** * **********/# import <Foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {char type = 'a'; switch (type) {case 'A ': NSLog (@ "A type"); break; case 'B': NSLog (@ "B type"); break; case 'C ': NSLog (@ "C type"); break; default: NSLog (@ "DEFAULT ");}}}
--Execution result:
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 11-switch.m octopus-2:oc octopus$ ./a.out 2014-12-02 18:54:18.696 a.out[850:507] A type
3. Loop Structure
Cycle Elements:
--Initialization (init_statements): Data used in the initialization cycle;
--Cyclic condition (test_expression): Boolean expression to determine whether to execute the loop body;
--Loop body (body_statements): Repeated execution content;
--Iteration Statement (iteration_statements): Change the cycle condition;
(1) while Loop
While loop format: First judge the value of test_expression. If it is TRUE, execute the loop body. Otherwise, execute the following statement;
Init_statements;
While (Test_expression)
{
Body_statement;
Iteration_statements;
}
(2) do while LOOP
Do while LOOP format: First execute the loop body to determine the loop condition. If test_expression is true, execute the next loop; otherwise, terminate the loop;
Init_statements;
Do
{
Body_statements;
Iteration_statements;
} While (Test_expression)
(3) for Loop
For Loop format:
For (Init_statements;Test_expression;Iteration_statements)
{
Body_statements;
}
(4) Sample Code
Code:
/*************************************** * *********************************> File Name: 11-while.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 2. 12/2 20:29:17 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {// while LOOP int a = 3; while (a> 0) {NSLog (@ "while loop: the value of a is % d", a); a --;} // do while loop where a does not meet the condition, only execute the do {NSLog (@ "do while loop: a = % d", a) ;}while (a> 100) Statement in the do statement ); // for Loop for (int I = 0; I <5; I ++) {NSLog (@ "for loop I = % d", I );}}}
Execution result:
Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 11-while.m octopus-2: oc octopus $. /. out 20:47:14. 454. out [1021:507] while loop: the value of a is 32014-12-02 20:47:14. 456. out [1021:507] while loop: the value of a is 22014-12-02 20:47:14. 456. out [1021:507] while loop: the value of a is 12014-12-02 20:47:14. 457. out [1021:507] do while loop: a = 02014-12-02 20:47:14. 457. out [1021:507] for loop I = 02014-12-02 20:47:14. 457. out [1021:507] for loop I = 12014-12-02 20:47:14. 458. out [1021:507] for loop I = 22014-12-02 20:47:14. 458. out [1021:507] for loop I = 32014-12-02 20:47:14. 459. out [1021:507] for loop I = 4
4. Loop Control
Loop Control:
--Break: Exit when layer loop;
--Continue: Skip this loop and execute the next loop;
--Return: Directly return the function, no matter how many layers there are, directly return;
Sample Code:
--Object-C code:
/*************************************** * *********************************> File Name: 11-circleControl.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 3. 12/3 00:40:44 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {NSLog (@ "break control:"); // The break jumps out of the corresponding first-level loop, if it is a nested loop, only the loop for (int I = 0; I <3; I ++) {for (int j = 0; j <2; j ++) {if (I = 1 & j = 1) {NSLog (@ "I = 1, j = 1) interrupts the cycle at this layer, execution I = 2 "); break;} NSLog (@" I = % d, j = % d ", I, j );}} NSLog (@ "\ n"); NSLog (@ "continue control:"); for (int I = 0; I <3; I ++) {if (I = 1) {NSLog (@ "I = 1, terminate this execution, execute I = 2"); continue ;} NSLog (@ "I = % d", I) ;}nslog (@ "\ n"); NSLog (@ "return Control:"); for (int I = 0; I <3; I ++) {for (int j = 0; j <2; j ++) {if (I = 1 & j = 1) {NSLog (@ "I = 1 & j = 1, exit the function directly, and no longer execute the following statement"); return 0 ;} NSLog (@ "I = % d, j = % d", I, j );}}}}
--Execution result:
Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 11-circleControl.m octopus-2: oc octopus $. /. out 01:06:35. 669. out [1360: 507] break control: 01:06:35. 671. out [1360: 507] I = 0, j = 02014-12-03 01:06:35. 671. out [1360: 507] I = 0, j = 12014-12-03 01:06:35. 672. out [1360: 507] I = 1, j = 02014-12-03 01:06:35. 672. out [1360: 507] I = 1, j = 1 interrupt the cycle of the current layer, and the execution of I = 2 is 01:06:35. 673. out [1360: 507] I = 2, j = 02014-12-03 01:06:35. 673. out [1360: 507] I = 2, j = 12014-12-03 01:06:35. 674. out [1360: 507] 01:06:35. 674. out [1360: 507] continue control: 01:06:35. 675. out [1360: 507] I = 02014-12-03 01:06:35. 675. out [1360: 507] I = 1, terminate this execution, execute I = 2 2014-12-03 01:06:35. 675. out [1360: 507] I = 22014-12-03 01:06:35. 676. out [1360: 507] 01:06:35. 676. out [1360: 507] return Control: 01:06:35. 676. out [1360: 507] I = 0, j = 02014-12-03 01:06:35. 677. out [1360: 507] I = 0, j = 12014-12-03 01:06:35. 677. out [1360: 507] I = 1, j = 02014-12-03 01:06:35. 678. out [1360: 507] I = 1 & j = 1. Exit the function directly and stop executing the following statement.
5. goto statement
Goto usage:
--Define tags: Tag any location of the program, for example, "start :";
--Jump tag: Use the "goto tag;" statement to jump to the specified position;
Common goto scenarios: Jump from the inner loop to the specified outer loop, or directly jump out of multiple nested loops, and continue to execute the following statement;
Sample Code:
--Object-C code:
/*************************************** * *********************************> File Name: 11-goto.m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 3. 12/3 01:09:55 2014 ************************************ ***********************************/# import <foundation/Foundation. h> int main (int argc, char * argv []) {@ autoreleasepool {NSLog (@ "goto replace do while loop:"); int k = 0; circle: NSLog (@ "k = % d", k ++); if (k <3) {goto circle;} NSLog (@ "\ n "); NSLog (@ "goto jump out of this layer loop"); for (int I = 0; I <3; I ++) {for (int j = 0; j <2; j ++) {if (I = 1 & j = 1) {NSLog (@ "at this time I = 1 & j = 1 jump out of the outer loop, execution I = 2 "); goto out;} NSLog (@" I = % d, j = % d ", I, j);} out: NSLog (@ "memory cyclic execution completed");} NSLog (@ "\ n"); NSLog (@ "goto jump out of all loops"); for (int I = 0; I <3; I ++) {for (int j = 0; j <2; j ++) {if (I = 1 & j = 1) {NSLog (@ "at this time I = 1 & j = 1 "); NSLog (@ "I = % d, j = % d", I, j) ;}}over: NSLog (@ "all loop executions completed ");}}
--Execution result:
Octopus-2: oc octopus $ clang-fobjc-arc-framework Foundation 11-goto.m octopus-2: oc octopus $. /. out 01:26:36. 027. out [1475: 507] goto instead of do while loop: 01:26:36. 028. out [1475: 507] k = 02014-12-03 01:26:36. 029. out [1475: 507] k = 12014-12-03 01:26:36. 029. out [1475: 507] k = 22014-12-03 01:26:36. 029. out [1475: 507] 01:26:36. 030. out [1475: 507] goto jumps out of this layer loop 01:26:36. 030. out [1475: 507] I = 0, j = 02014-12-03 01:26:36. 031. out [1475: 507] I = 0, j = 12014-12-03 01:26:36. 031. out [1475: 507] memory loop execution completed 01:26:36. 031. out [1475: 507] I = 1, j = 02014-12-03 01:26:36. 032. out [1475: 507] at this time, I = 1 & j = 1 jumped out of the outer loop and executed I = 2 at 01:26:36. 032. out [1475: 507] memory loop execution completed 01:26:36. 033. out [1475: 507] I = 2, j = 02014-12-03 01:26:36. 033. out [1475: 507] I = 2, j = 12014-12-03 01:26:36. 033. out [1475: 507] memory loop execution completed 01:26:36. 034. out [1475: 507] 01:26:36. 034. out [1475: 507] goto jump out of all loops 01:26:36. 035. out [1475: 507] at this time I = 1 & j = 1 jump out of all loops 01:26:36. 035. out [1475: 507] I = 1, j = 12014-12-03 01:26:36. 035. out [1475: 507] All loops have been executed