Introduction
Programming में कई बार हमें किसी condition के true होने पर ही कोई काम करना होता है।
C language में इस purpose के लिए if statement का उपयोग किया जाता है।
यह decision making का सबसे basic control statement है।
Definition
if statement एक decision-making statement है जो किसी condition के true होने पर ही block के अंदर का code execute करता है।
Syntax
if (condition)
{
// statements
}
Example
#include <stdio.h>
int main()
{
int a = 10;
if (a > 5)
{
printf("a is greater than 5");
} return 0;
}
Output:
a is greater than 5
Real World Example
मान लो:
- अगर बारिश हो → छाता ले जाओ
अगर condition true है तभी action होगा
इसी तरह if statement काम करता है।
if-else Statement
Introduction
कई बार हमें condition true होने पर एक काम और false होने पर दूसरा काम करना होता है।
ऐसी situation में if-else statement का उपयोग किया जाता है।
Definition
if-else statement एक control statement है जिसमें condition true होने पर if block execute होता है और false होने पर else block execute होता है।
Syntax
if (condition)
{
// statements if true
}
else
{
// statements if false
}
Example
#include <stdio.h>
int main()
{
int a = 3;
if (a > 5)
{
printf("Greater");
}
else
{
printf("Smaller");
} return 0;
}
Output:
Smaller
Real World Example
मान लो exam result:
- अगर marks ≥ 40 → Pass
- नहीं तो → Fail
यह if-else statement जैसा है।
Difference Between if and if-else
| if Statement | if-else Statement |
|---|---|
| केवल true condition पर execute होता है | true और false दोनों case handle करता है |
| else part नहीं होता | else part होता है |
| Simple condition के लिए use होता है | decision making के लिए ज्यादा useful |
Exam Points
- if statement केवल condition true होने पर execute होता है
- if-else दोनों conditions (true/false) handle करता है
- condition हमेशा boolean (true/false) होती है
- Output-based questions में condition analysis बहुत important है