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

C++ का if Statement

एक if statement सिर्फ तब code चलाता है जब एक condition true हो, ठंड होने पर ही coat लेने जैसा। अगर यह false हो, C++ इसे skip कर देता है।
Syntax
cpp
if (condition) {
  // runs when condition is true
}

Basic if Statement

एक if statement एक condition evaluate करता है और सिर्फ तभी अपना associated block execute करता है जब वह condition true हो; जब यह false हो, block पूरी तरह skip हो जाता है और execution इसके ठीक बाद जारी रहता है।

यह एक program के behavior को हमेशा same चीज़ करने की बजाय data पर depend कराने का सबसे fundamental building block है।

उदाहरण: Basic if Statement

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

// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Check whether age >= 18
	if (age >= 18) {
		// Print to the console with cout
		std::cout << "Adult" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if Condition के अंदर Comparison

एक if condition के अंदर == जैसे comparison operators आपको यह branch करने देते हैं कि दो values match करती हैं या नहीं, जैसे if (userChoice == y)।

यहाँ गलती से एक single = type करने से careful रहें, क्योंकि वह compare करने की बजाय assign करेगा और actual values की परवाह किए बिना लगभग हमेशा true evaluate होगा।

उदाहरण: Comparison inside if Condition

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

// Program execution starts in main()
int main() {
	// Declare userChoice and set it to 'y'
	char userChoice = 'y';
	// Check whether userChoice == 'y'
	if (userChoice == 'y') {
		// Print to the console with cout
		std::cout << "Confirmed" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if Conditions में Logical AND

एक if के अंदर दो conditions को && से combine करना block चलने से पहले दोनों का true होना ज़रूरी बनाता है, जैसे if (age >= 18 && hasLicense), जो access सिर्फ तब देता है जब हर requirement simultaneously satisfy हो।

यह code में 'ये सारी चीज़ें true होनी चाहिए' express करने का तरीका है।

उदाहरण: Logical AND in if Conditions

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

// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Declare hasLicense and set it to true
	bool hasLicense = true;
	// Check whether age >= 18 && hasLicense
	if (age >= 18 && hasLicense) {
		// Print to the console with cout
		std::cout << "Can drive" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

if Conditions में Logical OR

इसकी बजाय || इस्तेमाल करना block को चलने देता है अगर कम से कम एक condition true हो, जैसे if (isWeekend || isHoliday), useful जब भी कोई एक qualifying condition आगे बढ़ने के लिए काफी हो।

एक condition में && और || मिलाना अक्सर intended grouping को unambiguous बनाने के लिए parentheses चाहता है।

उदाहरण: Logical OR in if Conditions

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

// Program execution starts in main()
int main() {
	// Declare isWeekend and set it to false
	bool isWeekend = false;
	// Declare isHoliday and set it to true
	bool isHoliday = true;
	// Check whether isWeekend || isHoliday
	if (isWeekend || isHoliday) {
		// Print to the console with cout
		std::cout << "No work today" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested if Statements

एक if को दूसरे के अंदर रखना आपको दूसरी condition तभी check करने देता है जब पहली पहले ही pass हो चुकी हो, जैसे admin rights अलग से check करने से पहले यह verify करना कि एक user logged in है।

यह real decision-making जैसा है, जहाँ कुछ questions सिर्फ पहले वाले का जवाब मिलने के बाद ही पूछना makes sense करता है।

उदाहरण: Nested if Statements

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

// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to true
	bool isLoggedIn = true;
	// Declare isAdmin and set it to true
	bool isAdmin = true;
	// Check whether isLoggedIn
	if (isLoggedIn) {
		// Check whether isAdmin
		if (isAdmin) {
			// Print to the console with cout
			std::cout << "Welcome, admin" << 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. condition में == की बजाय = इस्तेमाल करना, जैसे if (x = 5)।
  2. condition के ठीक बाद एक semicolon add करना, if (x > 5);, ताकि block हमेशा चले।
  3. एक multi-line body पर braces छोड़ देना, ताकि सिर्फ पहला statement if से belong करे।

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.