发布于 2015-07-05 11:42:44 | 179 次阅读 | 评论: 0 | 来源: 网络整理
下表列出了所有支持Objective-C语言的逻辑运算符。假设变量A=1,变量B=0,那么:
运算符 | 描述 | 示例 |
---|---|---|
&& | 调用逻辑AND运算符。如果这两个操作数不为零,则条件为真。 | (A && B) is false. |
|| | 调用逻辑或运算符。如果两个操作数中的任何一个不为零,则条件为真。 | (A || B) is true. |
! | 调用逻辑NOT运算。使用反转其操作数的逻辑状态。如果一个条件是真的,那么逻辑NOT运算符为假。 | !(A && B) is true. |
尝试下面的例子就明白了所有在Objective-C编程语言的逻辑运算符:
#import <Foundation/Foundation.h>
main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
NSLog(@"Line 1 - Condition is truen" );
}
if ( a || b )
{
NSLog(@"Line 2 - Condition is truen" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b )
{
NSLog(@"Line 3 - Condition is truen" );
}
else
{
NSLog(@"Line 3 - Condition is not truen" );
}
if ( !(a && b) )
{
NSLog(@"Line 4 - Condition is truen" );
}
}
当编译和执行上述程序,它会产生以下结果:
2013-09-07 22:35:57.256 demo[19012] Line 1 - Condition is true
2013-09-07 22:35:57.256 demo[19012] Line 2 - Condition is true
2013-09-07 22:35:57.256 demo[19012] Line 3 - Condition is not true
2013-09-07 22:35:57.256 demo[19012] Line 4 - Condition is true