'중첩된 if'는 if(또는) else의 객체인 if 문입니다. 'if'는 다른 if(또는) else 안에 배치됩니다.
구문
아래 주어진 구문을 참조하십시오 -
if (condition1){
if (condition2)
stmt1;
else
stmt2;
}
else{
if (condition3)
stmt3;
else
stmt4;
}

예시
다음은 Nested If Else 조건부 연산자를 실행하는 C 프로그램입니다 -
#include<stdio.h>
void main (){
int a,b,c,d;
printf("Enter the values of a,b,c: \n");
scanf("%d,%d,%d",&a,&b,&c);
if((a>b)&&(a>c)){//Work with 4 numbers//
if(a>c){
printf("%d is the largest",a);
} else {
printf("%d is the largest",c);
}
} else {
if(b>c){
printf("%d is the largest",b);
} else {
printf("%d is the largest",c);
}
}
} 출력
다음 출력이 표시됩니다 -
Enter the values of a,b,c: 3,5,8 8 is the largest
예시
다음은 숫자가 양수인지 음수인지 확인하는 C 프로그램입니다. -
#include <stdio.h>
int main(){
int num;
printf("Enter a number:\n ");
scanf ("%d ", &num);
if(num > 0){
printf("This is positive num:%d\n", num);
}
else if(num < 0){
printf("This is a negative num:%d",num);
} else {
printf("This is a zero:%d",num);
}
return 0;
} 출력
다음 출력이 표시됩니다 -
Run 1: Enter a number: 23 23=This number is positive Run 2: Enter a number: -56 -56=This number is negative