जब आप कोई software बनाते है तो आपके सामने ऐसी situation आती है की आपको एक specific code of block को run करना होता है तो ऐसी situation में हम Decision Controlstatement का use करते है। इसमें हम एक condition को check करवाते है और अगर वो condition true होती है तो हम true वाले section के code को execute करवाते है और अगर false हुई तो हम false वाले section को run करवाते है।
नीचे दिए गए code को देखिये
#include<stdio.h>
main()
{
int ramSalary = 10000;
int shyamSalary = 20000;
}
इस program में ram और shyam की salary को 2 variable में store कराया गया है अगर आपसे कहा जाए की जिसकी salary ज्यादा हो use print कराओ तो आप क्या करेंगे कैसे print कराएँगे तो इस situation में हम लोग Decision Control का use करते है। program में decision 3 types से कर सकते है
if statements
if-else statements
switch-case statements
If Statements
//syntax of if statements
if(condition)
{
//block of codes
}
ऊपर दिए गए syntax को देखिये if के बाद parentheses () के अंदर condition और फिर curly braces {} के अंदर वो code लिखा जाता है जो condition के true होने पे execute होगा।
चलिए इसे एक example से समझते है नीचे दिए गए program को देखिये
#include<stdio.h>
main()
{
int a = 20;
int b = 10;
if(a>b)
{
printf("A is Greater Than B");
}
}
ऊपर दिए गए program में if statement लगा के condition लगाई गई है की a बड़ा है b से अगर ये condition true हुई तो if के अंदर लिखा गया printf() function execute होगा अगर condition false हुई तो कुछ भी print नहीं होगा। program को run कराइये।
Decision Control
IF-Else Statement
if statement में अगर condition true होती है तो true वाला block execute होता है लेकिन अगर condition false होती है तो क्या execute होगा तो इसके लिए else का use करते है। इसका syntax नीचे दिया हुआ है।
ifstatement और if-else statement अलग अलग नहीं है बस if में else जोड़ दिया गया है जिससे की अगर condition false होती है तो क्या किया जाना चाहिए ये आप decide कर सकते है
तो चलिए if else statement को implement करके देखते है example के लिए नीचे दिए गए program को देखिये।
#include<stdio.h>
main()
{
int a = 10;
int b = 20;
if(a>b)
{
printf("A is Greater Than B");
}
else
{
printf("B is Greater Than A");
}
}
तो इस program आप देख सकते है की if condition false है तो else वाला part execute होगा।
Else- If Statement
तो अगर आप if else के बीच में एक और condition लगाना चाहते है तो आप else if का use कर सकते है। नीचे दिए गए example को देखिये
#include<stdio.h>
main()
{
int a = 20;
int b = 20;
if(a>b)
{
printf("A Is Greater");
}
else if(b>a)
{
printf("B Is Greater");
}
else
{
printf("A And B Are Equal");
}
}
तो आप देख सकते की if के बाद if else statement को लिखा गया है तो इस तरीके से आप लिख सकते है जरुरी नहीं है की आप else if एक साथ में ही लिखे नीचे दिए गए example को देखिये इसमें else के अंदर if else लिखा गया है दोनों program सही है बस लिखने का तरीका अलग अलग है
#include<stdio.h>
main()
{
int a = 10;
int b = 20;
if(a>b)
{
printf("A Is Greater");
}
else
{
if(b>a)
{
printf("B Is Greater");
}
else
{
printf("A And B Are Equal");
}
}
}
Nested IF-Else Statement
Nested IF-Else Statement में हम एक if के अंदर multiple if या ये बोले की एक condition के अंदर बहुत सी condition होती है तो उसे हम लोग Nested IF-Else Statement बोलते है आइये इसे example से समझते है
#include<stdio.h>
main()
{
int a = 15;
int b = 20;
int c = 25;
if(a>b)
{
if(a>c)
{
printf("A Is Greater");
}
else
{
printf("C Is Greater");
}
}
else if(b>c)
{
printf("B Is Greater");
}
else
{
printf("C Is Greater");
}
}
तो आप देख सकते है पहले वाले if के अंदर एक और if है और उसका else भी लिखा गया है। तो इस तरह से आप nested if else का use कर सकते है।
Switch Statement
अपने अपने घर में light के switch देखे होंगे जिसमे हर एक switch का अलग अलग काम होता है fan के लिए अलग switch , light के लिए अलग switch दिया होता है तो उसी तरह programming में switch statement काम करता है नीचे दिए गए example देखिये
#include<stdio.h>
main()
{
int num = n;
switch(num)
{
case 1:
//case 1 statement execute
break;
case 2:
//case 2 statement execute
break;
case 3:
//case 3 statement execute
break;
case 4:
//case 4 statement execute
break;
default
// if not match any case default statement execute
}
}
switch में integer value pass की जाती है और फिर जिस भी case से वो value match करती है वो statement execute हो जाता है अगर कोई भी value case से match नहीं करती है तो default वाला case execute होता है। आप इसे ऐसे समझिये की switch आपका light का switch है और case आपके fan और light on off करने के button है जो भी button आप press करेंगे वो use on और off कर देगा। इसका example नीचे दिया गया है
#include<stdio.h>
main()
{
int num;
printf("Enter Number Between 1 To 4 = ");
scanf("%d",&num);
switch(num)
{
case 1:
printf("You Enter 1");
break;
case 2:
printf("You Enter 2");
break;
case 3:
printf("You Enter 3");
break;
case 4:
printf("You Enter 4");
break;
default:
printf("You Enter Wrong Number");
break;
}
}
अगर आप printf() और scanf() function के बारे में नहीं जानते तो आप यहाँ पे click करके पढ़ सकते है।
इस program को run कराइये और 1 to 4 के बीच में कोई भी number enter करिये तो जो भी number आप enter करेंगे वो उस case के statement को print कर देगा अगर आप 1 से 4 के अलावा कोई भी number enter करते है तो default case execute होगा। तो आपको Decision Control समझ में आ गया होगा।
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.