Logical Operators Part 3
Part of the Fundamentals section of Coddy's C++ journey — lesson 26 of 74.
When checking multiple conditions, the computer stops checking as soon as it knows the final answer (This is called short-circuit evaluation).
For example:
int x = 0;
int y = 5;
bool result = x != 0 && y / x > 2;Here x equals 0, therefore it will not evaluate y / x > 2. If we were to reverse the order:
bool result = y / x > 2 && x != 0;This will result in an error because y will be divided by 0 which is illegal in math.
This technique is used to optimize the evaluation of logical expressions. For example:
int a = 0;
int b = 2;
int c = 3;
int d = 5;
bool result = (a > 0 && b < 2) || (c < -5 && d < 10);In this example, b < 2 and d < 10 will not be evaluated because a > 0 and c < -5 are both false.
Challenge
BeginnerCreate a program to decide if it's a good day for solar panel energy production
Initialize these variables:
isSunnywith the value truewindSpeedwith the value 5.4temperaturewith the value 23solarPanelOutputwith the value 9isCloudywith the value false
Create one logical expression that checks ALL of these conditions:
- It's sunny
- The wind speed is less than 10
- The solar panel output is less than 15
- The temperature is above 20 OR there are NO clouds
Cheat sheet
Short-circuit evaluation means the computer stops checking conditions as soon as it knows the final answer.
With && (AND), if the first condition is false, the second won't be evaluated:
int x = 0;
int y = 5;
bool result = x != 0 && y / x > 2; // y / x > 2 is not evaluatedWith || (OR), if the first condition is true, the second won't be evaluated:
bool result = (a > 0 && b < 2) || (c < -5 && d < 10);
// If a > 0 is false, then c < -5 and d < 10 won't be evaluatedThis prevents errors (like division by zero) and optimizes performance by avoiding unnecessary evaluations.
Try it yourself
#include <iostream>
int main() {
// Initialize variables
// The complete logical expression
bool result =
// Print results
std::cout << "1. Is it sunny? " << std::boolalpha << isSunny << std::endl;
std::cout << "2. Is wind speed safe? " << (windSpeed < 10) << std::endl;
std::cout << "3. Do panels produce less? " << (solarPanelOutput < 15) << std::endl;
std::cout << "4. Is temperature good OR no clouds? " << (temperature > 20 || !isCloudy) << std::endl;
std::cout << "5. Final result: " << result << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else