C का if-else Statement
In this page:
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
if-else क्या है?
if-else एक condition के exactly दो possible outcomes cover करता है: condition true होने पर if block चलता है, और जब भी यह false हो तो इसकी जगह else block चलता है, इसलिए दोनों में से exactly एक path हमेशा execute होता है।
उदाहरण: What is if-else?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 15
int age = 15;
// Check whether age >= 18
if (age >= 18) {
// Print formatted text to the screen
printf("Adult");
} else {
// Print formatted text to the screen
printf("Minor");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass या Fail Check
if-else से एक score को एक passing threshold से compare करना -- if (score >= 50) ... else ... -- pass और fail outcomes को दो distinct, mutually exclusive code paths में cleanly अलग करता है।
उदाहरण: Pass or Fail Check
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 40
int score = 40;
// Check whether score >= 50
if (score >= 50) {
// Print formatted text to the screen
printf("Pass");
} else {
// Print formatted text to the screen
printf("Fail");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Even या Odd Divider
एक if-else के अंदर n % 2 == 0 test करना एक single conditional को दोनों even और odd cases को एक block में handle करने देता है, दोनों outcomes cover करने के लिए दो अलग if statements की ज़रूरत की बजाय।
उदाहरण: Even or Odd Divider
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 7
int n = 7;
// Check whether n % 2 == 0
if (n % 2 == 0) {
// Print formatted text to the screen
printf("Even");
} else {
// Print formatted text to the screen
printf("Odd");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Voting Eligibility
if-else eligibility checks के लिए एक natural fit है, जैसे किसी चीज़ के लिए qualify करने वाले users को न करने वालों से अलग करने के लिए if (age >= 18) ... else ... verify करना, एक single readable comparison में।
उदाहरण: Voting Eligibility
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 16
int age = 16;
// Check whether age >= 18
if (age >= 18) {
// Print formatted text to the screen
printf("Can vote");
} else {
// Print formatted text to the screen
printf("Cannot vote");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested if-else
एक outer if-else की किसी branch के अंदर एक if-else nest करना आपको sub-decisions handle करने देता है -- उदाहरण के लिए, एक बार जब आप जान लें कि एक number positive है, एक nested if-else इसे आगे even या odd के तौर पर classify कर सकता है।
उदाहरण: Nested if-else
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 8
int n = 8;
// Check whether n > 0
if (n > 0) {
// Check whether n % 2 == 0
if (n % 2 == 0) {
// Print formatted text to the screen
printf("Positive even");
} else {
// Print formatted text to the screen
printf("Positive odd");
}
} else {
printf("Not positive");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
if (...)के बाद एक semicolon लगाना, जो इसके बाद आने वालेelseको तोड़ देता है।- braces छोड़ देना ताकि गलत statement
elseसे attach हो जाए। else (x > 5)लिखना, जब plainelseकोई condition नहीं लेता।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: