zoukankan      html  css  js  c++  java
  • 【IOS 开发】Object


    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] 小于等于10
    2014-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 2014
    	Switch 分支语句 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 的值是 3
    2014-12-02 20:47:14.456 a.out[1021:507] while 循环 : a 的值是 2
    2014-12-02 20:47:14.456 a.out[1021:507] while 循环 : a 的值是 1
    2014-12-02 20:47:14.457 a.out[1021:507] do while 循环 : a = 0
    2014-12-02 20:47:14.457 a.out[1021:507] for 循环 i = 0
    2014-12-02 20:47:14.457 a.out[1021:507] for 循环 i = 1
    2014-12-02 20:47:14.458 a.out[1021:507] for 循环 i = 2
    2014-12-02 20:47:14.458 a.out[1021:507] for 循环 i = 3
    2014-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(@"
    ");
    		NSLog(@"continue 控制 : ");
    		for(int i = 0; i < 3; i ++)
    		{
    			if(i == 1)
    			{
    				NSLog(@"i == 1, 终止本次执行, 执行 i = 2 情况");
    				continue;
    			}
    			NSLog(@"i = %d", i);
    		}
    
    		NSLog(@"
    ");
    		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 = 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 中断本层循环, 执行 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, 终止本次执行, 执行 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, 直接退出函数, 不再执行下面的语句



    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(@"
    ");
    		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(@"
    ");
    		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 = 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 跳出本层循环
    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] 内存循环执行完毕
    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] 此时 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 = 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] 内存循环执行完毕
    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 = 1
    2014-12-03 01:26:36.035 a.out[1475:507] 所有循环执行完毕




  • 相关阅读:
    开启sentry权限控制hue
    hive_server2的权限控制
    自带的simple认证
    tableau备份
    tableau分布式添加节点
    升级tableau版本
    tableau日常管理
    mavn Nexus Repository Manager漏洞
    第3章:打造命令行工具
    基于从库+binlog方式恢复数据
  • 原文地址:https://www.cnblogs.com/hanshuliang/p/4215383.html
Copyright © 2011-2022 走看看