C++ में Logical Conditions
In this page:
if (condition1 && condition2) {
// both are true
}
if (condition1 || condition2) {
// at least one is true
}
if (!condition) {
// condition is false
}
Conditions में Logical Operators
Logical operators कई individual conditions को एक single expression में combine करते हैं जिसे एक if statement test कर सकता है, एक decision को एक साथ एक से ज़्यादा factor पर depend करने देते हुए।
उदाहरण: Logical Operators in 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 hasID and set it to true
bool hasID = true;
// Check whether age >= 18 && hasID
if (age >= 18 && hasID) {
// Print to the console with cout
std::cout << "Allowed" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
&& से Conditions Combine करना
&& (logical AND) operator दो conditions को combine करता है ताकि overall expression सिर्फ तब true हो जब दोनों individual conditions true हों, आमतौर पर यह check करने के लिए इस्तेमाल होता है कि एक value एक specific range में आती है।
उदाहरण: Combining Conditions with &&
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare temperature and set it to 25
int temperature = 25;
// Declare isSunny and set it to true
bool isSunny = true;
// Check whether temperature > 20 && isSunny
if (temperature > 20 && isSunny) {
// Print to the console with cout
std::cout << "Good weather" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
|| से Conditions Combine करना
|| (logical OR) operator दो conditions को combine करता है ताकि overall expression true हो जब इनमें से कम से कम एक true हो, यह check करने के लिए useful कि एक value कई acceptable options में से किसी से match करती है।
उदाहरण: Combining Conditions with ||
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare hasCash and set it to false
bool hasCash = false;
// Declare hasCard and set it to true
bool hasCard = true;
// Check whether hasCash || hasCard
if (hasCash || hasCard) {
// Print to the console with cout
std::cout << "Can pay" << 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 Negate करना
! (logical NOT) operator एक single condition को invert करता है, एक true value को false और एक false value को true बनाते हुए, जिसे अक्सर एक condition के नाम के आगे naturally not की तरह पढ़ा जाता है।
उदाहरण: Negating a Condition with !
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isRaining and set it to false
bool isRaining = false;
// Check whether !isRaining
if (!isRaining) {
// Print to the console with cout
std::cout << "Go outside" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Short-Circuit Evaluation
C++ && और || को short-circuit evaluation से evaluate करता है: && के लिए, अगर पहली condition false है, दूसरी evaluate ही नहीं होती, जो आमतौर पर division by zero जैसे operations से safely guard करने के लिए इस्तेमाल होता है।
उदाहरण: Short-Circuit Evaluation
// Include std::cout and std::cin
#include <iostream>
bool sideEffect() {
// Print to the console with cout
std::cout << "Second condition evaluated" << std::endl;
// Send true back to the caller
return true;
}
// Program execution starts in main()
int main() {
// Declare first and set it to false
bool first = false;
// Check whether first && sideEffect()
if (first && sideEffect()) {
// Print to the console with cout
std::cout << "Both true" << std::endl;
}
std::cout << "Done" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
x > 1 && < 10लिखना और दूसराxछोड़ देना, जो compile नहीं होता।&&को||से confuse करना, ताकि जिस condition को दोनों checks चाहिए वह किसी एक से भी pass हो जाए।&&और||मिलाते समय parentheses भूल जाना, क्योंकि&&||से tighter bind करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: