【iOS 开发】Objective-C 运算符(二)

4. 比较运算符


比较运算 : 结果是正数, 1 是 真, 0 是 假;


-- == 运算 : 比较数值类型, 即使 类型不同, 值相等, 那么返回 true, 5.0 == 5 结果是 1;


== != 运算 : 比较数值类型, 不管类型, 值不等, 返回 false 0;



源码示例 :



/*************************************************************************
    > File Name: 10-compare.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一 12/ 1 01:35:57 2014
 ************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
    @autoreleasepool {
  //只能进行数值比较, 只进行值的比较
  NSLog(@"3 > 1 = %d", 3 > 1);
  //数据类型不同, 但是如果值 相等, 那么返回 1
  NSLog(@"3 == 3.0 = %d", 3 == 3.0);
  NSLog(@"97 == 'a' = %d", 97 == 'a');
    }
}





执行结果 :



octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-compare.m 
octopus-2:oc octopus$ ./a.out 
2014-12-01 01:44:56.099 a.out[3844:507] 3 > 1 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 3 == 3.0 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 97 == 'a' = 1







5. 逻辑运算符


逻辑运算 : 逻辑运算只能是 BOOL 变量或者 常量才能使用该运算符;


-- 与 操作 (&&) : 都为 TRUE 结果才为 TRUE;


-- 或 操作 (||) : 只要有一个是 TRUE, 结果就是 TRUE;


-- 非 操作 (!) : 操作数有一个, !TRUE = FALSE;


-- 异或 操作 (^) : 操作数相同 返回 FALSE, 不用 返回 TRUE;



代码示例 :



/*************************************************************************
    > File Name: 10-logic.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一 12/ 1 01:49:28 2014
 ************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
    @autoreleasepool {
  //非 0 是 TRUE, 非 其它数 是 FALSE
  NSLog(@"!3 = %d", !3);    
  NSLog(@"3 > 2 && '1' > 10 = %d", 3 > 2 && '1' > 10);
  NSLog(@"3 > 2 || 2 > 3 = %d", 3 > 2 || 2 > 3);
  NSLog(@"3 > 2 ^ 3 > 2 = %d", 3 > 2 ^ 3 > 2);
    }
}





执行结果 :



octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-logic.m 
octopus-2:oc octopus$ ./a.out 
2014-12-01 01:54:00.282 a.out[3898:507] !3 = 0
2014-12-01 01:54:00.284 a.out[3898:507] 3 > 2 && '1' > 10 = 1
2014-12-01 01:54:00.285 a.out[3898:507] 3 > 2 || 2 > 3 = 1
2014-12-01 01:54:00.286 a.out[3898:507] 3 > 2 ^ 3 > 2 = 0







6. 逗号运算符


逗号表达式 : "表达式1, 表达式2 ... 表达式n", 结果返回的时 表达式n 的值;



代码示例 :



/*************************************************************************
    > File Name: 10-comma.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一 12/ 1 01:57:31 2014
 ************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
    @autoreleasepool {
  int a = (3, 3 > 2);
  NSLog(@"a = %d", a);
  int b = (a = 3, a = 4, a = 5, a = 6);
  NSLog(@"b = %d", b);
    }
}






执行结果 :



octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-comma.m 
10-comma.m:13:12: warning: expression result unused [-Wunused-value]
                int a = (3, 3 > 2);
                         ^
1 warning generated.
octopus-2:oc octopus$ ./a.out 
2014-12-01 02:00:06.466 a.out[3919:507] a = 1
2014-12-01 02:00:06.469 a.out[3919:507] b = 6



7. 三目运算符


三目运算符格式 : "表达式1 : 表达式2 : 表达式3", 如果表达式1 为 true, 返回 表达式2 的值, 表达式1 为 false, 返回 表达式3 的值;



示例源码 :



/*************************************************************************
    > File Name: 10-three.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一 12/ 1 02:03:40 2014
 ************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
    @autoreleasepool {
  int a = 0;
  NSString *str = a ? @"a是TRUE" : @"a是FALSE" ;  
  NSLog(@"%@", str);
    }
}

执行效果 :




octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-three.m 
octopus-2:oc octopus$ ./a.out 
2014-12-01 02:05:42.389 a.out[3930:507] a是FALSE

 


上一篇:【IOS 开发】Objective - C 语法 之 流程控制(二)


下一篇:LVM 的创建,扩展,缩减及建立快照