Decision making in C | nested if statement in C in Hindi 2022

इस आर्टिकल में  हम Decision making in C | nested if statement in C in Hindi 2022  और nested if स्टेटमेंट क्या हैं के बारे में जानेंगे.  दोस्तों Hindi me IT में आपका बहुत बहुत अभिनान्दन हैं.

last आर्टिकल में हमने if और if-else स्टेटमेंट के बारे में पढ़ा था। और इस आर्टिकल में हम nested if, Switch और nested switch के बारे में पढ़ेंगे।

nested if statement in C in Hindi (nested if स्टेटमेंट क्या हैं )

nested if को आसान शब्दों में समझते हैं. C प्रोग्रामिंग में if statement के अंदर if statement लिखते हैं उसे nested if कहते हैं. यानी की if के अंदर if हो तो  nested if statement कहते हैं।

nested if statement in C in Hindi 2022

Syntax :

if(condition1) {
  statement; //executes तब होगी जब condition1 true  
  if(condition2) {
    statement; // executes तब होगी जब condition2 true 
  }
}

इस syntax में ये समझने की कोशिश करते हैं. अगर पहली condition true हो जाती हैं. तो c का compiler if statement के अंदर आ जायेगा और if statement के अंदर लाइन को executes करेगा।

इसी  if के अंदर एक और if हैं जिसे हम condition2 कह रहे हैं। अगर condition2 भी true हो जाती हैं तो compiler उसके अंदर लिखी सभी statement को executes कर देगा। अगर condition2 false हो जाती हैं तो compiler दूसरे if के अंदर नहीं जायेगा पहले वाले if से ही बाहर आ जायेगा।

nested if को एक रियल लाइफ example से समझते हैं.

जैसे अगर आपकी age 18 या 18 से ज्यादा हैं तो आप गाड़ी चला सकते हैं. अगर आपकी age 18 हैं. तो आप पहली कंडीशन पास कर लेते हैं।  उसके बाद if के अंदर एक और कंडीशन चेक करेंगे ड्राइवर लाइसेंस की।

अगर आपके पास ड्राइवर लाइसेंस हैं तभी आप गाड़ी चला सकते हैं. अगर आपके पास लाइसेंस नहीं हैं तो गाड़ी नहीं चला सकते हैं.

यानी की पहली कंडीशन age की दूसरी condition ड्राइवर लाइसेंस जो की पहले कंडीशन के अंदर होगी। अगर दोनों कंडीशन true हो जाती हैं। गाड़ी चला सकते हैं अगर दोनों में से एक भी कंडीशन false होती हैं तो आप गाड़ी नहीं चला सकते हैं।

Example 1:-

#include<stdio.h>

int main() {
  int a = 19; //user की age
  int b = 1; //have license or not
  if(a>=18) {
    printf("Congratulation! Your are eligible to Drive \n");
    if(b==1) {
       printf("Great! You can Drive safely);
    } 
    else {
      printf("You are eligible but you do not possess a license");
    }
  }
  else {
   printf("OH! you are not eligible");
  }
  return 0;
}

output :- 

Congratulation! You are eligible to Drive
Great! You can Drive safely

Explanation  example 1:-

पहली if कंडीशन यूजर की age चेक कर रही हैं। अगर यूजर की age 18 या 18 से अधिक हैं । तो पहली कंडीशन true हो जायेगी। इस example में हमने यूजर की age 19 दी यानी a की value 19 हैं जो की 18 या 18 से अधिक हैं। जो की पहली कंडीशन true होने पर if के अंदर की सभी statement को execute करेगा और output में मैसेज प्रिंट कर दिया “Congratulation! You are eligible to Drive“;

उसके बाद कम्पाइलर आगे बढ़ा उसे एक और if कंडीशन मिली जिसमे हम चेक कर रहे हैं b == 1 ( one का “यहाँ मतलब हैं की यूजर के पास लाइसेंस हैं “) जो की true हैं क्योकिं b की value one ही हैं. इसलिये कम्पाइलर ने एक और मैसेज प्रिंट कर दिया “Great! You can Drive safely

Example 2 :-

#include<stdio.h>

int main() {
  int a = 17 ; //user की age
  int b = 1; //have license or not
  if(a>=18) {
    printf("Congratulation! Your are eligible to Drive \n");
    if(b==1) {
       printf("Great! You can Drive safely);
    } 
    else {
      printf("You are eligible but you do not possess a license");
    }
  }
  else {
   printf("OH! you are not eligible");
  }
  return 0;
}

Output :-

OH! you are not eligible

Explanation  example 2:-

पहली if कंडीशन यूजर की age चेक कर रही हैं। अगर यूजर की age 18 या 18 से अधिक हैं । हमने example 2 में यूजर की age 17  दी यानी a की value 17 दी हैं  जो की 18 से कम हैं। जो की कंडीशन False हैं इसलिए कम्पाइलर if के अंदर नहीं जायेगा if को छोड़ कर सीधा else part पर आ जायेगा और  “OH! you are not eligible”  . output में मैसेज प्रिंट हो जायेगा।

Example 3 :-

#include<stdio.h>

int main() {
  int a = 20 ; //user की age
  int b = 0 ; //have license or not
  if(a>=18) {
    printf("Congratulation! Your are eligible to Drive \n");
    if(b==1) {
       printf("Great! You can Drive safely);
    } 
    else {
      printf("You are eligible but you do not possess a license");
    }
  }
  else {
   printf("OH! you are not eligible");
  }
  return 0;
}

Output :-

Congratulation! Your are eligible to Drive
You are eligible but you do not possess a license

Explanation  example 3 :-

पहली if कंडीशन यूजर की age चेक कर रही हैं। अगर यूजर की age 18 या 18 से अधिक हैं । तो पहली कंडीशन true हो जायेगी। इस example में हमने यूजर की age 19 दी यानी a की value 19 हैं जो की 18 या 18 से अधिक हैं। जो की पहली कंडीशन true होने पर if के अंदर की सभी statement को execute करेगा और output में मैसेज प्रिंट कर दिया “Congratulation! You are eligible to Drive“;

उसके बाद कम्पाइलर आगे बढ़ा उसे एक और if कंडीशन मिली जिसमे हम चेक कर रहे हैं b == 1 ( one “यहाँ मतलब हैं की यूजर के पास लाइसेंस हैं  और 0 का मतलब हैं  लाइसेंस नहीं हैं “) जो की false हैं क्योकिं b की value 0 दे रहे हैं जो की one के बराबर नहीं हैं. इसलिये कंडीशन false हो रही हैं और कम्पाइलर सीधा पहली कंडीशन के अंदर else स्टेटमेंट पर रहा हैं  ने एक मैसेज प्रिंट कर दिया “You are eligible but you do not possess a license

By reference :- https://www.w3schools.com/c/c_conditions.php

आशा  करता हूँ Decision making in C | nested if statement in C in Hindi 2022  और nested if स्टेटमेंट क्या हैं आपको पसंद आई होगी. अगर आपको इससे related कोई question पूछना हैं तो आप comment करके पूछ सकते है. अगर हमारे व्दारा की जानकर अच्छी लगी हो तो आपने दोस्तों के साथ जरुर शेयर करें. धन्यबाद

FAQ प्रश्न

what is नेस्टेड if स्टेटमेंट in C in Hindi?

जब प्रोग्राम में ऐसे लॉजिक की जरूरत होती हैं. जिसमें प्रोग्रामर if condition के अंदर if condition use करता हैं। तो उसे nested if statement कहते हैं।

Related Posts

Leave a Comment