日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【IOS 开发】Objective - C 语法 之 流程控制

發(fā)布時(shí)間:2025/6/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【IOS 开发】Objective - C 语法 之 流程控制 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

1. if 條件語(yǔ)句

?

if 表達(dá)式 : 表達(dá)式是一個(gè) 整型 或者 布爾型, 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數(shù)字也可以是TRUE");}} }


執(zhí)行結(jié)果 :?

?

?

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] 小于等于10 2014-12-02 01:49:12.490 a.out[658:507] 非0數(shù)字也可以是TRUE

?

?

?

?

?

?

2. switch 分支語(yǔ)句

?

?

switch 控制表達(dá)式 : switch() 中得控制表達(dá)式類型限定 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 分支語(yǔ)句 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");}} }

?

?

?

?

?

-- 執(zhí)行結(jié)果 :?

?

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. 循環(huán)結(jié)構(gòu)

?

?

循環(huán)要素 :?

-- 初始化 (init_statements) : 初始化循環(huán)中用到的數(shù)據(jù);

-- 循環(huán)條件 (test_expression) : boolean 表達(dá)式, 決定是否執(zhí)行循環(huán)體;

-- 循環(huán)體 (body_statements) : 重復(fù)執(zhí)行的內(nèi)容;

-- 迭代語(yǔ)句 (iteration_statements) : 改變循環(huán)條件;

?

?

(1) while 循環(huán)

?

while 循環(huán)格式 : 先判斷 test_expression 值, 如果為 TRUE, 執(zhí)行循環(huán)體, 否則執(zhí)行下面的語(yǔ)句;

?

init_statements;

while(test_expression)

{

body_statement;

iteration_statements;

}

?

?

(2) do while 循環(huán)

?

do while 循環(huán)格式 : 先執(zhí)行循環(huán)體, 判斷循環(huán)條件, 如果 test_expression 為真, 就執(zhí)行下一次循環(huán), 否則終止循環(huán);

?

init_statements;

do

{

body_statements;

iteration_statements;

}while(test_expression)

?

?

(3) for 循環(huán)

?

for 循環(huán)格式 :?

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 循環(huán)int a = 3;while(a > 0){NSLog(@"while 循環(huán) : a 的值是 %d", a);a--;}//do while 循環(huán) 這里 a 不符合條件, 只執(zhí)行 do 中得語(yǔ)句do{NSLog(@"do while 循環(huán) : a = %d", a);}while(a > 100);//for 循環(huán)for(int i = 0; i < 5; i ++){NSLog(@"for 循環(huán) i = %d", i);}} }

?

?

?

?

?

執(zhí)行結(jié)果 :?

?

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 循環(huán) : a 的值是 3 2014-12-02 20:47:14.456 a.out[1021:507] while 循環(huán) : a 的值是 2 2014-12-02 20:47:14.456 a.out[1021:507] while 循環(huán) : a 的值是 1 2014-12-02 20:47:14.457 a.out[1021:507] do while 循環(huán) : a = 0 2014-12-02 20:47:14.457 a.out[1021:507] for 循環(huán) i = 0 2014-12-02 20:47:14.457 a.out[1021:507] for 循環(huán) i = 1 2014-12-02 20:47:14.458 a.out[1021:507] for 循環(huán) i = 2 2014-12-02 20:47:14.458 a.out[1021:507] for 循環(huán) i = 3 2014-12-02 20:47:14.459 a.out[1021:507] for 循環(huán) i = 4

?

?

?

?

?

?

4. 循環(huán)控制?

?

循環(huán)控制 :?

-- break : 退出當(dāng)層循環(huán);

-- continue : 跳過該次循環(huán), 執(zhí)行下一次循環(huán);

-- return : 直接返回函數(shù), 不管有多少層, 直接返回;

?

代碼示例 :?

-- 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 會(huì) 跳出 對(duì)應(yīng)的當(dāng)前一級(jí)的循環(huán), 如果是嵌套循環(huán), 只會(huì)跳出那一層循環(huán)for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j++){if(i == 1 && j == 1){NSLog(@"i = 1, j = 1 中斷本層循環(huán), 執(zhí)行 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, 終止本次執(zhí)行, 執(zhí)行 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, 直接退出函數(shù), 不再執(zhí)行下面的語(yǔ)句");return 0;}NSLog(@"i = %d, j = %d", i, j);}}} }

?

?

?

?

?

-- 執(zhí)行結(jié)果 :?

?

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 = 0 2014-12-03 01:06:35.671 a.out[1360:507] i = 0, j = 1 2014-12-03 01:06:35.672 a.out[1360:507] i = 1, j = 0 2014-12-03 01:06:35.672 a.out[1360:507] i = 1, j = 1 中斷本層循環(huán), 執(zhí)行 i = 2 的情況 2014-12-03 01:06:35.673 a.out[1360:507] i = 2, j = 0 2014-12-03 01:06:35.673 a.out[1360:507] i = 2, j = 1 2014-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 = 0 2014-12-03 01:06:35.675 a.out[1360:507] i == 1, 終止本次執(zhí)行, 執(zhí)行 i = 2 情況 2014-12-03 01:06:35.675 a.out[1360:507] i = 2 2014-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 = 0 2014-12-03 01:06:35.677 a.out[1360:507] i = 0, j = 1 2014-12-03 01:06:35.677 a.out[1360:507] i = 1, j = 0 2014-12-03 01:06:35.678 a.out[1360:507] i == 1 && j == 1, 直接退出函數(shù), 不再執(zhí)行下面的語(yǔ)句

?

?

?

?

?

?

5. goto 語(yǔ)句

?

?

goto 用法 :?

-- 定義標(biāo)簽 : 在程序任意位置打上標(biāo)簽, 例如 "start : ";

-- 跳轉(zhuǎn)標(biāo)簽 : 使用 "goto 標(biāo)簽;" 語(yǔ)句, 跳轉(zhuǎn)到指定位置;

?

goto 常用場(chǎng)景 : 從內(nèi)層循環(huán)跳到指定的外層循環(huán), 或者直接跳出多重嵌套循環(huán), 還要繼續(xù)執(zhí)行下面的語(yǔ)句;

?

代碼示例 :?

-- 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 循環(huán) : "); int k = 0;circle :NSLog(@"k = %d", k++);if(k < 3){goto circle;}NSLog(@"\n");NSLog(@"goto 跳出本層循環(huán)");for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j ++){if(i == 1 && j == 1){NSLog(@"此時(shí) i == 1 && j == 1跳出到 外層循環(huán), 執(zhí)行 i = 2 的情況");goto out;}NSLog(@"i = %d, j = %d", i, j);}out :NSLog(@"內(nèi)存循環(huán)執(zhí)行完畢");}NSLog(@"\n");NSLog(@"goto 跳出所有循環(huán)");for(int i = 0; i < 3; i ++){for(int j = 0; j < 2; j ++){if(i == 1 && j == 1){NSLog(@"此時(shí) i == 1 & j == 1 跳出所有循環(huán)");NSLog(@"i = %d, j = %d", i, j);}}}over : NSLog(@"所有循環(huán)執(zhí)行完畢");} }

?

?

?

?

?

-- 執(zhí)行結(jié)果 :?

?

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 循環(huán) : 2014-12-03 01:26:36.028 a.out[1475:507] k = 0 2014-12-03 01:26:36.029 a.out[1475:507] k = 1 2014-12-03 01:26:36.029 a.out[1475:507] k = 2 2014-12-03 01:26:36.029 a.out[1475:507] 2014-12-03 01:26:36.030 a.out[1475:507] goto 跳出本層循環(huán) 2014-12-03 01:26:36.030 a.out[1475:507] i = 0, j = 0 2014-12-03 01:26:36.031 a.out[1475:507] i = 0, j = 1 2014-12-03 01:26:36.031 a.out[1475:507] 內(nèi)存循環(huán)執(zhí)行完畢 2014-12-03 01:26:36.031 a.out[1475:507] i = 1, j = 0 2014-12-03 01:26:36.032 a.out[1475:507] 此時(shí) i == 1 && j == 1跳出到 外層循環(huán), 執(zhí)行 i = 2 的情況 2014-12-03 01:26:36.032 a.out[1475:507] 內(nèi)存循環(huán)執(zhí)行完畢 2014-12-03 01:26:36.033 a.out[1475:507] i = 2, j = 0 2014-12-03 01:26:36.033 a.out[1475:507] i = 2, j = 1 2014-12-03 01:26:36.033 a.out[1475:507] 內(nèi)存循環(huán)執(zhí)行完畢 2014-12-03 01:26:36.034 a.out[1475:507] 2014-12-03 01:26:36.034 a.out[1475:507] goto 跳出所有循環(huán) 2014-12-03 01:26:36.035 a.out[1475:507] 此時(shí) i == 1 & j == 1 跳出所有循環(huán) 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] 所有循環(huán)執(zhí)行完畢

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的【IOS 开发】Objective - C 语法 之 流程控制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。