Process Control of the "IOS development" OBJECT-C syntax

Source: Internet
Author: User


1. If condition statement


If expression : expression is an integer or Boolean, 0 or FALSE, greater than 0 is TRUE;


code example :

/*************************************************************************    > File name:11-ifelse.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created time: 212/2 01:22:57 2014 ******************* /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {int a = 9;if (a >) {NSLog (@ "greater than 9");} else if (a >) {NSLog (@ "greater than 10");} Else{nslog (@ "Less than or equal to 10");} if (a) {NSLog (@ "Not 0 digits can also be true");}}}

Execution Results:

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 11-ifelse.m octopus-2:oc octopus$/a.out 2014-12-02 01:49:1 2.487 a.out[658:507] Less than or equal to 102014-12-02 01:49:12.490 a.out[658:507] not 0 digits can also be true



2. Switch Branch statement



switch Control expression : The control expression type in switch () qualifies char, short, int, long, long long.


code example :

-- Code :

/*************************************************************************    > File name:11-switch.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created time: 212/2 18:49:28 2014Switch Branch statement in switch Can only be a short char int long and long long type ************************************************************************/#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 Results :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 11-switch.m octopus-2:oc octopus$/a.out 2014-12-02 18:54:1 8.696 a.out[850:507] A type



3. Cyclic structure



Looping features :

-- initialization (init_statements) : Initializes the data used in the loop;

-- cyclic condition (test_expression) : Boolean expression that determines whether the loop body is executed;

-- Loop Body (body_statements) : Repeated execution of the content;

-- Iteration statement (iteration_statements) : changing the cyclic condition;



(1) while Loop


While loop format : First judge the test_expression value, if true, execute the loop body, otherwise execute the following statement;


init_statements;

while (test_expression)

{

body_statement;

iteration_statements;

}



(2) do and loop


Do while loop format : First executes the loop body, determines the loop condition, if the test_expression is true, executes the next cycle, otherwise terminates 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) Code example


Code :

/*************************************************************************    > File name:11-while.m    > Author:octopus    > Mail:octopus_truth.163.com     > Created time: 212/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: value of A is%d", a); a--;} Do While loop where a does not meet the condition, only executes the statement do{nslog (@ "Do While loop: A =%d", a);} while (a >);//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$./a.out 2014-12-02 20:47:14 .454 a.out[1021:507] While loop: The value of A is 32014-12-02 20:47:14.456 a.out[1021:507] While loop: The value of A is 22014-12-02 20:47:14.456 a . out[1021:507] While loop: The value of A is 12014-12-02 20:47:14.457 a.out[1021:507] Do While loop: a = 02014-12-02 20:47:14.457 a.out[  1021:507] for loop i = 02014-12-02 20:47:14.457 a.out[1021:507] for loop i = 12014-12-02 20:47:14.458 a.out[1021:507] for loop i = 22014-12-02 20:47:14.458 a.out[1021:507] for loop i = 32014-12-02 20:47:14.459 a.out[1021:507] for loop i = 4



4. Cycle Control


loop control :

- break: Exits when the layer loops;

-- Continue : Skips the cycle and executes the next loop;

- -return: Returns the function directly, no matter how many layers it has, and returns directly;


code example :

-- object-c code :

/************************************************************************* > File name:11-circlecontrol.m > A Uthor:octopus > Mail:octopus_truth.163.com > Created time: 312/3 00:40:44 2014 ************************** /#import <foundation/foundation.h>int Main (int argc, char * argv []) {@autoreleasepool {NSLog (@ "Break control:"),//break will jump out of the corresponding current level of the loop, if it is a nested loop, will only jump out of that layer of 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 this layer loop, executes i = 2"); 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, terminates this execution, executes i = 2 case"); 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 function directly, no longer execute the following statement"); re Turn 0;} NSLog (@ "i =%d, J =%d", I, J);}}}


-- Execution Results :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 11-circlecontrol.m octopus-2:oc octopus$./a.out 2014-12-03 01:06:35.669 a.out[1360:507] Break control: 2014-12-03 01:06:35.671 a.out[1360:507] i = 0, j = 02014-12-03 01:06:35.671 a.out[  1360:507] i = 0, j = 12014-12-03 01:06:35.672 a.out[1360:507] i = 1, j = 02014-12-03 01:06:35.672 a.out[1360:507] i = 1, j = 1 Interrupts this layer loop, executes the case of i = 2 2014-12-03 01:06:35.673 a.out[1360:507] i = 2, j = 02014-12-03 01:06:35.673 a.out[1360:507] i = 2 , j = 12014-12-03 01:06:35.674 a.out[1360:507] 2014-12-03 01:06:35.674 a.out[1360:507] Continue control: 2014-12-03 01:06:35.6 A.OUT[1360:507] i = 02014-12-03 01:06:35.675 a.out[1360:507] i = = 1, terminating this execution, performing i = 2 case 2014-12-03 01:06:35.675 a.out[1 360:507] i = 22014-12-03 01:06:35.676 a.out[1360:507] 2014-12-03 01:06:35.676 a.out[1360:507] return control: 2014-12-03 01:06 : 35.676 a.out[1360:507] i = 0, j = 02014-12-03 01:06:35.677 a.out[1360:507] i = 0, j = 12014-12-03 01:06:35.677 a.out[1360 : 507] i = 1, j = 02014-12-03 01:06:35.678 a.out[1360:507] i = = 1 && j = 1, Exit function directly, no longer execute the following statement 



5. Goto Statement



goto Usage :

-- define Tags : tag in any place of the program, e.g. "Start:";

-- Jump tag : use "goto tag;" To jump to the specified position;


Goto Common Scenario : jumps from the inner loop to the specified outer loop, or jumps out of multiple nested loops, and continues execution of the following statement;


code example :

-- object-c code :

/************************************************************************* > File name:11-goto.m > Author:oc Topus > Mail:octopus_truth.163.com > Created time: 312/3 01:09:55 2014 *********************************** /#import <foundation/foundation.h>int Main (int argc, char * argv[]) {@ Autoreleasepool {NSLog (@ "goto instead of Do While loop:"), int k = 0;circle:nslog (@ "k =%d", k++), if (K < 3) {goto circle;} NSLog (@ "\ n"); NSLog (@ "Goto jumps 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 (@ "This When i = = 1 && J = = 1 jumps out into the outer loop, executes i = 2 "); goto out;} NSLog (@ "i =%d, J =%d", I, j);} Out:nslog (@ "Memory loop execution Complete");} NSLog (@ "\ n"); NSLog (@ "Goto jumps out of all loops"), for (int i = 0; i < 3; i + +) {for (int j = 0; J < 2; J + +) {if (i = = 1 && j = = 1) {NSLog (@ "This When i = = 1 & j = = 1 jump out of all loops "); NSLog (@ "i =%d, J =%d", I, J);}}} Over:nslog (@ "all loops executed");}}


-- Execution Results :

Octopus-2:oc octopus$ clang-fobjc-arc-framework Foundation 11-goto.m octopus-2:oc octopus$./a.out 2014-12-03 01:26:36. 027 a.out[1475:507] Goto instead of Do While loop: 2014-12-03 01:26:36.028 a.out[1475:507] k = 02014-12-03 01:26:36.029 a.out[1475: 507] k = 12014-12-03 01:26:36.029 a.out[1475:507] k = 22014-12-03 01:26:36.029 a.out[1475:507] 2014-12-03 01:26:36.030 A.O  UT[1475:507] Goto jump out of this layer loop 2014-12-03 01:26:36.030 a.out[1475:507] i = 0, j = 02014-12-03 01:26:36.031 a.out[1475:507] i = 0, j = 12014-12-03 01:26:36.031 a.out[1475:507] Memory loop execution complete 2014-12-03 01:26:36.031 a.out[1475:507] i = 1, j = 02014-12-03 01:26 : 36.032 a.out[1475:507] At this time i = = 1 && j = = 1 jump out to the outer loop, perform i = 2 case 2014-12-03 01:26:36.032 a.out[1475:507] Memory loop execution completed 2 014-12-03 01:26:36.033 a.out[1475:507] i = 2, j = 02014-12-03 01:26:36.033 a.out[1475:507] i = 2, j = 12014-12-03 01:26:36 .033 a.out[1475:507] Memory loop execution completed 2014-12-03 01:26:36.034 a.out[1475:507] 2014-12-03 01:26:36.034 a.out[1475:507] Goto Jump out of all loops 2014-12-03 01:26:36.035 A.OUT[1475:507] At this time i = = 1 & j = = 1 jump out of all loops 2014-12-03 01:26:36.035 a.out[1475:507] i = 1, j = 12014-12-03 01:26:36.035 A.ou T[1475:507] All loops executed




Process Control of the "IOS development" OBJECT-C syntax

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.