下表列出了所有支持Objective-C語言的關(guān)系運(yùn)算符。假設(shè)變量A=10和變量B=20,則:
運(yùn)算符 | 描述 | 示例 |
---|---|---|
== | 檢查是否兩個操作數(shù)的值等于或不等于,如果是的話那么條件為真。 | (A == B) is not true. |
!= | 檢查兩個操作數(shù)的值等于或不相等,如果值不相等,則條件為真。 | (A != B) is true. |
> | 檢查是否左操作數(shù)的值大于右操作數(shù)的值,如果是的話那么條件為真。 | (A > B) is not true. |
< | 檢查是否左操作數(shù)的值小于右操作數(shù)的值,如果是的話那么條件為真。 | (A < B) is true. |
>= | 檢查是否左操作數(shù)的值大于或等于右操作數(shù)的值,如果是的話那么條件為真。 | (A >= B) is not true. |
<= | 檢查是否左操作數(shù)的值小于或等于右邊的操作數(shù)的值,如果是的話那么條件為真。 | (A <= B) is true. |
嘗試下面的例子就明白了在Objective-C編程語言的所有關(guān)系運(yùn)算符:
#import <Foundation/Foundation.h> main() { int a = 21; int b = 10; int c ; if( a == b ) { NSLog(@"Line 1 - a is equal to b " ); } else { NSLog(@"Line 1 - a is not equal to b " ); } if ( a < b ) { NSLog(@"Line 2 - a is less than b " ); } else { NSLog(@"Line 2 - a is not less than b " ); } if ( a > b ) { NSLog(@"Line 3 - a is greater than b " ); } else { NSLog(@"Line 3 - a is not greater than b " ); } /* Lets change value of a and b */ a = 5; b = 20; if ( a <= b ) { NSLog(@"Line 4 - a is either less than or equal to b " ); } if ( b >= a ) { NSLog(@"Line 5 - b is either greater than or equal to b " ); } }
當(dāng)編譯和執(zhí)行上述程序,它會產(chǎn)生以下結(jié)果:
2013-09-07 22:42:18.254 demo[9486] Line 1 - a is not equal to b 2013-09-07 22:42:18.254 demo[9486] Line 2 - a is not less than b 2013-09-07 22:42:18.254 demo[9486] Line 3 - a is greater than b 2013-09-07 22:42:18.254 demo[9486] Line 4 - a is either less than or equal to b 2013-09-07 22:42:18.254 demo[9486] Line 5 - b is either greater than or equal to b