Objective-C 嵌套 if-else 语句
在 Objective-C 编程中,是可以使用嵌套 if-else 语句的,这意味着您可以在一个 if 或 else-if 语句中使用另一个 if 或 else-if 语句。
语法
嵌套 if 语句的语法如下:
if( boolean_expression 1) {/* 当 boolean 表达式 1 为 true 时,执行这里的代码 */if(boolean_expression 2) {/* 当 boolean 表达式 1 与 2 都为 true 时,执行这里的代码 */}}
您可以用与嵌套 if 语句类似的方式嵌套 else if…else。
实例
#import <Foundation/Foundation.h>int main () {/* local variable definition */int a = 100;int b = 200;/* check the boolean condition */if( a == 100 ) {/* if condition is true then check the following */if( b == 200 ) {/* if condition is true then print the following */NSLog(@"Value of a is 100 and b is 200\n" );}}NSLog(@"Exact value of a is : %d\n", a );NSLog(@"Exact value of b is : %d\n", b );return 0;}
结果如下:
2022-07-07 22:08:19.984 demo[18141] Value of a is 100 and b is 2002022-07-07 22:08:19.985 demo[18141] Exact value of a is : 1002022-07-07 22:08:19.985 demo[18141] Exact value of b is : 200