조건문

제민·2024년 7월 2일

Java 개념 공부

목록 보기
7/21
post-thumbnail

조건문 (Control Statements)

조건문은 프로그램의 흐름을 제어하기 위해 사용됩니다. 자바에서는 조건문을 통해 특정 조건을 만족할 때만 코드 블록을 실행하거나, 여러 조건 중 하나를 선택하여 실행할 수 있습니다. 주요 조건문으로는 if, else if, else, 그리고 switch 문이 있습니다.

1. if문

if 문은 조건식이 true일 때만 코드 블록을 실행합니다. 기본적인 형태는 다음과 같습니다:

if (조건식) {
    // 조건식이 true일 때 실행되는 코드
}

예제 1: 단순 if문

import java.util.Scanner;

public class A_if1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("정수: ");
        int num = sc.nextInt();

        if (num > 0) {
            System.out.println("양수이다.");
        }
        if (num == 0) {
            System.out.println("0이다.");
        }
        if (num < 0) {
            System.out.println("음수이다.");
        }
        sc.close();
    }
}

2. if-else문

if-else 문은 조건식이 true일 때와 false일 때 각각 다른 코드 블록을 실행합니다.

if (조건식) {
    // 조건식이 true일 때 실행되는 코드
} else {
    // 조건식이 false일 때 실행되는 코드
}

예제 2: if-else문

import java.util.Scanner;

public class A_if1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("정수: ");
        int num = sc.nextInt();

        if (num > 0) {
            System.out.println("양수이다.");
        } else {
            if (num == 0) {
                System.out.println("0이다.");
            } else {
                System.out.println("음수이다.");
            }
        }
        sc.close();
    }
}

3. if-else if-else문

if-else if-else 문은 여러 조건식을 사용할 수 있으며, 첫 번째로 true가 되는 조건식의 코드 블록이 실행됩니다. 조건식이 모두 false일 경우 else 블록이 실행됩니다.

if (조건식1) {
    // 조건식1이 true일 때 실행되는 코드
} else if (조건식2) {
    // 조건식2가 true일 때 실행되는 코드
} else {
    // 모든 조건식이 false일 때 실행되는 코드
}

예제 3: if-else if-else문

import java.util.Scanner;

public class A_if1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("정수: ");
        int num = sc.nextInt();

        if (num > 0) {
            System.out.println("양수이다.");
        } else if (num == 0) {
            System.out.println("0이다.");
        } else {
            System.out.println("음수이다.");
        }
        sc.close();
    }
}

4. switch문

swtich 문은 하나의 변수에 대해 여러 경우의 수를 검사하여 해당하는 코드 블록을 실행합니다. 각 경우는 case 키워드를 사용하여 정의하며, 모든 casefalse일 경우 default 블록이 실행됩니다.

switch (변수) {
    case1:
        // 변수의 값이 값1일 때 실행되는 코드
        break;
    case2:
        // 변수의 값이 값2일 때 실행되는 코드
        break;
    // ...
    default:
        // 모든 case가 만족되지 않을 때 실행되는 코드
}

예제 4: switch문

import java.util.Scanner;

public class A_if2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("성별을 입력하세요(m, f): ");
        char gender = sc.next().charAt(0);

        switch (gender) {
            case 'm':
            case 'M':
                System.out.println("남학생입니다.");
                break;
            case 'f':
            case 'F':
                System.out.println("여학생입니다.");
                break;
            default:
                System.out.println("잘못 입력했습니다.");
        }
        sc.close();
    }
}
profile
초보부터 시작하는 개발자 생활

0개의 댓글