What is Conditional Statements in C in Hindi - C में Conditional Statements क्या है
साधारणत: C में Conditional Statements का Use Condition के आधार पर Decision लेने के लिए होता है।ये Statements Condition के True या False होने की स्थिति में Program में विभिन्न Blocks के Code को Execute करने की अनुमति देते है तथा Program के Execution के क्रम को बदल देते है
Types of Conditional Statements in C in Hindi - C में Conditional Statements के प्रकार
C में Conditional Statements के निम्न होते है।
1) if Statement
2) if--else Statement
3) Nested if Statement
4) else--if ladder Statement
5) switch Statement
if Statement
C में if Statement, Conditional Statement का सबसे Basic Form है यदि Condition True है तो यह Condition को मूल्यांकित करता है और Block के Code को Execute करता है और यदि Condition False है तो यह Block के Code को Skip कर देता हैSyntax:
if (condition)
{
statement1;
}
ऊपर दिया हुआ condition एक expression है जो True या False को evaluates करता है
यदि Condition True है तब तो if block के अंदर का Code Execute होगा नही तो इसे छोड़ दिया जायेगा
Program for if Statement
#include <stdio.h>
int main()
{
int a = 10;
int b= 15;
if(a<b)
{
printf ("a is smaller than b");
}
return 0;
}
a is smaller than b
Explanation:
1)ऊपर के Program में हमने दो Variables a और b को क्रमश: 10 और 15 Value से initialize किया है
2) उसके बाद हमने if के बाद (a<b) एक Condition Expression लगाया है चूकि a की Value b से छोटी है अतः Condition True होगा और if के बाद
का Code Execute हो जायेगा
का Code Execute हो जायेगा
3) यदि a की Value b से बड़ी होती तो Condition False हो जाता और कोई भी Statement Run नही होता है।
if-else Statement
if Statement की सबसे बड़ी कमी यह है कि यदि Condition True है तब तो Block के Code Execute होंगे लेकिन Condition False होने पर कोई भी Statements Execute नही होगाबस इसी कमी का समाधान if-else Statement है जिसमे Condition के True होने पर एक Block के Code Execute होंगे और Condition के False होने पर अन्य Block के Code Execute होंगे
Syntax:
if (condition)
{
Statement1;
}
else
{
Statement1;
}
Program for if--else Statement Statement
#include <stdio.h>int main() { int a = 15; int b= 10; if(a<b)
{
printf ("a is smaller than b");
}
else
{
printf ("b is smaller than a");
}
return 0;
}
b is smaller than a
Explanation:
1) ऊपर Program में दो Variables और b को क्रमश: 15 और 10 Value Assign किया गया है
1) ऊपर Program में दो Variables और b को क्रमश: 15 और 10 Value Assign किया गया है
2)if के बाद Condition मे a को b से छोटा करके दिया गया है जो True होने पर if Block के Statements Execute होंगे
3) चूंकि a और b की Value के आधार पर Condition False है अत: else के बाद का Statement Execute होगा
Nested if Statement
एक if या if- else Statements के अंतर्गत दूसरा if या if-else Statement nested-if Statement कहलाता हैएक Nested if statement आपको अन्य if या if-else block के अंदर if-else statement का निर्माण करने की अनुमति देता है
ताकि आप कई निर्णयों के आधार पर अपना कार्य कर सकें यह आपको कई Conditions का मूल्याकंन करते हुए अधिक जटिल निर्णय लेने वाले की अनुमति देता है
Syntax:
if (condition1)
{
if or else statement
}
else
{
if or else statement
}
Program for Nested if Statement Statement
#include <stdio.h>
int main() {
int a, b, c;
// Input three numbers from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
//Using Nested if-else statements to find the largest number
if (a >= b) //Outer if block
{
if (a >= c) //Inner if block
{
printf("%d is the largest number.\n", a);
}
else
{
printf("%d is the largest number.\n", c);
}
}
else //else block start if Outer if block condition false
{
if (b >= c)
{
printf("%d is the largest number.\n", b);
}
else
{
printf("%d is the largest number.\n", c);
}
}
return 0;
}
Enter three numbers: 15 10 25
25 is the largest number.
Explanation:
सबसे पहले User से तीन Numbers Input के रूप मे लिया जाएगा
Outer if block अगर True है तो निम्न कार्य होगा
1) पहले Outer if block के Condition को check किया जायेगा कि क्या a, b से बड़ा है यदि है तो Control Inner if block में जाकर पुन: a को c से Check किया जायेगा कि क्या a, c से बड़ा है
यदि Condition True है अर्थात a बड़ा है तो a को Print किया जायेगा
यदि यदि Condition False है अर्थात c बड़ा है तो c को Print किया जायेगा
यदि यदि Condition False है अर्थात c बड़ा है तो c को Print किया जायेगा
Outer if block अगर False है तो निम्न कार्य होगा
1) यदि a, b से बड़ा नही है तो Program का Control else block में आ जायेगा
अब b को c से Check किया जायेगा अगर b बड़ा है, तो b Print होगा अन्यथा C Print होगा
Output Explanation:
Enter three numbers: 15 10 25
25 is the largest number.
ऊपर a की Value 15, b की 10 और c की 25 है चूकि 25 जो की c की Value है बड़ी है इसलिए c को Largest number के रूप में Print किया जाएगा
else-if ladder
जब कई निर्णयों के आधार पर कार्य करना होता है तब else-if-ladder का प्रयोग किया जाता है यह भी nested if Statement का एक रूप हैलेकिन else-if ladder मे nesting केवल else भाग मे ही की जाती है इसमे कई else if Statement शामिल होते हैं
जब Condition True रहता है तब if के अंतर्गत आने वाले Statement Execute होते है और Condition False है तो else if के अंतर्गत आने वाले Statement Execute होते है
यह प्रक्रिया तब तक चलती है जब तक Program में else-if उपस्थित रहते है
Syntax:
if (condition1)
{
Statement 1
}
else if (condition2)
{
Statement 2
}
else if (condition3)
{
Statement 3
}
...
else {
Statement 4
}
Program for else--if ladder
#include <stdio.h>
int main()
{ float percentage; printf("Enter your percentage: "); scanf("%f", &percentage);
if ( percentage >= 60)
{
printf("First Division: \n"); }
else if (percentage >= 50)
{ printf("Second Division: \n"); }
else if (percentage >= 33)
{
printf("Third Division: \n"); }
else
{ printf("Fail: \n"); } return 0; }
Enter your percentage: 55
Second Division:
Explanation:
1) पहले User percentage Enter करेंगे
1) पहले User percentage Enter करेंगे
2) अब else-if ladder का Use करके उन विभिन्न Conditions के आधार पर Enter किए गए percentage को Check किया जायेगा और उससे संबंधित Division को Print किया जायेगा
switch Statement
switch statement एक Exepression या Value के आधार पर कई Conditions को Handle करने का एक वैकल्पिक तरीका प्रदान करता हैआप else..if ladder का प्रयोग करके भी यह कार्य कर सकते है लेकिन switch statement का Syntax पढ़ने और लिखने मे ज्यादा सरल है
Syntax,:
switch (expression) {
case constant1:
statement 1
break;
case constant2:
statement 2
break;
.
.
.
default:
default statement 3
}
Program for switch Statement
#include <stdio.h>
int main()
{
int dayName;
printf("Enter a number to print day's name:");
scanf("%d", &dayName);
switch (dayName) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break; case 3: printf("Tuesday"); break; case 4: printf("Wednesday"); break; case 5: printf("Thursday"); break; case 6: printf("Friday"); break; case 7: printf("Saturday"); break; default: printf("Error! Day number is not valid."); } return 0; }
Explaination:
switch statement:
ऊपर Program में switch statement एक Variable(dayName) के Value को मूल्यांकित करता है और उस Value के आधार पर उचित Case को Execute करता है
ऊपर Program में switch statement एक Variable(dayName) के Value को मूल्यांकित करता है और उस Value के आधार पर उचित Case को Execute करता है
जैसे यदि अपने dayName, Variable की Value, 3 दी है तो वह case 3 के Statement (Tuesday) को Print करेगा
break statement:
break statement का Use संबंधित case के execution के बाद switch block से exit होने के लिए किया जाता
default statement:
default case उस स्थिति को Handle करता है जब variable की Value के लिए कोई भी cases match नही होती जैसे आपने Variable की Value 8 दी है उस स्थिति में default statement execute होगी
Difference between if and else if in Hindi
1) `if`:
इसका उपयोग एक condition (शर्त) की जांच के लिए होता है।
`else if`:
इसका उपयोग कई conditions (शर्तो) की जांच के लिए होता है।
2) `if`:
यह statement तभी execute (क्रियान्वित) होता है जब condition सही है।
`else if` :
यह statement तब executes होता है जब पिछली `if` condition गलत है।
3)`if`:
इस statement का उपयोग अकेले किया जा सकता है।
`else if`:
इस statement का उपयोग if statement के बाद होता है।
4) 'if':
इसमें प्रत्येक condition की अलग से जांच की जाती है।
'else if':
सही condition मिलने पर जांच रुक जाती है।
5) 'if':
कई conditions के साथ इसे समझना कठिन हो जाता है।
'else if':
यह conditions को एक स्पष्ट क्रम मैं दिखाता है।
6) ' if':
Syntax
if (condition)
{
statement1;
}
'else if':
Syntax
if (condition1)
{
Statement 1
}
else if (condition2)
{
Statement 2
}
else if (condition3)
{
Statement 3
}
...
else {
Statement 4
}
Related Post
4 टिप्पणियाँ
While loop or do while loop Mai antar or syntax
जवाब देंहटाएंwhile और do while में अंतर और syntax 👇https://www.computerehub.com/2024/03/loop-in-c.html
जवाब देंहटाएंIf statement or else if statement me antar
जवाब देंहटाएंif और else if में अंतर ऊपर 👆 अपडेट किया गया है
जवाब देंहटाएं