본문 바로가기
Java_language

연산자 & 조건문

by 헬로제이콥 2025. 11. 17.

 

📘 3회차 요약 — 연산자 & 조건문

1. 산술 연산자

연산자의미예
+ 더하기 3 + 2 → 5
- 빼기 5 - 3 → 2
* 곱하기 2 * 4 → 8
/ 나누기 10 / 2 → 5
% 나머지 10 % 3 → 1

2. 비교 연산자

연산자의미
== 같다
!= 다르다
> 크다
< 작다
>= 크거나 같다
<= 작거나 같다

3. 논리 연산자

연산자의미
&& 그리고 (AND)
|| 또는 (OR)
! 부정 (NOT)

4. 대입 연산자

연산자의미
= 값 저장
+= 더해서 저장
-= 빼서 저장
*= 곱해서 저장
/= 나누어서 저장

🧩 조건문 요약

if 문

조건이 참일 때만 실행

if (조건) {
    실행문;
}
 
 

if-else 문

조건이 참이면 if 실행, 아니면 else 실행

if (조건) {
    실행문1;
} else {
    실행문2;
}
 
 

switch 문

하나의 값에 따라 여러 경우 선택

switch(값) {
    case 값1: 실행; break;
    case 값2: 실행; break;
    default: 실행;
}
 

🧪 예제 1 — 점수 입력 후 학점 계산 (if-else)

아주 쉽게 작성된 기초 예제입니다.

 
import java.util.Scanner;

public class GradeExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("점수를 입력하세요: ");
        int score = sc.nextInt();

        if (score >= 90) {
            System.out.println("학점: A");
        } else if (score >= 80) {
            System.out.println("학점: B");
        } else if (score >= 70) {
            System.out.println("학점: C");
        } else if (score >= 60) {
            System.out.println("학점: D");
        } else {
            System.out.println("학점: F");
        }
    }
}

🧪 예제 2 — 짝수 / 홀수 판단 (if 문)

아주 기본적인 if 연습 예제입니다.

 
public class EvenOddExample {
    public static void main(String[] args) {
        int num = 7;

        if (num % 2 == 0) {
            System.out.println("짝수입니다.");
        } else {
            System.out.println("홀수입니다.");
        }
    }
}

🧪 예제 3 — switch로 요일 출력

 
 
public class DayExample {
    public static void main(String[] args) {
        int day = 3;

        switch (day) {
            case 1:
                System.out.println("월요일");
                break;
            case 2:
                System.out.println("화요일");
                break;
            case 3:
                System.out.println("수요일");
                break;
            case 4:
                System.out.println("목요일");
                break;
            case 5:
                System.out.println("금요일");
                break;
            case 6:
                System.out.println("토요일");
                break;
            case 7:
                System.out.println("일요일");
                break;
            default:
                System.out.println("잘못된 번호입니다.");
        }
    }
}

🧪 예제 4 — switch로 간단 계산기

 
public class SimpleCalc {
    public static void main(String[] args) {
        char op = '+';  // 연산자
        int a = 10;
        int b = 5;

        switch (op) {
            case '+':
                System.out.println(a + b);
                break;
            case '-':
                System.out.println(a - b);
                break;
            case '*':
                System.out.println(a * b);
                break;
            case '/':
                System.out.println(a / b);
                break;
            default:
                System.out.println("지원하지 않는 연산자입니다.");
        }
    }
}