Records rather than Memories

논리 연산자 본문

Software/JAVA

논리 연산자

Downer 2019. 10. 15. 16:40

논리 연산자(Conditional Operation)

 

조건문을 중심으로 더 강력하게 하기 위해 논리 연산자와 비교(boolean)이 사용된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package javatutorials.conditionaloperator;
 
public class AndDemo {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        if (true && true) {
            System.out.println("1");
        }
        if (true && false) {
            System.out.println("2");
        }
        if (false && true) {
            System.out.println("3");
        }
        if (false && false) {
            System.out.println("4");
        }
 
    }
 
}
 
cs

&& : 두 boolean 모두 참이어야 참이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package javatutorials.conditionaloperator;
 
public class LoginDemo3 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String id = args[0];
        String password = args[1];
        if (id.equals("egoing"&& password.equals("111111")) {
            System.out.println("right");
        } else {
            System.out.println("wrong");
        }
 
    }
 
}
 
cs

if문의 중첩문법을 훨씬 간결하게 and를 사용해 활용할 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package javatutorials.conditionaloperator;
 
public class LoginDemo4 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String id = args[0];
        String password = args[1];
        if ((id.equals("egoing"|| id.equals("korea"|| id.equals("forever")) 
                && password.equals("111111")) {
            //id 세가지중 하나라도 참이고 password가지 일치하면 right를 출력
            System.out.println("right");
        } else {
            System.out.println("wrong");
        }
    }
 
}
 
cs

|| : 두 boolean 중 하나만 참이어도 참이다.

 

 

1
2
3
4
5
6
7
8
9
if(!true){
System.out.println(2);
 
}
 
if(!false){
System.out.println(2);
// true가 되어 출력
 
cs

! : not

'Software > JAVA' 카테고리의 다른 글

[java] for문  (0) 2019.10.15
[java] while 반복문  (0) 2019.10.15
GUIApp  (0) 2019.10.15
[java] switch문  (0) 2019.10.14
[java] 조건문의 중첩  (0) 2019.10.14
Comments