논리 연산자(logical operator)

&&(and), ||(or),!(not)

 

&&(AND결합) 피연산자 양쪽 모두 true여야 true를 결과로 얻는다.
||(or결합) 피연산자 중 어느 한 쪽만 true면 true를 결과로 얻는다.

논리 연산자의 피연산자가 참인 경우와 거짓인 경우의 연산 결과를 표(truth table)로나타내면 다음과 같다.

 

x y x||y x&&y
true true true true
true false true false
false true true false
false false false false

 

사용 시 주의해야 할 점을 알아보자

 

 

public class OperatorEx24_0 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
		
		x = 15;
		System.out.printf("x=%2d, 10 < x && x < 20 = %b%n", x, 10 < x && x < 20);
        
        }
}
x=15, 10 < x && x < 20 = true

 

x=15

&&는 10 < x과 x <20을 만족해야 true > (boolean) true

 

 

public class OperatorEx24_1 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
		
        		x = 6;
		System.out.printf("x=%2d , x%%2==0 || x%%3==0 && x%%6!=0 = %b%n",x, x%2==0 || x%3==0&&x%6!=0);
		System.out.printf("x=%2d , (x%%2==0 || x%%3==0) && x%%6!=0 = %b%n",x, (x%2==0 || x%3==0)&&x%6!=0);
		}

}
x= 6 , x%2==0 || x%3==0 && x%6!=0 = true
x= 6 , (x%2==0 || x%3==0) && x%6!=0 = false

 

x=6

(X가 3의 배수이고(&&) X가 6의 배수가 아니면 참)이거나(||) X가 2의 배수(x%2==0)

거짓 이거나(||) X가 이의 배수 = 참

&&가(AND) 곱셈 연산자이고 ||(OR)이 덧셈 연산자라 &&가 ||보다 계산이 빠름

 

X가 2의 배수(x%2==0) 거나(||) X가 3의 배수이고(&&) X가 6의 배수가 아니면 참= 참

 

 

public class OperatorEx24_2 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
				ch='1';
		System.out.printf("ch='%c', '0' <= ch && ch <= '9' =%b%n", ch, '0' <= ch && ch <='9');
      	}

}
ch='1', '0' <= ch && ch <= '9' =true

 

ch는 1 == char 타입 46

45 <=ch <=54가 참이므로 true

 

 

public class OperatorEx24_3 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
				ch='a';
		System.out.printf("ch='%c', 'a' <= ch && ch <= 'z' =%b%n", ch, 'a' <= ch && ch <='z');
		}

}
ch='a', 'a' <= ch && ch <= 'z' =true

 

ch = 'a' = char타입 97

a <=a <=z 즉 참

 

 

public class OperatorEx24_4 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
				ch='A';
		System.out.printf("ch='%c', 'A' <= ch && ch <= 'Z' =%b%n", ch, 'A' <= ch && ch <='Z');
       	}

}
ch='A', 'A' <= ch && ch <= 'Z' =true

 

ch = 'A' = char타입 65

A <=A <=Z 즉 참

 

 

public class OperatorEx24_5 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
        		ch='q';
		System.out.printf("ch='%c', 'q' <= ch && ch <= 'Q' =%b%n", ch, 'q' <= ch && ch <='Q');
        }
}
ch='q', 'q' <= ch && ch <= 'Q' =false

 

ch = 'q' = char 타입 113

'q' <= 'q'이고(&&) 'q'(113) <= 'Q'(71) = 거짓

 

 

효율적인 연산(short circuit evaluation)

논리 연산자의 또 다른 특징은 효율적인 연산을 한다는 것이다.

 

x y x||y
true false true
true false true
false true false
false false false

x가 true면 그대로 연산 종료

 

x y x&&y
true false true
true false true
false true false
false false false

x가 false면 그대로 연산 종료

 

 

논리 부정 연산자

!

이 연산자는 피연산자가 true이면 false를, false이면 true를 결과로 반환하는 연산자이다.

 

x !x
true false
false true

어떤 값에 논리연산자를 중복으로 적용하면 참과 거짓이 차례로반복된다. a가 참이면

!!!!a의 경우 

!!! + !a(거짓)

!! !a'(참)

! !a''(거짓)

!a'''(참)

최종 a=참이 된다.

+ Recent posts