← Back to C++ Course | Chapter 4: Control Flow | Lesson 4 of 13

C++ का if-else Statement

if-else एक road पर fork है: अगर condition true हो आप एक रास्ते जाते हैं, नहीं तो दूसरे। दोनों में से एक हमेशा चलता है।
Syntax
cpp
if (condition) {
  // runs when condition is true
} else {
  // runs when condition is false
}

if-else का Introduction

एक if-else statement आपको exactly दो paths देता है: condition true होने पर if block चलता है, और जब भी यह false हो तो else block चलता है, guarantee करते हुए कि दोनों में से exactly एक हमेशा execute हो।

यह अकेले एक bare if से ज़्यादा useful है जब भी आपके पास बस कुछ न करने की बजाय एक specific fallback action हो।

उदाहरण: Introduction to if-else

cpp
// Include std::cout and std::cin
#include <iostream>

// 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 to the console with cout
		std::cout << "Adult" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Minor" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if-else के अंदर Variable Assignment

एक condition के आधार पर एक variable की value set करने के लिए if-else इस्तेमाल करना, जैसे weekday या weekend label के बीच चुनना, उस decision को variable जहाँ defined है वहीं रहने देता है इसकी बजाय कि यह बाद के code में बिखर जाए।

यह pattern इतना common है कि C++ simple two-way value choices के लिए ternary operator को भी एक ज़्यादा compact alternative की तरह offer करता है।

उदाहरण: Variable Assignment inside if-else

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare day and set it to 6
	int day = 6;
	std::string label;
	// Check whether day == 6 || day == 7
	if (day == 6 || day == 7) {
		label = "weekend";
	} else {
		label = "weekday";
	}
	// Print to the console with cout
	std::cout << label << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

if-else में Boolean Flags

Boolean variables if-else के साथ naturally pair होते हैं क्योंकि condition खुद पहले ही एक question जैसा पढ़ता है — if (isLoggedIn) तुरंत clear है, 0 या 1 के against एक numeric flag check करने के उलट।

यह exactly login gates और subscription-only content जैसे common features के पीछे का pattern है।

उदाहरण: Boolean Flags in if-else

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to false
	bool isLoggedIn = false;
	// Check whether isLoggedIn
	if (isLoggedIn) {
		// Print to the console with cout
		std::cout << "Welcome back" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Please log in" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if-else के अंदर Block Scope

एक if या else block के curly braces के अंदर declared एक variable सिर्फ उस block की duration के लिए exist करता है और execution के इसे छोड़ते ही गायब हो जाता है — बाद में इसे reference करने की कोशिश एक compile error cause करती है।

यह scoping rule एक branch की temporary logic के लिए intended variables को कहीं और accidentally leak या collide होने से रोकती है।

उदाहरण: Block Scope inside if-else

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare condition and set it to true
	bool condition = true;
	// Check whether condition
	if (condition) {
		// Declare x and set it to 5
		int x = 5;
		// Print to the console with cout
		std::cout << x << std::endl;
	} else {
		int x = 10;
		// Print to the console with cout
		std::cout << x << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if-else में Compound Conditions

एक single if-else के अंदर && और || से कई conditions combine करना आपको एक statement में fairly sophisticated decision logic express करने देता है, जैसे सिर्फ logged-in admins या verified moderators को access देना।

जैसे-जैसे ये compound conditions ज़्यादा complex होते हैं, उन्हें पहले clearly named boolean variables में तोड़ना अक्सर final if-else को कहीं ज़्यादा readable रखता है।

उदाहरण: Compound Conditions in if-else

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasTicket and set it to true
	bool hasTicket = true;
	// Check whether age >= 18 && hasTicket
	if (age >= 18 && hasTicket) {
		// Print to the console with cout
		std::cout << "Entry allowed" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Entry denied" << std::endl;
	}
	// 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 condition के बाद एक semicolon लगाना, जो if को खत्म कर देता है और else को एक partner के बिना छोड़ देता है।
  2. braces छोड़ देना ताकि गलत statement else (dangling else) से attach हो जाए।
  3. एक condition की उम्मीद करते हुए else (x > 5) लिखना, जब एक plain else कोई नहीं लेता।

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.