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

C का if-else Statement

if-else एक road पर fork है: अगर condition true हो आप एक रास्ते जाते हैं, नहीं तो दूसरे। दोनों में से एक हमेशा चलता है।
Syntax
c
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?

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

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

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

Even या Odd Divider

एक if-else के अंदर n % 2 == 0 test करना एक single conditional को दोनों even और odd cases को एक block में handle करने देता है, दोनों outcomes cover करने के लिए दो अलग if statements की ज़रूरत की बजाय।

उदाहरण: Even or Odd Divider

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

Voting Eligibility

if-else eligibility checks के लिए एक natural fit है, जैसे किसी चीज़ के लिए qualify करने वाले users को न करने वालों से अलग करने के लिए if (age >= 18) ... else ... verify करना, एक single readable comparison में।

उदाहरण: Voting Eligibility

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

Nested if-else

एक outer if-else की किसी branch के अंदर एक if-else nest करना आपको sub-decisions handle करने देता है -- उदाहरण के लिए, एक बार जब आप जान लें कि एक number positive है, एक nested if-else इसे आगे even या odd के तौर पर classify कर सकता है।

उदाहरण: Nested if-else

c
// 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;
}
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. if (...) के बाद एक semicolon लगाना, जो इसके बाद आने वाले else को तोड़ देता है।
  2. braces छोड़ देना ताकि गलत statement else से attach हो जाए।
  3. else (x > 5) लिखना, जब plain else कोई condition नहीं लेता।

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.