C programming में हम इस से पहले C if statement और C if else statement के बारे में सीख चुके हैं यदि आपने नही सीखा तो पहले सिखले तब nested if statement आसानी से समझेंगे ।
इस tutorial में हम C language में Nested if statements के बारे में सीखेंगे वो भी उदाहरणो के साथ ।
Nested if statements in C programming | Hindi
जब किसी if statement के अंदर या else statement के अंदर if statement या if else statement को लिखा जाता हैं तो उसे nested if statement कहते हैं ।
साधारण हिन्दी भाषा में समझे तो जैसे हम if block और else block बनाते हैं यदि उन block में हम if statement लिखते हैं या if else statement लिखते हैं या else block में if statement लिखते हैं या if else statement लिखते हैं यानिकी if statement के अंदर ओर if statement या if else statement को लिखना nested if statement कहलाता हैं ।
चलिये C programming में nested if statement के syntax से समझते हैं ।
Syntax-
if(condition) {
//Nested if else inside the block (body) of "if"
if(condition) {
//Statements inside the block(body) of nested "if"
}
else {
//Statements inside the block(body) of nested "else"
}
}
else {
//Statements inside the block(body) of "else"
}
या बिलकुल साधारण syntax –
if (condition1)
{
// executes when condition1 is true
if (condition2)
{
// executes when condition2 is true
}
}
ऊपर दिये गए syntax में आप देख सकते हैं की if block के अंदर एक ओर if else statement लिखी गयी हैं इस तरह से कितने भी if statement या if else stetment लिखे जा सकते हैं जिसे nested if statement कहते हैं ।
यदि if condition true होती हैं तो compiler if ब्लॉक में जाता हैं चुकी if block के अंदर एक और condition हैं तो compiler उसे भी चेक करता हैं यदि वो भी true हो जाती हैं तो उसके ब्लॉक में जो कोड़ हैं execute हो जाता हैं ।
यदि false हैं तो else block execute हो जाता हैं । ऐसे ही समान process हैं जो की हमने if else statement में सीखा था ।
चलिये C program से nested if statements को समझते हैं ।
Examples of Nested if statements in C language :-
Example 1 –
#include <stdio.h>
int main() {
int x = 15;
if (x == 15) // First if statement
{
if (x < 20) // Nested - if statement
// Code will be executed when the condition is true
{
printf("x is smaller than 20\n");
}
// Code will be executed when the condition is true
if (x < 17){ // Nested - if statement
printf("x is smaller than 17 too\n");
}
else
{ // Nested - else statement
//Code will be executed when the condition is false
printf("x is greater than 16");
}
}
return 0;
}
Output –
x is smaller than 20
x is smaller than 17 too
उपर दिये गए उदाहरण में हमने if condition बनाई फिर if block में एक ओर if condition बनाई और एक if else statement का भी उपयोग किया ।
जैसे की आप जानते ही हैं यदि कंडिशन true होती हैं तो वह ब्लॉक execute होता हैं जो की आप उपर दिये गए उदाहरण से समझ सकते हैं ।
Example 2 –
#include <stdio.h>
int main() {
int a = 12;
if (a<6) // First if statement
{
printf("a is less than 6 ");
}
else {
if (a>6)
{
printf("a is greater than 6 ");
}
else{
printf("a is less than 6 ");
}
}
return 0;
}
Output –
a is greater than 6
उपर दिये गए उदाहरण में हमने else block में if else statement को लिखा तो इसका मतलब यह हैं की अगर हमे condition के अंदर ओर condition बनानी हैं तब हम nested if statement का उपयोग कर सकते हैं । यहाँ पर ये matter नही करता की आप if block में बनाए या else block मे programmer अपनी जरूरतों के अनुसार किसी भी ब्लॉक चाहे else block हो या if ब्लॉक किसी में भी और if else statement बना सकते हैं जैसे –
if(condition 1)
{
if(condition 2)
{
if(condition 3)
{
} condition 3 block closed
}condition 2 block closed
} condition 1 main block closed
else
{
}
ऐसे ही आप else block में भी कितने ही if या if के अंदर if conditions बना सकते हैं इसे ही nested if statement कहते हैं ।
इस तरह C programming में nested if statements का उपयोग किया जाता हैं ।
Recommended Posts:-