Java if

1 개요[ | ]

자바의 조건분 if에 대하여 알아보자
  • 조건문의 형태
    • if : 조건문이 참(True)이면 실행
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;

        if (  a < b  ){
            System.out.println("조건문이 참입니다.");
        }
    }
}
    • else : 조건문이 참이아닐경우(False) 수행
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        if (  a > b  ){
            System.out.println("조건문이 참입니다.");
        }
        else{
            System.out.println("조건문이 참이 아닙니다");
        }
    }
}
    • else if : 이전 조건문이 참이 아니고, 현재 조건문이 참이면 실행
public class helloj {
    public static void main(String[] args){
        int a=20;
        int b=20;
        if (  a > b  ){
            System.out.println("조건문이 참입니다.");
        }
        else if ( a == b ){
            System.out.println("else if 조건문에 해당되었습니다.");
        }
    }
}

2 switch[ | ]

    • switch : 특정 조건에 해당하면 실행
public class helloj {
    public static void main(String[] args){
        int day = 4;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
        }
    }
}

3 비교 연산자[ | ]

  • 비교 연산자 종류
    • 보다 작다 : A < B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a < b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
    • 보다 작거나 같다 : A <= B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a <= b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
    • 보다 크다 : A > B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a > b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
    • 보다 크거나 같다 : A >= B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a >= b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
    • 같다 : A == B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a == b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
    • 같지 않다  : A != B
public class helloj {
    public static void main(String[] args){
        int a=10;
        int b=20;
        boolean c;
        c = a != b;

        System.out.println("비교결과는 " + c + " 입니다.");
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}