C++ का if Statement
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
if Conditions में Logical OR
इसकी बजाय || इस्तेमाल करना block को चलने देता है अगर कम से कम एक condition true हो, जैसे if (isWeekend || isHoliday), useful जब भी कोई एक qualifying condition आगे बढ़ने के लिए काफी हो।
एक condition में && और || मिलाना अक्सर intended grouping को unambiguous बनाने के लिए parentheses चाहता है।
उदाहरण: Logical OR in if Conditions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Nested if Statements
एक if को दूसरे के अंदर रखना आपको दूसरी condition तभी check करने देता है जब पहली पहले ही pass हो चुकी हो, जैसे admin rights अलग से check करने से पहले यह verify करना कि एक user logged in है।
यह real decision-making जैसा है, जहाँ कुछ questions सिर्फ पहले वाले का जवाब मिलने के बाद ही पूछना makes sense करता है।
उदाहरण: Nested if Statements
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- condition में
==की बजाय=इस्तेमाल करना, जैसेif (x = 5)। - condition के ठीक बाद एक semicolon add करना,
if (x > 5);, ताकि block हमेशा चले। - एक multi-line body पर braces छोड़ देना, ताकि सिर्फ पहला statement
ifसे belong करे।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: