본문 바로가기

C language

[C언어] chat got 를 이용해 nested if statement code 예제 알아보기

nested if statement 란 중첩 if 문을 의미한다.

 

즉, 다른 if 문 내에 하나 이상의 if 문이 포함된 구조이다.

#include <stdio.h>

int main()
{
int age = 25;
int salary = 60000;

if (age > 18) {
printf("You are an adult.\n");

if (salary > 40000) {
printf("You have a decent income.\n");
} else {
printf("You might want to consider a higher paying job.\n");
}
} else {
printf("You are a minor.\n");
}

return 0;
}

 

자 , 한줄 씩 알아보자
 
우선

 

age  변수는 25,

 

salary = 60000 이다.
 
age> 18 이라면 You are an adult 를 출력하고,
 
그에 이어 salary > 40000 이라면 You have a decent income 을 출력한다
 
if 문 내에서 else, 즉 salary <= 40000 이라면 You might want to consider a higher paying job 을 출력한다.
 
그러나 age <=18 이라면, You are a minor 를 출력하며 프로그램을 마친다.