inblog logo
|
DailyCoding
    JAVA

    11. 조건문(Conditional Statement)

    박은서's avatar
    박은서
    Dec 10, 2025
    11. 조건문(Conditional Statement)
    Contents
    1. 조건문(Conditional Statement)1️⃣ 조건문이란?2️⃣ 조건문의 종류3️⃣ 교재 예제

    1. 조건문(Conditional Statement)

    교재 p.88-93 3.1 if-else문

    1️⃣ 조건문이란?

    조건의 참/거짓에 따라 프로그램의 흐름을 다르게 만드는 문법
    상황에 따라 다른 코드가 실행되도록 하는 것

    2️⃣ 조건문의 종류

    1. if 문
    1. if-else 문
    1. else if 문
    1. switch 문 (조건문이지만 형태는 조금 다름)

    1) if 문

    개념
    조건이 true일 때만 코드를 실행함.
    if (조건식) { 실행할 코드 }
    예시
    int age = 20; if (age >= 18) { System.out.println("성인입니다."); }

    2) if-else 문

    개념
    조건이 true면 if 블록 실행, false면 else 블록 실행.
    if (조건식) { 코드1 } else { 코드2 }
    예시
    if (age >= 18) { System.out.println("성인"); } else { System.out.println("미성년자"); }

    3) else if 문

    개념
    여러 조건을 순차적으로 체크할 때 사용.
    if (조건1) { 코드1 } else if (조건2) { 코드2 } else { 코드3 }
    예시
    int score = 85; if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else { System.out.println("C"); }

    3️⃣ 교재 예제

    1) 예제 3-1. 짝수와 홀수 구별하기 (p.89)

    package ex03; import java.sql.SQLOutput; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { int number; Scanner sc = new Scanner(System.in); System.out.print("정수를 입력하시오: "); number = sc.nextInt(); if (number % 2 == 0) { System.out.println("입력된 정수는 짝수입니다."); } else { System.out.println("입력된 정수는 홀수입니다."); } } }
    notion image
    notion image

    2) 예제 3-2. 성적 처리 예제 (p.91)

    package ex03; import java.util.Scanner; public class Grading { public static void main(String[] args) { int grade; Scanner sc = new Scanner(System.in); System.out.print("성적을 입력하시오: "); grade = sc.nextInt(); if (grade >= 90) System.out.println("학점 A"); else if (grade >= 80) System.out.println("학점 B"); else if (grade >= 70) System.out.println("학점 C"); else if (grade >= 60) System.out.println("학점 D"); else System.out.println("학점 F"); } }
    notion image
    notion image
    notion image
    notion image
    notion image

    3) 예제 3-3. 가위, 바위, 보 게임 (p.92)

    package ex03; import java.util.Scanner; public class RockPaperScissor { final int SCISSOR = 0; final int ROCK = 1; final int PAPER = 2; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("가위(0), 바위(1), 보(2): "); int user = sc.nextInt(); int computer = (int) (Math.random() * 3); if ( user == computer ) System.out.println("인간과 컴퓨터가 비겼음"); else if (user == (computer + 1) % 3) System.out.println("인간: " + user + " 컴퓨터: " + computer + " 인간 승리"); else System.out.println("인간: " + user + " 컴퓨터: " + computer + " 컴퓨터 승리"); } }
    notion image
    notion image
    notion image
    notion image
     
    Share article
    Contents
    1. 조건문(Conditional Statement)1️⃣ 조건문이란?2️⃣ 조건문의 종류3️⃣ 교재 예제

    DailyCoding

    RSS·Powered by Inblog