- Introduction to C conditional statement in Hindi
- C conditional statement in Hindi
- Different type of C conditional statement in Hindi
Introduction to conditional statement
C language मे condition statement बहुत ही important होता है | क्योकि conditional statement के द्वारा बहुत सारे program बना सकते है |
हम अपने program मे कौन सा code कब execute करना चाहते है | इसे हम decide कर सकते है | और इसे हम decision making statement भी कहते है |
conditional statement के द्वारा अपने code को control कर सकते है | इसलिए इसे control statement भी कहा जाता है |
मान लीजिये आपके पास 2 numbers है | और दोनों numbers मे सबसे बड़ा नंबर print करना चाहते है | तो कैसे करेंगे | उदाहरण के लिए निचे दिये गये statement देखे |
#include <stdio.h> #include <conio.h> void main() { int num1 = 40; int num2 = 50; }
ऊपर दिये गये code मे 2 variables declare किये गये है | जिसमे num2 variable की value अधिक है | और आप चाहते है | num2 variable की value को print करना तो इसके लिए conditional statement का प्रयोग करना होगा | अब निचे दिये गये block को देखे | जिसके साथ conditional statement का प्रयोग किया जाता है
- if
- else if
- switch
If Statement
C language मे if statement के द्वारा conditional statement की operation को perform कर सकते है | if statement मे define किये गये condition true होते है | तो program execute होता है | और condition false होता है | तो else मे define किया गया message print हो जाता है | अब निचे दिये गये example को देखे और समझे |
#include <stdio.h> #include <conio.h> void main() { int num1 = 40; int num2 = 50; if(num1<num2) { printf("variable is num2 : ",num2); } return 0; }
OUT PUT
variable is num2 : 50
ऊपर दिये गये program मे conditional operator का use करते हुए एक condition लगायी गयी है। जिसमे condition है | अगर num1 variable की value num2 variable की value से छोटी है | तो num2 की value print करो |
If-Else Statement
C language मे If-Else Statement दोनों ही if statement के part है | लेकिन यहाँ पर condition के साथ साथ दो statement होते है | यानी अगर condition true होगा तो if statement का code execute होगा और condition false होने पर else statement का code execute हो जाता है | अब निचे दिये गये example को देखे और समझे |
#include <stdio.h> #include <conio.h> void main() { int num1 = 40; int num2 = 50; if(num1>num2) /* condition is false so execute code of else part */ { printf("variable is num2 : ",num2); } else { printf("variable is num1"); } return 0; }
OUT PUT
variable is num1
else if statement
अगर आप condition के साथ एक और condition लगाना चाहते है तो आप else if statement का use कर सकते है | else if statement मे एक condition false होने पर दूसरा condition चेक किया जाता है | अब निचे दिये गये example को देखे |
#include <stdio.h> #include <conio.h> void main() { int num1 = 40; int num2 = 50; if(num1>num2) /* condition is false so execute code of else part */ { printf("variable is num2 : ",num2); } else if(num1>num2) /* condition is false so execute code of else part */ { printf("variable is num2 : ",num2); } else { printf("variable is num1"); } return 0; }
OUT PUT
variable is num1
Switch Statement
C language मे switch statement भी if statement की तरह ही होता है | पर switch statement मे condition के जगह case चेक किया जाता है |
आप switch statement का case अपने अनुसार लिख सकते है | और अपने अनुसार code भी execute कर सकते है |
switch statement का case character भी हो सकता है | और numbers भी हो सकता है | और symbols भी हो सकता है | character,numbers,symbols इन तीनो category का use case के लिए किया जाता है |
Case एक variable से match किया जाता है। जो case variable से match हो जाता है वही case execute हो जाता है | अगर case match नही होता है | तो default case execute हो जाता है | अब निचे दिये गये example देखे |
#include <stdio.h> #include <conio.h> int main() { int number; printf("Enter a number : "); scanf("%d",&number); switch(number) { case 1: printf("First Case"); break; case 2: printf("Second Case"); break; default: printf("Default Case"); break; } return 0; }
ऊपर दिये गये code के अनुसार हम ने 1 number enter किया है | और 1 number enter करने के बाद जो result आया है | उसे हम निचे दे रहे है |
OUT PUT
First Case
C – Operators | C – Loop |
Previous | Next |
---|