एक 'नेस्टेड अगर' एक if स्टेटमेंट है जो कि या तो (या) किसी और का ऑब्जेक्ट है। 'if' दूसरे के अंदर रखा जाता है अगर (या) और।
सिंटैक्स
नीचे दिए गए सिंटैक्स को देखें -
if (condition1){
if (condition2)
stmt1;
else
stmt2;
}
else{
if (condition3)
stmt3;
else
stmt4;
}

उदाहरण
नेस्टेड इफ एल्स कंडीशनल ऑपरेटर्स को निष्पादित करने के लिए सी प्रोग्राम नीचे दिया गया है -
#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