← Back to C Course | Chapter 4: Control Flow | Lesson 3 of 11

C की else-if Ladder

एक else-if ladder questions को order में तब तक पूछता है जब तक एक true न हो, एक test को A से F तक grade करने जैसा। C पहला match चलाता है और बाकी skip कर देता है।
Syntax
c
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?

c
// 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;
}

Grading System

Numeric score ranges के आधार पर letter grades assign करना एक else-if ladder का textbook इस्तेमाल है -- हर condition एक lower boundary test करती है, और क्योंकि वे order में check होती हैं, सिर्फ पहली matching range apply होती है।

उदाहरण: Grading System

c
// 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;
}

Number Classification

आप एक else-if ladder में तीन chained conditions से एक number को positive, negative, या zero classify कर सकते हैं, जो same variable check करने वाले तीन अलग, unrelated if statements से कहीं ज़्यादा clearly पढ़ता है।

उदाहरण: Number Classification

c
// 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;
}

Age Categories

एक else-if ladder से ages को child, adult, और senior जैसी categories में group करना categories के बीच boundaries को explicit और बाद में adjust करना आसान रखता है, क्योंकि हर range एक जगह define होती है।

उदाहरण: Age Categories

c
// 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;
}

Temperature Guides

Temperature ranges (cold, mild, hot) classify करना एक else-if ladder के लिए एक natural fit है, क्योंकि हर condition को सिर्फ एक boundary check करना है यह देखते हुए कि पहले, ज़्यादा extreme ranges पिछले checks से पहले ही rule out हो चुकी हैं।

उदाहरण: Temperature Guides

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. conditions को broad से specific order में रखना, जैसे score >= 90 से पहले score >= 50, ताकि पहला true test हमेशा जीते।
  2. else if की बजाय elseif या else-if लिखना, जो valid C नहीं है।
  3. एक ladder की बजाय अलग if statements इस्तेमाल करना, ताकि same value के लिए एक से ज़्यादा branch चल सके।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.