首页 技术 正文
技术 2022年11月14日
0 收藏 714 点赞 5,040 浏览 6700 个字

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.moctopus-2:oc octopus$ ./a.out2014-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.moctopus-2:oc octopus$ ./a.out2014-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.moctopus-2:oc octopus$ ./a.out2014-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.moctopus-2:oc octopus$ ./a.out2014-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.moctopus-2:oc octopus$ ./a.out2014-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] 所有循环执行完毕
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,466
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765