Menu
Coddy logo textTech

중첩 If-Else

Coddy Rust 여정의 Fundamentals 섹션에 포함된 레슨 — 75개 중 28번째.

if-elif-else 문을 서로 중첩할 수 있습니다. 이를 통해 계층적인 의사 결정 구조를 만들 수 있습니다.

예를 들어:

if age > 18 {
    if hasLicense {
        println!("You can drive");
    } else {
        println!("Get a license first");
    }
} else {
    println!("Too young to drive");
}

무한히 중첩될 수 있습니다:

if condition1 {
	if condition2 {
		if condition3 {
		    // condition1, condition2, condition3가 모두 참인 경우
		    ...
		}
	}
}
challenge icon

챌린지

초급

롤러코스터를 탈 수 있는지 확인하는 프로그램을 만드세요. 요구 사항은 다음과 같습니다:

  • 최소 12세 이상이어야 함
  • 키가 150cm보다 커야 함
  • 두 요구 사항을 모두 충족하지만 15세 미만인 경우, 성인 보호자가 동반되어야 함

각 경우에 대해 정확히 다음 메시지들을 출력하세요:

  • 나이가 너무 어린 경우: Sorry, you're too young
  • 키가 충분히 크지 않은 경우: Sorry, you're not tall enough
  • 15세 미만이고 성인이 동반되지 않은 경우: Sorry, you need an adult with you
  • 15세 미만이고 성인이 동반된 경우: You can ride with adult supervision!
  • 15세 이상이고 키가 충분히 큰 경우: You can ride by yourself!

치트 시트

if-elif-else 문을 서로 중첩하여 계층적인 의사 결정 구조를 만들 수 있습니다:

if age > 18 {
    if hasLicense {
        println!("You can drive");
    } else {
        println!("Get a license first");
    }
} else {
    println!("Too young to drive");
}

중첩은 무한히 가능합니다:

if condition1 {
    if condition2 {
        if condition3 {
            // condition1, condition2 그리고 condition3가 참인 경우
            ...
        }
    }
}

직접 해보기

use std::io;

fn main() {
    let mut age_input = String::new();
    let mut height_input = String::new();
    let mut adult_input = String::new();
    
    io::stdin().read_line(&mut age_input).unwrap();
    io::stdin().read_line(&mut height_input).unwrap();
    io::stdin().read_line(&mut adult_input).unwrap();
    
    let age: i32 = age_input.trim().parse().unwrap();
    let height: i32 = height_input.trim().parse().unwrap();
    let has_adult: bool = adult_input.trim().parse().unwrap();

    // 아래에 코드를 작성하세요
    
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Fundamentals의 모든 레슨