Objective-c-logical-operators

提供:Dev Guides
移動先:案内検索

Objective-Cの論理演算子

次の表は、Objective-C言語でサポートされているすべての論理演算子を示しています。 変数 A が1を保持し、変数 B が0を保持すると仮定します-

Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false.
Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A
B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

次の例を試して、Objective-Cプログラミング言語で利用可能なすべての論理演算子を理解してください-

#import <Foundation/Foundation.h>

int main() {
   int a = 5;
   int b = 20;

   if ( a && b ) {
      NSLog(@"Line 1 - Condition is true\n" );
   }

   if ( a || b ) {
      NSLog(@"Line 2 - Condition is true\n" );
   }

  /*lets change the value of  a and b*/
   a = 0;
   b = 10;

   if ( a && b ) {
      NSLog(@"Line 3 - Condition is true\n" );
   } else {
      NSLog(@"Line 3 - Condition is not true\n" );
   }

   if ( !(a && b) ) {
      NSLog(@"Line 4 - Condition is true\n" );
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

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