標籤:object-c
1. if 條件陳述式
if 運算式 : 運算式是一個 整型 或者 布爾型, 0 或者 FALSE 為 FALSE, 大於 0 為 TRUE;
程式碼範例 :
/************************************************************************* > File Name: 11-ifelse.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 二 12/ 2 01:22:57 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {int a = 9;if(a > 20){NSLog(@"大於9");}else if(a > 20){NSLog(@"大於10");}else{NSLog(@"小於等於10");}if(a){NSLog(@"非0數字也可以是TRUE");}}}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 11-ifelse.m octopus-2:oc octopus$ ./a.out 2014-12-02 01:49:12.487 a.out[658:507] 小於等於102014-12-02 01:49:12.490 a.out[658:507] 非0數字也可以是TRUE
2. switch 分支語句
switch 控製表達式 : switch() 中得控製表達式類型限定 char, short, int, long, long long .
程式碼範例 :
-- 代碼 :
/************************************************************************* > File Name: 11-switch.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 二 12/ 2 18:49:28 2014Switch 分支語句 switch 中只能是 short char int long 和 long long 類型 ************************************************************************/#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");}}}
-- 執行結果 :
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. 迴圈結構
迴圈要素 :
-- 初始化 (init_statements) : 初始化迴圈中用到的資料;
-- 迴圈條件 (test_expression) : boolean 運算式, 決定是否執行迴圈體;
-- 迴圈體 (body_statements) : 重複執行的內容;
-- 迭代語句 (iteration_statements) : 改變迴圈條件;
(1) while 迴圈
while 迴圈格式 : 先判斷 test_expression 值, 如果為 TRUE, 執行迴圈體, 否則執行下面的語句;
init_statements;
while(test_expression)
{
body_statement;
iteration_statements;
}
(2) do while 迴圈
do while 迴圈格式 : 先執行迴圈體, 判斷迴圈條件, 如果 test_expression 為真, 就執行下一次迴圈, 否則終止迴圈;
init_statements;
do
{
body_statements;
iteration_statements;
}while(test_expression)
(3) for 迴圈
for 迴圈格式 :
for(init_statements; test_expression; iteration_statements)
{
body_statements;
}
(4) 程式碼範例
代碼 :
/************************************************************************* > File Name: 11-while.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 二 12/ 2 20:29:17 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {//while 迴圈int a = 3;while(a > 0){NSLog(@"while 迴圈 : a 的值是 %d", a);a--;}//do while 迴圈 這裡 a 不符合條件, 只執行 do 中得語句do{NSLog(@"do while 迴圈 : a = %d", a);}while(a > 100);//for 迴圈for(int i = 0; i < 5; i ++){NSLog(@"for 迴圈 i = %d", i);}}}
執行結果 :
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 迴圈 : a 的值是 32014-12-02 20:47:14.456 a.out[1021:507] while 迴圈 : a 的值是 22014-12-02 20:47:14.456 a.out[1021:507] while 迴圈 : a 的值是 12014-12-02 20:47:14.457 a.out[1021:507] do while 迴圈 : a = 02014-12-02 20:47:14.457 a.out[1021:507] for 迴圈 i = 02014-12-02 20:47:14.457 a.out[1021:507] for 迴圈 i = 12014-12-02 20:47:14.458 a.out[1021:507] for 迴圈 i = 22014-12-02 20:47:14.458 a.out[1021:507] for 迴圈 i = 32014-12-02 20:47:14.459 a.out[1021:507] for 迴圈 i = 4
4. 迴圈控制
迴圈控制 :
-- break : 退出當層迴圈;
-- continue : 跳過該次迴圈, 執行下一次迴圈;
-- return : 直接返回函數, 不管有多少層, 直接返回;
程式碼範例 :
-- Object-C 代碼 :
/************************************************************************* > File Name: 11-circleControl.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 三 12/ 3 00:40:44 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {NSLog(@"break 控制 : ");//break 會 跳出 對應的當前一級的迴圈, 如果是嵌套迴圈, 只會跳出那一層迴圈for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j++){if(i == 1 && j == 1){NSLog(@"i = 1, j = 1 中斷本層迴圈, 執行 i = 2 的情況");break;}NSLog(@"i = %d, j = %d", i, j);}}NSLog(@"\n");NSLog(@"continue 控制 : ");for(int i = 0; i < 3; i ++){if(i == 1){NSLog(@"i == 1, 終止本次執行, 執行 i = 2 情況");continue;}NSLog(@"i = %d", i);}NSLog(@"\n");NSLog(@"return 控制 : ");for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j ++){if(i == 1 && j == 1){NSLog(@"i == 1 && j == 1, 直接退出函數, 不再執行下面的語句");return 0;}NSLog(@"i = %d, j = %d", i, j);}}}}
-- 執行結果 :
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 控制 : 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 中斷本層迴圈, 執行 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 控制 : 2014-12-03 01:06:35.675 a.out[1360:507] i = 02014-12-03 01:06:35.675 a.out[1360:507] i == 1, 終止本次執行, 執行 i = 2 情況2014-12-03 01:06:35.675 a.out[1360: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 控制 : 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, 直接退出函數, 不再執行下面的語句
5. goto 語句
goto 用法 :
-- 定義標籤 : 在程式任意位置打上標籤, 例如 "start : ";
-- 跳轉標籤 : 使用 "goto 標籤;" 語句, 跳轉到指定位置;
goto 常用情境 : 從內層迴圈跳到指定的外層迴圈, 或者直接跳出多重嵌套迴圈, 還要繼續執行下面的語句;
程式碼範例 :
-- Object-C 代碼 :
/************************************************************************* > File Name: 11-goto.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 三 12/ 3 01:09:55 2014 ************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]){@autoreleasepool {NSLog(@"goto 代替 do while 迴圈 : ");int k = 0;circle :NSLog(@"k = %d", k++);if(k < 3){goto circle;}NSLog(@"\n");NSLog(@"goto 跳出本層迴圈");for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j ++){if(i == 1 && j == 1){NSLog(@"此時 i == 1 && j == 1跳出到 外層迴圈, 執行 i = 2 的情況");goto out;}NSLog(@"i = %d, j = %d", i, j);}out :NSLog(@"記憶體迴圈執行完畢");}NSLog(@"\n");NSLog(@"goto 跳出所有迴圈");for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j ++){if(i == 1 && j == 1){NSLog(@"此時 i == 1 & j == 1 跳出所有迴圈");NSLog(@"i = %d, j = %d", i, j);}}}over : NSLog(@"所有迴圈執行完畢");}}
-- 執行結果 :
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 代替 do while 迴圈 : 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.out[1475:507] goto 跳出本層迴圈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] 記憶體迴圈執行完畢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] 此時 i == 1 && j == 1跳出到 外層迴圈, 執行 i = 2 的情況2014-12-03 01:26:36.032 a.out[1475:507] 記憶體迴圈執行完畢2014-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] 記憶體迴圈執行完畢2014-12-03 01:26:36.034 a.out[1475:507] 2014-12-03 01:26:36.034 a.out[1475:507] goto 跳出所有迴圈2014-12-03 01:26:36.035 a.out[1475:507] 此時 i == 1 & j == 1 跳出所有迴圈2014-12-03 01:26:36.035 a.out[1475:507] i = 1, j = 12014-12-03 01:26:36.035 a.out[1475:507] 所有迴圈執行完畢
【IOS 開發】Object - C 文法 之 流程式控制制