Decision making statements को हम conditional statements (condition , परिस्तिथि के हिसाब से) भी कहते है। decision making स्टेटमेंट से मेरा मतलब है की जैसे स्कूल में हमें marks के हिसाब से grade मिलते है (जैसे A का मतलब होता था 75 % से ज्यादा मार्क्स , B का मतलब होता था 60% से ज्यादा and 75% से कम।

उसी प्रकार से programming में भी condition के according program को execute (चालना) करना है तो decision making स्टेटमेंट की जरुरत पड़ती है।

अब जैसे एक condition है हमारे पास, यदि किसी भी व्यक्ति की उम्र 18 से ज्यादा है तभी वह वोट दे सकता है। उम्र 18 से कम है तो वह वोट नहीं दे सकता।
तो ऐसी problems को programming से हल करने के लिए हमें decision making स्टेटमेंट की जरुरत पड़ती है।

Types of Decision-making statements

  1. if statement
  2. if-else statement
  3. Nested if-else statement
  4. else if ladder
  5. Switch-case statement

1. if statement

if statement एक प्रकार की decision making statement है, जिसमें यदि दी गयी condition true होती है तो if block की body execute होती है (या कह सकते है की if block execute होता है)। हम अपने प्रोग्राम में एक से अधिक if statement का उपयोग कर सकते है।

Syntax:-

if (condition) {
  	// condition is true then execute
	// if body or if block
  	// statements
}

Example:-

हम एक program लिखेंगे जिसमें user की age यदि 18 से ज्यादा होगी तो print होगा “you are eligible for vote” age यदि 18 से काम होगी तो print होगा “you are not eligible for vote”.

#include <stdio.h>

int main() {
    int age = 20;

    if (age > 18) {
        printf("you are eligible for vote\n");
    } 
    
    if(age < 18) {
        printf("you are not eligible for vote\n");
    }

    return 0;
}

// output
// you are eligible for vote

2. if else statement

if-else statement एक प्रकार की decision making statement है, जिसमें यदि दी गयी condition true होती है तो if block की body execute होती है (या कह सकते है की if block execute होता है)। यदि if में दी गयी condition true नहीं होती है तो else block में लिखी गयी statements execute होती है। else में हमें कोई भी conditon देने की जरुरत नहीं पड़ती है।

Syntax:-

if (condition) {
  	// condition is true then execute
	// if body or if block
  	// statements
} else {
   // condition is false then execute 
   // else body or else block 
   // statements 
}

// ----------- OR ----------------

if (condition) 
{
  	// condition is true then execute
	// if body or if block
  	// statements
} 
else 
{
   // condition is false then execute 
   // else body or else block 
   // statements 
}

Example:-

हम एक program लिखेंगे जिसमें user की age यदि 18 से ज्यादा होगी तो print होगा “you are eligible for vote” age यदि 18 से काम होगी तो print होगा “you are not eligible for vote”.

#include <stdio.h>

int main() {
    int age = 20;

    if (age > 18) {
        printf("you are eligible for vote\n");
    } else {
        printf("you are not eligible for vote\n");
    }

    return 0;
}

// output
// you are eligible for vote

3. Nested if-else statement

Nested if-else statement एक प्रकार की decision making statement है, जिसमें एक if statement की अंदर एक ओर if statement होती है , जिसे हम nested if statement कहते है।

Syntax:-

if (condition) {
  	// condition is true then execute
	// if body or if block
  	// statements
    if (condition) {
    	// nested if 
    } else {
    	// nested else 
    }
} else {
   // condition is false then execute 
   // else body or else block 
   // statements 
}

// ----------- OR ----------------

if (condition) 
{
  	// condition is true then execute
	// if body or if block
  	// statements
    if (condition) 
    {
    	// nested if 
    } 
  	else 
    {
    	// nested else 
    }
} 
else 
{
   // condition is false then execute 
   // else body or else block 
   // statements 
}

Example:-

हम एक program लिखेंगे जिसमें user की age यदि 19 से कम होगी तो nested if में check करेंगे की age क्या 13 से ज्यादा है, ज्यादा है तो print करेंगे “you are teenage” यदि 13 से कम है तो print करेंगे “you are child”, ऐसा नहीं हुआ तो बाहर वाले if के else में print करेंगे “you are older than 19 years”.

#include <stdio.h>

int main() {
    int age = 12;

    if(age < 19) {
        if(age > 13) {
            printf("you are teenage\n");
        } else {
            printf("you are child\n");
        } 
    } else {
        printf("you are older than 19 years\n");
    }
}

// output
// you are child


4. else if ladder statement

else-if statement एक प्रकार की decision making statement है, जिसमें else if statement में codition देनी पड़ती यदि condition true होती है, तो else if block execute होता है।

  • बिना if statement के हम else if statement नहीं लगा सकते।
  • last else को छोड़कर सभी else के साथ if होता है, इसलिए else if statement कहते है।
  • जब हमें multiple conditions check करनी होती है तब हम else if statement लिखते है।

Syntax:-

if(condition) {
  // statememt
} else if(condition) {
  // statement
} else if (condition) {
  // statement
} else {
  // statement
}

Example:-

हम एक program लिखेंगे जिसमें user की age यदि user की age 13 कम होगी तो “child” print कराएंगे, यदि age 12 अधिक & 20 से कम होगी तो “teenage” print कराएंगे, यदि age 19 अधिक & 40 से कम होगी तो “adult” print कराएंगे, यदि age 39 अधिक & 60 से कम होगी तो “Middle Age Adult” print कराएंगे, age 60 से अधिक होने पर “Senior Adult” print कराएंगे।

if(condition) {
  // statememt
} else if(condition) {
  // statement
} else if (condition) {
  // statement
} else {
  // statement
}

5. Switch-case statement

Switch case statement एक प्रकार की decision making statement है, switch statement में एक expression (key) देनी पड़ती है जैसे नीचे syntax में दर्शाया गया है। case में जहां पर constant-expression की जगह key के according संभावित value देनी पड़ती है। यदि expression की value constant-expression के equal है तो case में लिखा कोड execute होगा और break keyword switch statement से बाहर निकाल देगा।

ऐसी प्रकार से हम एक से अधिक case लिख सकते है। यदि कोई भी case match नहीं होता है , तो default statement के बाद लिखा गया code execute होता है।

switch (expression)
    {
    case /* constant-expression */:
        /* code */
        break;
    
    default:
        break;
    }
 

Example –

एक program बनाएंगे जिसमे number के according English में word print होगा, जैसे यदि number 1 हुआ तो “one”, number 2 हुआ तो “two”, ऐसी प्रकार से 5 तक print करेंगे। 5 सेट अधिक होने पर “number is greater then 5” print कर देंगे।

#include <stdio.h>

int main()
{
    int n = 6;

    switch (n)
    {
    case 1:
        printf("one");
        break;
    case 2:
        printf("two");
        break;
    case 3:
        printf("three");
        break;
    case 4:
        printf("four");
        break;
    case 5:
        printf("five");
        break;

    default:
        printf("number is greater then 5");
        break;
    }
  
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *