Coming Soon
This lesson is currently being developed
Logical operators
Understand boolean logic with AND, OR, and NOT operators.
What to Expect
Comprehensive explanations with practical examples
Interactive coding exercises to practice concepts
Knowledge quiz to test your understanding
Step-by-step guidance for beginners
Development Status
Content is being carefully crafted to provide the best learning experience
Preview
Early Preview Content
This content is still being developed and may change before publication.
6.8 — Logical operators
In this lesson, you'll learn about logical operators that combine boolean values, understand short-circuit evaluation, discover how to build complex logical conditions, and master the art of creating readable and efficient boolean logic in your programs.
What are logical operators?
Logical operators work with boolean values (true
and false
) to perform logical operations. They're essential for creating complex conditions in if statements, loops, and other control structures.
C++ provides three logical operators:
&&
(logical AND)||
(logical OR)!
(logical NOT)
These operators follow the rules of Boolean logic and return true
or false
based on their inputs. They're crucial for combining multiple conditions and creating sophisticated decision-making logic in your programs.
Logical AND operator (&&)
The logical AND operator (&&
) returns true
only when both operands are true
. If either operand is false
, the result is false
.
Truth table for AND (&&)
Left | Right | Result |
---|---|---|
false |
false |
false |
false |
true |
false |
true |
false |
false |
true |
true |
true |
Basic AND operations
#include <iostream>
int main()
{
// Basic AND truth table demonstration
std::cout << "Logical AND (&&) truth table:" << std::endl;
std::cout << "false && false = " << (false && false) << std::endl; // 0
std::cout << "false && true = " << (false && true) << std::endl; // 0
std::cout << "true && false = " << (true && false) << std::endl; // 0
std::cout << "true && true = " << (true && true) << std::endl; // 1
// Practical example: checking multiple conditions
int age = 25;
bool hasLicense = true;
bool hasInsurance = true;
bool canDrive = (age >= 18) && hasLicense && hasInsurance;
std::cout << "\nDriving eligibility check:" << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Has license: " << hasLicense << std::endl;
std::cout << "Has insurance: " << hasInsurance << std::endl;
std::cout << "Can drive: " << canDrive << std::endl;
// Range checking with AND
int score = 85;
bool isPassingGrade = (score >= 60) && (score <= 100);
std::cout << "\nGrade validation:" << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "Is passing grade: " << isPassingGrade << std::endl;
return 0;
}
Output:
Logical AND (&&) truth table:
false && false = 0
false && true = 0
true && false = 0
true && true = 1
Driving eligibility check:
Age: 25
Has license: 1
Has insurance: 1
Can drive: 1
Grade validation:
Score: 85
Is passing grade: 1
AND with comparisons
#include <iostream>
int main()
{
// Multiple comparison conditions
int temperature = 22;
int humidity = 45;
// Check if conditions are comfortable
bool isComfortable = (temperature >= 20 && temperature <= 25) &&
(humidity >= 40 && humidity <= 60);
std::cout << "Weather conditions:" << std::endl;
std::cout << "Temperature: " << temperature << "°C" << std::endl;
std::cout << "Humidity: " << humidity << "%" << std::endl;
std::cout << "Comfortable conditions: " << isComfortable << std::endl;
// User authentication example
std::string username = "admin";
std::string password = "secret123";
bool isActive = true;
bool canLogin = (username == "admin") && (password == "secret123") && isActive;
std::cout << "\nLogin validation:" << std::endl;
std::cout << "Username: " << username << std::endl;
std::cout << "Password: " << (password.empty() ? "empty" : "provided") << std::endl;
std::cout << "Account active: " << isActive << std::endl;
std::cout << "Can login: " << canLogin << std::endl;
return 0;
}
Output:
Weather conditions:
Temperature: 22°C
Humidity: 45%
Comfortable conditions: 1
Login validation:
Username: admin
Password: provided
Account active: 1
Can login: 1
Logical OR operator (||)
The logical OR operator (||
) returns true
when at least one operand is true
. It only returns false
when both operands are false
.
Truth table for OR (||)
Left | Right | Result |
---|---|---|
false |
false |
false |
false |
true |
true |
true |
false |
true |
true |
true |
true |
Basic OR operations
#include <iostream>
int main()
{
// Basic OR truth table demonstration
std::cout << "Logical OR (||) truth table:" << std::endl;
std::cout << "false || false = " << (false || false) << std::endl; // 0
std::cout << "false || true = " << (false || true) << std::endl; // 1
std::cout << "true || false = " << (true || false) << std::endl; // 1
std::cout << "true || true = " << (true || true) << std::endl; // 1
// Practical example: multiple ways to qualify
bool hasHighSchoolDiploma = false;
bool hasGED = true;
bool hasCollegeDegree = false;
bool canEnrollInCollege = hasHighSchoolDiploma || hasGED || hasCollegeDegree;
std::cout << "\nCollege enrollment eligibility:" << std::endl;
std::cout << "High school diploma: " << hasHighSchoolDiploma << std::endl;
std::cout << "GED: " << hasGED << std::endl;
std::cout << "College degree: " << hasCollegeDegree << std::endl;
std::cout << "Can enroll: " << canEnrollInCollege << std::endl;
// Weekend or holiday check
bool isWeekend = true;
bool isHoliday = false;
bool isOffDay = isWeekend || isHoliday;
std::cout << "\nDay off check:" << std::endl;
std::cout << "Is weekend: " << isWeekend << std::endl;
std::cout << "Is holiday: " << isHoliday << std::endl;
std::cout << "Is off day: " << isOffDay << std::endl;
return 0;
}
Output:
Logical OR (||) truth table:
false || false = 0
false || true = 1
true || false = 1
true || true = 1
College enrollment eligibility:
High school diploma: 0
GED: 1
College degree: 0
Can enroll: 1
Day off check:
Is weekend: 1
Is holiday: 0
Is off day: 1
OR with range and error checking
#include <iostream>
int main()
{
// Input validation - accept if in any valid range
int userInput = 15;
bool isValidInput = (userInput >= 1 && userInput <= 10) || // Range 1-10
(userInput >= 20 && userInput <= 30) || // Range 20-30
(userInput >= 50 && userInput <= 60); // Range 50-60
std::cout << "Input validation:" << std::endl;
std::cout << "User input: " << userInput << std::endl;
std::cout << "Valid ranges: 1-10, 20-30, 50-60" << std::endl;
std::cout << "Is valid: " << isValidInput << std::endl;
// Error condition checking
int temperature = -5;
int pressure = 1500;
bool hasError = (temperature < -10) || // Too cold
(temperature > 100) || // Too hot
(pressure < 500) || // Too low pressure
(pressure > 2000); // Too high pressure
std::cout << "\nSystem error checking:" << std::endl;
std::cout << "Temperature: " << temperature << "°C" << std::endl;
std::cout << "Pressure: " << pressure << " Pa" << std::endl;
std::cout << "Has error: " << hasError << std::endl;
return 0;
}
Output:
Input validation:
User input: 15
Valid ranges: 1-10, 20-30, 50-60
Is valid: 0
System error checking:
Temperature: -5°C
Pressure: 1500 Pa
Has error: 0
Logical NOT operator (!)
The logical NOT operator (!
) is a unary operator that flips the boolean value: true
becomes false
, and false
becomes true
.
Truth table for NOT (!)
Input | Result |
---|---|
false |
true |
true |
false |
Basic NOT operations
#include <iostream>
int main()
{
// Basic NOT truth table demonstration
std::cout << "Logical NOT (!) truth table:" << std::endl;
std::cout << "!false = " << (!false) << std::endl; // 1
std::cout << "!true = " << (!true) << std::endl; // 0
// Practical examples
bool isLoggedIn = false;
bool needsToLogin = !isLoggedIn;
std::cout << "\nLogin status:" << std::endl;
std::cout << "Is logged in: " << isLoggedIn << std::endl;
std::cout << "Needs to login: " << needsToLogin << std::endl;
// Negating conditions
int age = 16;
bool isAdult = age >= 18;
bool isMinor = !isAdult; // Equivalent to: age < 18
std::cout << "\nAge classification:" << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Is adult: " << isAdult << std::endl;
std::cout << "Is minor: " << isMinor << std::endl;
// Double negation
bool originalValue = true;
bool doubleNegated = !!originalValue; // Back to original
std::cout << "\nDouble negation:" << std::endl;
std::cout << "Original: " << originalValue << std::endl;
std::cout << "!original: " << (!originalValue) << std::endl;
std::cout << "!!original: " << doubleNegated << std::endl;
return 0;
}
Output:
Logical NOT (!) truth table:
!false = 1
!true = 0
Login status:
Is logged in: 0
Needs to login: 1
Age classification:
Age: 16
Is adult: 0
Is minor: 1
Double negation:
Original: 1
!original: 0
!!original: 1
NOT with complex expressions
#include <iostream>
int main()
{
int score = 45;
bool hasPassed = score >= 60;
bool hasFailed = !hasPassed;
std::cout << "Test results:" << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "Has passed: " << hasPassed << std::endl;
std::cout << "Has failed: " << hasFailed << std::endl;
// Negating compound expressions
bool isWeekend = false;
bool isHoliday = false;
bool isWorkday = !(isWeekend || isHoliday); // NOT (weekend OR holiday)
std::cout << "\nWork day calculation:" << std::endl;
std::cout << "Is weekend: " << isWeekend << std::endl;
std::cout << "Is holiday: " << isHoliday << std::endl;
std::cout << "Is workday: " << isWorkday << std::endl;
// Using NOT for input validation
std::string password = "123";
bool isValidPassword = password.length() >= 8;
bool isInvalidPassword = !isValidPassword;
std::cout << "\nPassword validation:" << std::endl;
std::cout << "Password length: " << password.length() << std::endl;
std::cout << "Is valid password: " << isValidPassword << std::endl;
std::cout << "Is invalid password: " << isInvalidPassword << std::endl;
return 0;
}
Output:
Test results:
Score: 45
Has passed: 0
Has failed: 1
Work day calculation:
Is weekend: 0
Is holiday: 0
Is workday: 1
Password validation:
Password length: 3
Is valid password: 0
Is invalid password: 1
Short-circuit evaluation
One of the most important features of logical operators is short-circuit evaluation. This means that if the result can be determined from the left operand alone, the right operand is not evaluated.
- AND (
&&
): If the left operand isfalse
, the right operand is not evaluated (result isfalse
) - OR (
||
): If the left operand istrue
, the right operand is not evaluated (result istrue
)
Demonstrating short-circuit evaluation
#include <iostream>
int main()
{
std::cout << "Short-circuit evaluation demonstration:" << std::endl;
// AND short-circuit: if first condition is false, second isn't checked
bool result1 = false && (std::cout << "This won't print (AND)" << std::endl, true);
std::cout << "AND result: " << result1 << std::endl;
// This WILL print because first condition is true
bool result2 = true && (std::cout << "This will print (AND)" << std::endl, true);
std::cout << "AND result: " << result2 << std::endl;
// OR short-circuit: if first condition is true, second isn't checked
bool result3 = true || (std::cout << "This won't print (OR)" << std::endl, false);
std::cout << "OR result: " << result3 << std::endl;
// This WILL print because first condition is false
bool result4 = false || (std::cout << "This will print (OR)" << std::endl, true);
std::cout << "OR result: " << result4 << std::endl;
return 0;
}
Output:
Short-circuit evaluation demonstration:
AND result: 0
This will print (AND)
AND result: 1
OR result: 1
This will print (OR)
OR result: 1
Practical benefits of short-circuit evaluation
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers = {1, 2, 3, 4, 5};
int index = 10; // Invalid index
// ✅ Safe: check index validity before accessing array
// If index is invalid, numbers[index] is never evaluated
if (index >= 0 && index < numbers.size() && numbers[index] > 3) {
std::cout << "Element at index " << index << " is greater than 3" << std::endl;
} else {
std::cout << "Index is invalid or element is not greater than 3" << std::endl;
}
// Division by zero protection
int dividend = 10;
int divisor = 0;
// ✅ Safe: check for zero before dividing
if (divisor != 0 && dividend / divisor > 2) {
std::cout << "Quotient is greater than 2" << std::endl;
} else {
std::cout << "Cannot divide or quotient is not greater than 2" << std::endl;
}
// Pointer safety
int* ptr = nullptr;
// ✅ Safe: check for null pointer before dereferencing
if (ptr != nullptr && *ptr > 0) {
std::cout << "Pointer value is positive" << std::endl;
} else {
std::cout << "Pointer is null or value is not positive" << std::endl;
}
return 0;
}
Output:
Index is invalid or element is not greater than 3
Cannot divide or quotient is not greater than 2
Pointer is null or value is not positive
Combining logical operators
You can combine multiple logical operators to create complex conditions:
Complex logical expressions
#include <iostream>
int main()
{
// User access control system
std::string role = "admin";
int experience = 3;
bool isActive = true;
bool hasTraining = true;
// Complex access logic
bool canAccessSystem = (role == "admin" || role == "manager") &&
isActive &&
(experience >= 2 || hasTraining);
std::cout << "Access control evaluation:" << std::endl;
std::cout << "Role: " << role << std::endl;
std::cout << "Experience: " << experience << " years" << std::endl;
std::cout << "Is active: " << isActive << std::endl;
std::cout << "Has training: " << hasTraining << std::endl;
std::cout << "Can access system: " << canAccessSystem << std::endl;
// Grade calculation with multiple criteria
int attendance = 90;
int examScore = 75;
int projectScore = 85;
bool extraCredit = false;
bool passesClass = (attendance >= 80) &&
(examScore >= 70 || (projectScore >= 80 && extraCredit)) &&
((examScore + projectScore) / 2 >= 75);
std::cout << "\nGrade evaluation:" << std::endl;
std::cout << "Attendance: " << attendance << "%" << std::endl;
std::cout << "Exam score: " << examScore << std::endl;
std::cout << "Project score: " << projectScore << std::endl;
std::cout << "Extra credit: " << extraCredit << std::endl;
std::cout << "Passes class: " << passesClass << std::endl;
return 0;
}
Output:
Access control evaluation:
Role: admin
Experience: 3 years
Is active: 1
Has training: 1
Can access system: 1
Grade evaluation:
Attendance: 90%
Exam score: 75
Project score: 85
Extra credit: 0
Passes class: 1
Using parentheses for clarity
#include <iostream>
int main()
{
bool a = true, b = false, c = true, d = false;
// ❌ Confusing without parentheses (relies on precedence knowledge)
bool confusing = a && b || c && !d;
// ✅ Clear with parentheses
bool clear = (a && b) || (c && !d);
std::cout << "Logical operator precedence:" << std::endl;
std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << std::endl;
std::cout << "Confusing expression: " << confusing << std::endl;
std::cout << "Clear expression: " << clear << std::endl;
// Complex condition made readable
int temperature = 25;
int humidity = 55;
bool isRaining = false;
bool hasUmbrella = true;
// Good weather conditions
bool isGoodWeather = (temperature >= 20 && temperature <= 30) &&
(humidity >= 40 && humidity <= 70) &&
(!isRaining || hasUmbrella);
std::cout << "\nWeather evaluation:" << std::endl;
std::cout << "Temperature: " << temperature << "°C" << std::endl;
std::cout << "Humidity: " << humidity << "%" << std::endl;
std::cout << "Is raining: " << isRaining << std::endl;
std::cout << "Has umbrella: " << hasUmbrella << std::endl;
std::cout << "Is good weather: " << isGoodWeather << std::endl;
return 0;
}
Output:
Logical operator precedence:
a = 1, b = 0, c = 1, d = 0
Confusing expression: 1
Clear expression: 1
Weather evaluation:
Temperature: 25°C
Humidity: 55%
Is raining: 0
Has umbrella: 1
Is good weather: 1
De Morgan's laws
De Morgan's laws are important logical identities that help you rewrite logical expressions:
!(A && B)
is equivalent to!A || !B
!(A || B)
is equivalent to!A && !B
Applying De Morgan's laws
#include <iostream>
int main()
{
bool a = true;
bool b = false;
std::cout << "De Morgan's laws demonstration:" << std::endl;
std::cout << "a = " << a << ", b = " << b << std::endl;
// First law: !(A && B) ≡ !A || !B
bool law1_left = !(a && b);
bool law1_right = !a || !b;
std::cout << "\nFirst law: !(A && B) ≡ !A || !B" << std::endl;
std::cout << "!(a && b) = " << law1_left << std::endl;
std::cout << "!a || !b = " << law1_right << std::endl;
std::cout << "Are equal: " << (law1_left == law1_right) << std::endl;
// Second law: !(A || B) ≡ !A && !B
bool law2_left = !(a || b);
bool law2_right = !a && !b;
std::cout << "\nSecond law: !(A || B) ≡ !A && !B" << std::endl;
std::cout << "!(a || b) = " << law2_left << std::endl;
std::cout << "!a && !b = " << law2_right << std::endl;
std::cout << "Are equal: " << (law2_left == law2_right) << std::endl;
// Practical application
int age = 17;
bool hasPermission = false;
// Instead of: !(age >= 18 || hasPermission)
bool cannotEnter1 = !(age >= 18 || hasPermission);
// Use De Morgan's law: !A && !B
bool cannotEnter2 = (age < 18) && !hasPermission;
std::cout << "\nPractical example:" << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Has permission: " << hasPermission << std::endl;
std::cout << "Cannot enter (method 1): " << cannotEnter1 << std::endl;
std::cout << "Cannot enter (method 2): " << cannotEnter2 << std::endl;
return 0;
}
Output:
De Morgan's laws demonstration:
a = 1, b = 0
First law: !(A && B) ≡ !A || !B
!(a && b) = 1
!a || !b = 1
Are equal: 1
Second law: !(A || B) ≡ !A && !B
!(a || b) = 0
!a && !b = 0
Are equal: 1
Practical example:
Age: 17
Has permission: 0
Cannot enter (method 1): 1
Cannot enter (method 2): 1
Logical operators with non-boolean values
Any value can be used in a boolean context. Non-zero values are true
, and zero values are false
:
Implicit boolean conversion
#include <iostream>
int main()
{
// Numeric values in boolean context
int zero = 0;
int positive = 42;
int negative = -5;
double floatZero = 0.0;
double floatNonZero = 3.14;
std::cout << "Implicit boolean conversions:" << std::endl;
std::cout << "0 && true: " << (zero && true) << std::endl; // 0
std::cout << "42 && true: " << (positive && true) << std::endl; // 1
std::cout << "-5 || false: " << (negative || false) << std::endl; // 1
std::cout << "0.0 || false: " << (floatZero || false) << std::endl; // 0
std::cout << "3.14 && true: " << (floatNonZero && true) << std::endl; // 1
// Character values
char nullChar = '\0'; // Null character (ASCII 0)
char letterA = 'A'; // ASCII 65
std::cout << "\nCharacter values:" << std::endl;
std::cout << "'\\0' && true: " << (nullChar && true) << std::endl; // 0
std::cout << "'A' && true: " << (letterA && true) << std::endl; // 1
// Pointer values
int* nullPtr = nullptr;
int* validPtr = &positive;
std::cout << "\nPointer values:" << std::endl;
std::cout << "nullptr || false: " << (nullPtr || false) << std::endl; // 0
std::cout << "validPtr && true: " << (validPtr && true) << std::endl; // 1
// Practical usage: checking for valid values
int userInput = 0;
if (userInput) {
std::cout << "User provided input" << std::endl;
} else {
std::cout << "No input provided (or input is zero)" << std::endl;
}
return 0;
}
Output:
Implicit boolean conversions:
0 && true: 0
42 && true: 1
-5 || false: 1
0.0 || false: 0
3.14 && true: 1
Character values:
'\0' && true: 0
'A' && true: 1
Pointer values:
nullptr || false: 0
validPtr && true: 1
No input provided (or input is zero)
Best practices and common patterns
Input validation patterns
#include <iostream>
#include <string>
int main()
{
// Multiple validation criteria
std::string email = "user@example.com";
std::string password = "mypassword123";
int age = 25;
// Email validation (simplified)
bool hasAtSymbol = email.find('@') != std::string::npos;
bool hasDot = email.find('.') != std::string::npos;
bool hasMinLength = email.length() >= 5;
bool isValidEmail = hasAtSymbol && hasDot && hasMinLength;
// Password validation
bool hasMinPasswordLength = password.length() >= 8;
bool hasDigit = password.find_first_of("0123456789") != std::string::npos;
bool isValidPassword = hasMinPasswordLength && hasDigit;
// Age validation
bool isValidAge = (age >= 13) && (age <= 120);
// Overall validation
bool canCreateAccount = isValidEmail && isValidPassword && isValidAge;
std::cout << "Account creation validation:" << std::endl;
std::cout << "Email valid: " << isValidEmail << std::endl;
std::cout << "Password valid: " << isValidPassword << std::endl;
std::cout << "Age valid: " << isValidAge << std::endl;
std::cout << "Can create account: " << canCreateAccount << std::endl;
return 0;
}
Output:
Account creation validation:
Email valid: 1
Password valid: 1
Age valid: 1
Can create account: 1
Error handling patterns
#include <iostream>
#include <vector>
int main()
{
std::vector<int> data = {1, 2, 3, 4, 5};
int index = 2;
int value = 0;
// Safe data access pattern
bool hasError = (index < 0) || // Index too small
(index >= data.size()) || // Index too large
(data.empty()); // Empty container
if (!hasError) {
value = data[index];
std::cout << "Successfully accessed element: " << value << std::endl;
} else {
std::cout << "Error: Cannot access data at index " << index << std::endl;
}
// File operation pattern (simulated)
bool fileExists = true;
bool hasReadPermission = true;
bool hasWritePermission = false;
bool canReadFile = fileExists && hasReadPermission;
bool canWriteFile = fileExists && hasWritePermission;
bool canModifyFile = canReadFile && canWriteFile;
std::cout << "\nFile operation permissions:" << std::endl;
std::cout << "Can read: " << canReadFile << std::endl;
std::cout << "Can write: " << canWriteFile << std::endl;
std::cout << "Can modify: " << canModifyFile << std::endl;
return 0;
}
Output:
Successfully accessed element: 3
File operation permissions:
Can read: 1
Can write: 0
Can modify: 0
Common mistakes and solutions
Mistake 1: Using = instead of ==
#include <iostream>
int main()
{
bool flag = true;
int x = 5;
// ❌ Assignment instead of comparison
// if (x = 10) { // This assigns 10 to x and checks if 10 is true (it is)
// std::cout << "This will always execute!" << std::endl;
// }
// ✅ Correct comparison
if (x == 5) {
std::cout << "x equals 5" << std::endl;
}
// ❌ Assignment in logical expression
// bool result = flag && (x = 20); // Assigns 20 to x, then checks if 20 is true
// ✅ Correct logical expression
bool result = flag && (x == 5);
std::cout << "Result: " << result << std::endl;
return 0;
}
Output:
x equals 5
Result: 1
Mistake 2: Precedence confusion
#include <iostream>
int main()
{
bool a = true, b = false, c = true;
// ❌ Confusing - what's the intention?
bool confusing = a && b || c; // Actually: (a && b) || c
// ✅ Clear intention with parentheses
bool clear1 = (a && b) || c; // OR has lower precedence
bool clear2 = a && (b || c); // Force different grouping
std::cout << "Precedence demonstration:" << std::endl;
std::cout << "a && b || c: " << confusing << std::endl;
std::cout << "(a && b) || c: " << clear1 << std::endl;
std::cout << "a && (b || c): " << clear2 << std::endl;
return 0;
}
Output:
Precedence demonstration:
a && b || c: 1
(a && b) || c: 1
a && (b || c): 1
Summary
Logical operators are essential for creating complex boolean logic:
The three logical operators:
&&
(AND): Returnstrue
only when both operands aretrue
||
(OR): Returnstrue
when at least one operand istrue
!
(NOT): Flips the boolean value
Key concepts:
- Short-circuit evaluation: Left operand can determine result without evaluating right operand
- Precedence:
!
(highest) →&&
→||
(lowest) - Type conversion: Non-boolean values convert to boolean (zero =
false
, non-zero =true
) - De Morgan's laws: Rules for transforming logical expressions
Best practices:
- Use parentheses for clarity in complex expressions
- Take advantage of short-circuit evaluation for safety checks
- Combine logical operators to create sophisticated conditions
- Be careful with operator precedence
- Use meaningful variable names for boolean values
Common applications:
- Input validation and error checking
- User authentication and authorization
- Range and boundary checking
- Complex decision-making logic
Understanding logical operators enables you to create robust conditional logic that forms the foundation of program control flow and decision making.
Quiz
-
What does
true && false
evaluate to? a)true
b)false
c) 1 d) 0 -
What does
false || true
evaluate to? a)true
b)false
c) 1 d) 0 -
Due to short-circuit evaluation, when does the right operand of
&&
NOT get evaluated? a) When left operand istrue
b) When left operand isfalse
c) Always d) Never -
What does
!false
evaluate to? a)false
b)true
c) 0 d) Error -
Which operator has the highest precedence? a)
&&
b)||
c)!
d)==
Practice exercises
Try these exercises to master logical operators:
-
Boolean Expression Evaluator: Write a program that demonstrates all combinations of logical operators with different boolean values.
-
User Access System: Create a complex access control system using multiple logical conditions (role, experience, active status, etc.).
-
Input Validator: Build a comprehensive input validation system that checks multiple criteria using logical operators.
-
Logic Gate Simulator: Implement digital logic gates (AND, OR, NOT) and create complex circuits using logical operators.
Explore More Courses
Discover other available courses while this lesson is being prepared.
Browse CoursesLesson Discussion
Share your thoughts and questions