https://www.rakeshmgs.in/search/label/Template
https://www.rakeshmgs.in
RakeshMgs

Decision Making in Programming C | Using if else, if, else if, else statement

Updated:

 

Decision making in C

आप अपने program में कौनसे statements को execute करना चाहते है और कौनसे statements को skip करना चाहते है ये आप खुद decide कर सकते है। इसे decision making कहते है। ज्यादातर decision making किसी condition के base पर की जाती है।

एक particular condition आने पर आप मनचाहे statements को execute कर सकते है। इसके लिए आप कुछ built in statements को यूज़ करते है। क्योंकि ये statements conditions के साथ काम करते है इसलिए इन्हें conditional statement भी कहा जाता है।
और क्योंकि ये statement program में execution को control करते है इसलिए इन्हें control statements भी कहा जाता है।

1. if statement
2.switch statement
3.conditional operator statement (? : operator)
4.goto statement

Decision making with if statement

1.Simple if statement
2.if....else statement
3.Nested if....else statement
4.Using else if statement

Simple if statement

	
	
if(expression) { statement inside; } statement outside;

If statement curly braces {} के द्वारा एक block define करता है। जब condition true होती है तो इस block में दिए गए statement execute होते है।

यदि condition false हो तो इस पुरे block को compiler skip कर देता है। यदि उपर दिए गए example में if statement का use किया जाये तो program को इस प्रकार लिखा जा सकता है।

if...else statement

If else statement को if statement का ही part माना जाता है। लेकिन इसमें else block और add किया जाता है। Else block में दिए गए statement तब execute होते है जब if की condition false हो जाती है।

जैसा की आपको पता है if की condition true होने पर if block में दिए गए statements execute होते है। लेकिन आप ये भी decide कर सकते है की यदि condition false हो तो क्या किया जाना चाहिए। इसके लिए आप else block यूज़ करते है। ये block हमेशा if block के बाद में आता है।

	
	
if(expression) { statement block1; } else { statement block2; }

इस block में वो statements लिखे जाते है जो की condition false होने पर execute होंगे। यदि ऊपर दिए गए उदाहरण में if else statement का प्रयोग किया जाये तो आप उसे इस प्रकार लिख सकते है।

	
	
#include <stdio.h> void main( ) { int x, y; x = 15; y = 18; if (x > y ) { printf("x is greater than y"); } else { printf("y is greater than x"); } }

Output

y is greater than x

Nested if....else statement

यदि आप if और else के बीच में एक और condition लगाना चाहते है तो ऐसा आप else if block define करके कर सकते है।

	
	
if(expression) { statement block1; } else if { statement block2; } else { statement block3; }

यदि expression गलत है, तो statement-block3 show किया जाएगा, अन्यथा निष्पादन जारी रहता है और पहले के अंदर प्रवेश करता है यदि अगले के लिए चेक प्रदर्शन करना है यदि statement-block, जहां अगर expression 1 true है, तो statement-block 1 executed हो गया है अन्यथा statement-block 2 executed है|

else if ladder

else-if ladder सामान्य रूप

	
	
if(expression1) { statement block1; } else if(expression2) { statement block2; } else if(expression3) { statement block3; } else { default statement; }

expression का test ऊपर (सीढ़ी से) नीचे की ओर से किया जाता है। जैसे ही एक true conditon मिलती है, उससे जुड़े statement को executed किया जाता है।


आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com