Overview
Logical operators are used to combine or invert boolean values. They are essential for creating complex conditions in your programs.
Logical Operators Table
| Operator | Name | Description | Example | | | | |
|---|
&& | AND | Returns true if both operands are true | true && false → false | | | | |
| ` | | ` | OR | Returns true if at least one operand is true | `true | | false→true` |
! | NOT | Inverts the boolean value | !true → false | | | | |
Basic Example
Here’s a complete example demonstrating logical operators:
void main() {
bool esEstudiante = true;
bool tieneTitulo = false;
int valor = 28;
print(valor == 18 || tieneTitulo);
print(esEstudiante && tieneTitulo); // AND: false
//print(esEstudiante || tieneTitulo); // OR: true
print(!esEstudiante); // NOT: false
/*
Tabla de verdad AND:
true && true = true
true && false = false
false && true = false
false && false = false
*/
}
Output:
AND Operator (&&)
The AND operator returns true only when both conditions are true.
Truth Table for AND
| Left | Right | Result |
|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Example
bool hasLicense = true;
bool hasInsurance = true;
bool canDrive = hasLicense && hasInsurance;
print(canDrive); // Output: true
The AND operator uses “short-circuit” evaluation: if the left operand is false, the right operand is not evaluated because the result will always be false.
OR Operator (||)
The OR operator returns true when at least one condition is true.
Truth Table for OR
| Left | Right | Result |
|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Example
bool isWeekend = false;
bool isHoliday = true;
bool canRelax = isWeekend || isHoliday;
print(canRelax); // Output: true
The OR operator also uses “short-circuit” evaluation: if the left operand is true, the right operand is not evaluated because the result will always be true.
NOT Operator (!)
The NOT operator inverts a boolean value.
Truth Table for NOT
| Value | Result |
|---|
true | false |
false | true |
Example
bool isLoggedIn = true;
bool isGuest = !isLoggedIn;
print(isGuest); // Output: false
The NOT operator is useful for checking the opposite of a condition. For example, !isEmpty is more readable than isEmpty == false.
Combining Logical Operators
You can combine multiple logical operators to create complex conditions:
int age = 25;
bool hasTicket = true;
bool isStudent = false;
// Must be 18 or older AND have a ticket OR be a student
bool canEnter = (age >= 18 && hasTicket) || isStudent;
print(canEnter); // Output: true
Practical Examples
Access Control
bool isAdmin = false;
bool isOwner = true;
bool canDelete = isAdmin || isOwner;
print(canDelete); // Output: true
String username = "john";
String password = "pass1234";
bool validUsername = username.length >= 3;
bool validPassword = password.length >= 8;
bool canSubmit = validUsername && validPassword;
print(canSubmit); // Output: true
Eligibility Check
int age = 16;
bool hasParentalConsent = true;
bool canParticipate = age >= 18 || (age >= 13 && hasParentalConsent);
print(canParticipate); // Output: true
Inverted Conditions
bool isProcessing = false;
bool canStart = !isProcessing;
print(canStart); // Output: true