C की else-if Ladder
In this page:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
else-if Ladder क्या है?
एक else-if ladder कई conditions को साथ chain करता है ताकि वे order में check हों, और जैसे ही एक true evaluate होती है, इसका block चलता है और chain में बाद की हर condition बिना check हुए भी skip हो जाती है।
उदाहरण: What is an else-if Ladder?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 90
if (score >= 90) {
// Print formatted text to the screen
printf("A");
} else if (score >= 70) {
// Print formatted text to the screen
printf("B");
} else {
printf("C");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Grading System
Numeric score ranges के आधार पर letter grades assign करना एक else-if ladder का textbook इस्तेमाल है -- हर condition एक lower boundary test करती है, और क्योंकि वे order में check होती हैं, सिर्फ पहली matching range apply होती है।
उदाहरण: Grading System
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 82
int score = 82;
// Check whether score >= 90
if (score >= 90) {
// Print formatted text to the screen
printf("A");
} else if (score >= 80) {
// Print formatted text to the screen
printf("B");
} else if (score >= 70) {
printf("C");
} else {
printf("F");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Number Classification
आप एक else-if ladder में तीन chained conditions से एक number को positive, negative, या zero classify कर सकते हैं, जो same variable check करने वाले तीन अलग, unrelated if statements से कहीं ज़्यादा clearly पढ़ता है।
उदाहरण: Number Classification
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to -5
int n = -5;
// Check whether n > 0
if (n > 0) {
// Print formatted text to the screen
printf("Positive");
} else if (n < 0) {
// Print formatted text to the screen
printf("Negative");
} else {
printf("Zero");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Age Categories
एक else-if ladder से ages को child, adult, और senior जैसी categories में group करना categories के बीच boundaries को explicit और बाद में adjust करना आसान रखता है, क्योंकि हर range एक जगह define होती है।
उदाहरण: Age Categories
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 45
int age = 45;
// Check whether age < 13
if (age < 13) {
// Print formatted text to the screen
printf("Child");
} else if (age < 65) {
// Print formatted text to the screen
printf("Adult");
} else {
printf("Senior");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Temperature Guides
Temperature ranges (cold, mild, hot) classify करना एक else-if ladder के लिए एक natural fit है, क्योंकि हर condition को सिर्फ एक boundary check करना है यह देखते हुए कि पहले, ज़्यादा extreme ranges पिछले checks से पहले ही rule out हो चुकी हैं।
उदाहरण: Temperature Guides
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare temp and set it to 85
int temp = 85;
// Check whether temp < 50
if (temp < 50) {
// Print formatted text to the screen
printf("Cold");
} else if (temp < 80) {
// Print formatted text to the screen
printf("Mild");
} else {
printf("Hot");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- conditions को broad से specific order में रखना, जैसे
score >= 90से पहलेscore >= 50, ताकि पहला true test हमेशा जीते। else ifकी बजायelseifयाelse-ifलिखना, जो valid C नहीं है।- एक ladder की बजाय अलग
ifstatements इस्तेमाल करना, ताकि same value के लिए एक से ज़्यादा branch चल सके।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: