• [Objective-C语言教程]决策结构(10)


    决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及在条件被确定为真时要执行的一个或多个语句,以及可选的,如果条件要执行的其他语句 被认定是假的。

    以下是大多数编程语言中的典型决策结构的一般形式 -

    Objective-C编程语言将任何非零和非null假定为true,如果它为零或null,则将其假定为false
    Objective-C编程语言提供以下类型的决策制定语句。 单击以下链接查看其详细信息 -

    编号语句描述
    1 if语句 if语句是由布尔表达式后跟一个或多个语句组成。
    2 if…else语句 if语句后面可以跟一个可选的else语句,该语句在if布尔条件表达式为false时执行。
    3 嵌套if语句 在一个ifelse if语句中可使用ifelse if语句。
    4 switch语句 switch语句用于测试变量与值列表的相等性。
    5 嵌套switch语句 在一个switch语句中使用一个switch语句。

    ?:运算符

    前面我们讲过了条件运算符?:,条件运算符可以用来替换if...else语句。它的一般形式如下 -

    Exp1 ? Exp2 : Exp3;

    Exp1Exp2Exp3都是表达式。 注意冒号的使用和放置。

    ?表达式的确定方式如下:评估Exp1。 如果结果为true,那么Exp2会被评估并成为整个值?表达式的值。 如果Exp1评估为false,则计算Exp3Exp3的结果值将成为表达式的值。

    1. if语句

    if语句由布尔表达式后跟一个或多个语句组成。

    语法

    Objective-C编程语言中if语句的语法是 -

    1 if(boolean_expression) {
    2    /*  如果布尔表达式为true,则执行 */
    3    // statement(s)
    4 }

    如果布尔表达式(boolean_expression)的计算结果为true,那么将执行if语句中的代码块。 如果布尔表达式(boolean_expression)的计算结果为false,那么将执行if语句结束后(在结束大括号之后)的第一组代码。

    Objective-C编程语言将任何非零和非null值假定为true,如果它为零或null,则将其假定为false

    流程图

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4 
     5    /* 定义局部变量 */
     6    int a = 10;
     7 
     8    /* 使用if语句检查布尔条件 */
     9    if( a < 20 ) {
    10       /* 如果条件为 true,则打印以下内容 */
    11       NSLog(@"a is less than 20
    " );
    12    }
    13 
    14    NSLog(@"value of a is : %d
    ", a);
    15    return 0;
    16 }

    执行上面示例代码,得到以下结果:

    1 2018-11-14 08:58:49.415 main[152191] a is less than 20
    2 2018-11-14 08:58:49.417 main[152191] value of a is : 10

    2. if…else语句

    if语句后面可以跟一个可选的else语句,else语句在布尔表达式为false时执行。

    语法

    Objective-C编程语言中if...else语句的语法是 -

    1 if(boolean_expression) {
    2    /* statement(s) 如果布尔表达式为true,则执行此语句 */
    3 } else {
    4   /* statement(s) 如果布尔表达式为false,则执行此语句 */
    5 }

    如果布尔表达式(boolean_expression)的计算结果为true,那么将执行if代码块,否则将执行else中的代码块。

    Objective-C编程语言将任何非零和非null值假定为true,如果它为零或null,则将其假定为false

    流程图

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4    /* 局部变量定义 */
     5    int a = 100;
     6 
     7    /* 检查布尔条件 */
     8    if( a < 20 ) {
     9       /* 如果条件为true,则打印以下结果 */
    10       NSLog(@"a is less than 20
    " );
    11    } else {
    12       /* 如果条件为false,则打印以下结果 */
    13       NSLog(@"a is not less than 20
    " );
    14    }
    15 
    16    NSLog(@"value of a is : %d
    ", a);
    17    return 0;
    18 }

    编译并执行上述代码时,会产生以下结果 -

    1 2018-11-14 09:23:05.241 main[6546] a is not less than 20
    2 2018-11-14 09:23:05.243 main[6546] value of a is : 100

    if…else if…else语句

    if语句后面可以跟一个可选的else if...else语句,这对于使用单个if...else if语句测试各种条件非常有用。

    当使用ifelse ifelse语句时,要记住几点 -

    • if可以有零个或一个else,它必须在else if之后。
    • if可以有零或多个else if,并且它们必须在else之前。
    • 当有一个else if条件匹配成功,其余的else if或者else都不会再条件匹配测试。

    语法

    Objective-C编程语言中if...else if语句的语法是 -

    1 if(boolean_expression 1) {
    2    /* Executes when the boolean expression 1 is true */
    3 } else if( boolean_expression 2) {
    4    /* Executes when the boolean expression 2 is true */
    5 } else if( boolean_expression 3) {
    6    /* Executes when the boolean expression 3 is true */
    7 } else {
    8    /* executes when the none of the above condition is true */
    9 }

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4    /* 定义局部变量 */
     5    int a = 100;
     6 
     7    /* 检查布尔条件 */
     8    if( a == 10 ) {
     9       /* 如果if条件为真,则打印以下内容 */
    10       NSLog(@"Value of a is 10
    " );
    11    } else if( a == 20 ) {
    12       /* 如果else...if条件为真,则打印以下内容 */
    13       NSLog(@"Value of a is 20
    " );
    14    } else if( a == 30 ) {
    15       /* 如果else...if条件为真,则打印以下内容 */
    16       NSLog(@"Value of a is 30
    " );
    17    } else {
    18       /* 如果没有一个条件为真,则打印以下内容 */
    19       NSLog(@"None of the values is matching
    " );
    20    }
    21    NSLog(@"Exact value of a is: %d
    ", a );
    22    return 0;
    23 }

    编译并执行上述代码时,会产生以下结果 -

    1 2018-11-14 09:31:07.594 main[96166] None of the values is matching
    2 2018-11-14 09:31:07.596 main[96166] Exact value of a is: 100

    3. 嵌套if语句

    在Objective-C编程中,嵌套if-else语句是合法的,可以在一个ifelse if语句中使用ifelse if语句。

    语法

    嵌套if语句的语法如下 -

    1 if( boolean_expression1) {
    2    /* 如果 boolean_expression1 为true,则执行 */
    3    if(boolean_expression2) {
    4       /* 如果 boolean_expression2 为true,则执行 */
    5    }
    6 }

    可以使用与嵌套if语句类似的方式嵌套if else语句。

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4 
     5    /* 定义局部变量 */
     6    int a = 100;
     7    int b = 200;
     8 
     9    /* 检查布尔条件 */
    10    if( a == 100 ) {
    11 
    12       /* 如果条件为true,则继续检查以下条件 */
    13       if( b == 200 ) {
    14          /* 如果条件为true,则打印以下结果 */
    15          NSLog(@"Value of a is 100 and b is 200
    " );
    16       }
    17    }
    18    NSLog(@"Exact value of a is : %d
    ", a );
    19    NSLog(@"Exact value of b is : %d
    ", b );
    20 
    21    return 0;
    22 }

    执行上面示例代码,得到以下结果:

    1 2018-11-14 09:40:57.545 main[149996] Value of a is 100 and b is 200
    2 2018-11-14 09:40:57.547 main[149996] Exact value of a is : 100
    3 2018-11-14 09:40:57.547 main[149996] Exact value of b is : 200

    4. switch语句

    switch语句用于测试变量与值列表的相等性。每个值称为一个case,并检查每个switch case接通的变量。

    语法

    Objective-C编程语言中switch语句的语法如下 -

     1 switch(expression){
     2    case constant-expression  :
     3       statement(s);
     4       break; /* 可选 */
     5    case constant-expression  :
     6       statement(s);
     7       break; /* 可选 */
     8 
     9    /* 可以有任意数量的case语句*/
    10    default : /* 可选 */
    11       statement(s);
    12 }

    以下规则适用于switch语句 -

    • switch语句中使用的表达式(expression)必须具有整数或枚举类型,或者是类类型,其中类具有单个转换函数为整数或枚举类型。
    • switch中包含任意数量的case语句,每个case后跟要与之比较的值和冒号。
    • case之后的constant-expression必须与switch中的变量具有相同的数据类型,并且必须是常量或文字。
    • 当接通的变量等于case时,case之后的语句将一直执行,直到达到break语句。
    • 当达到break语句时,switch终止,控制流跳转到switch语句后面的下一行。
    • 并非每个break都需要包含break。 如果没有出现break,则控制流将进入后续case,直到达到break
    • switch语句可以有一个可选的默认情况,它必须出现在switch的末尾。 当没有任何case匹配为真时,default可用于执行任务。 default不需要包含break语句。

    流程图

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4 
     5    /* 局布变量定义 */
     6    char grade = 'B';
     7 
     8    switch(grade) {
     9    case 'A' :
    10       NSLog(@"Excellent!
    " );
    11       break;
    12    case 'B' :
    13    case 'C' :
    14       NSLog(@"Well done
    " );
    15       break;
    16    case 'D' :
    17       NSLog(@"You passed
    " );
    18       break;
    19    case 'F' :
    20       NSLog(@"Better try again
    " );
    21       break;
    22    default :
    23       NSLog(@"Invalid grade
    " );
    24    }
    25 
    26    NSLog(@"Your grade is  %c
    ", grade );
    27    return 0;
    28 }

    执行上面示例代码,得到以下结果:

    1 2018-11-14 09:55:06.673 main[53795] Well done
    2 2018-11-14 09:55:06.675 main[53795] Your grade is  B

    5. 嵌套switch语句

    可以将switch作为外部switch语句序列的一部分。 即使内部和外部switchcase常量包含公共值,也不会产生冲突。

    语法

    嵌套switch语句的语法如下 -

     1 switch(ch1) {
     2    case 'A': 
     3       printf("This A is part of outer switch" );
     4 
     5       switch(ch2) {
     6          case 'A':
     7             printf("This A is part of inner switch" );
     8             break;
     9          case 'B': /* case code */
    10       }
    11       break;
    12    case 'B': /* case code */
    13 }

    示例代码

     1 #import <Foundation/Foundation.h>
     2 
     3 int main () {
     4 
     5    /* 局部变量定义 */
     6    int a = 100;
     7    int b = 200;
     8 
     9    switch(a) {
    10       case 100: 
    11          NSLog(@"This is part of outer switch
    ", a );
    12          switch(b) {
    13             case 200:
    14                NSLog(@"This is part of inner switch
    ", a );
    15          }
    16    }
    17    NSLog(@"Exact value of a is : %d
    ", a );
    18    NSLog(@"Exact value of b is : %d
    ", b );
    19 
    20    return 0;
    21 }

    执行上面示例代码,得到以下结果:

    1 2018-11-15 00:55:06.093 main[167622] This is part of outer switch
    2 2018-11-15 00:55:06.095 main[167622] This is part of inner switch
    3 2018-11-15 00:55:06.095 main[167622] Exact value of a is : 100
    4 2018-11-15 00:55:06.095 main[167622] Exact value of b is : 200
  • 相关阅读:
    生产者-消费者模型-线程安全队列Queue
    多线程简单案例
    cloudstack 用admin 账号创建虚拟机只是提示insufficient resource
    什么是工厂函数?Python 中工厂函数怎么理解?(转)
    rsync + mysql + gzip + --single-transaction
    Python中获取异常(try Exception)信息
    ansible copy 模块 changed false 没有变化
    _mysql.c:29:20: error: Python.h: No such file or directory
    常用网址
    Android 中常见控件的介绍和使用
  • 原文地址:https://www.cnblogs.com/strengthen/p/10563297.html
Copyright © 2020-2023  润新知