Logical Operators — The Security System
15 XPMedium
Ctrl+Enter RunCtrl+S Save
🔐 Securing the Digital City
Your city is thriving, Architect, but with growth comes risk. The mayor has asked you to design the Central Security System — a multi-layered authentication gate that checks multiple conditions before granting access to restricted zones.
A simple if can check one thing. But real security requires checking combinations of conditions: Does the user have a badge AND a clearance level? Is it a weekend OR after hours? Is the alert mode NOT active? That's where logical operators come in.
🔗 The Three Logical Operators
&& — AND: Both conditions must be true. Like needing a keycard AND a PIN.
|| — OR: At least one condition must be true. Like entering through Door A OR Door B.
! — NOT: Flips true to false, false to true. Like checking the alarm is NOT active.
bool hasBadge = true;
bool hasClearance = true;
bool isLockdown = false;
// AND: Both must be true
if (hasBadge && hasClearance)
{
Console.WriteLine("Access: GRANTED");
}
// OR: At least one is true
if (hasBadge || hasClearance)
{
Console.WriteLine("Partial access available");
}
// NOT: Invert the condition
if (!isLockdown)
{
Console.WriteLine("Building is open");
}
⚡ Short-Circuit Evaluation — The Lazy Guard
C# is smart about logical operators. It uses short-circuit evaluation:
&& — If the left side is false, the right side is never evaluated. Why check the PIN if the badge already failed?
|| — If the left side is true, the right side is never evaluated. If Door A is open, no need to check Door B.
string user = null;
// Safe! Short-circuit prevents NullReferenceException
if (user != null && user.Length > 0)
{
Console.WriteLine("User is valid");
}
// If user is null, user.Length is NEVER evaluated — crisis averted!
🎯 Combining Multiple Conditions
Real-world security checks often combine several operators. Use parentheses to make your intent crystal clear:
int clearanceLevel = 5;
bool isAdmin = true;
bool isEmergency = false;
// Complex condition with parentheses for clarity
if ((clearanceLevel >= 5 && isAdmin) || isEmergency)
{
Console.WriteLine("FULL ACCESS granted");
}
else if (clearanceLevel >= 3)
{
Console.WriteLine("PARTIAL ACCESS granted");
}
else
{
Console.WriteLine("ACCESS DENIED");
}
❓ The Ternary Operator — Quick Decisions
For simple "this or that" choices, the ternary operator? : is a compact one-liner. Think of it as the architect's quick stamp — APPROVED or REJECTED, no paperwork needed:
int clearanceLevel = 5;
// condition ? valueIfTrue : valueIfFalse
string access = clearanceLevel >= 5 ? "FULL ACCESS" : "LIMITED ACCESS";
Console.WriteLine(access); // FULL ACCESS
// You can also use it directly in expressions
Console.WriteLine($"Admin: {(clearanceLevel >= 5 ? "Yes" : "No")}");
📊 Truth Tables — The Architect's Cheat Sheet
When in doubt, consult the truth tables:
// AND (&&) Truth Table:
// true && true → true
// true && false → false
// false && true → false (right side NOT evaluated)
// false && false → false (right side NOT evaluated)
// OR (||) Truth Table:
// true || true → true (right side NOT evaluated)
// true || false → true (right side NOT evaluated)
// false || true → true
// false || false → false
// NOT (!) Truth Table:
// !true → false
// !false → true
🪆 Nested Conditions vs. Logical Operators
You might be tempted to nest if statements deeply. Instead, flatten them with logical operators for cleaner code:
// ❌ Deep nesting — hard to read
if (hasBadge)
{
if (hasClearance)
{
if (!isLockdown)
{
Console.WriteLine("Access granted");
}
}
}
// ✅ Flat and clean — same logic!
if (hasBadge && hasClearance && !isLockdown)
{
Console.WriteLine("Access granted");
}
🏗️ Your Mission
The Central Security System awaits your code. You need to evaluate a user's credentials — clearance level, admin status — and determine their access level. The city's safety is in your hands, Architect!
📋 Instructions
**Central Security System**
You have three variables:
- `int clearanceLevel = 5;`
- `bool isAdmin = true;`
- `bool isLockdown = false;`
Write a program that:
1. **Determine access level** using combined logical operators:
- If `clearanceLevel >= 5` AND `isAdmin` is true AND `isLockdown` is false → print `"Access Level: FULL ACCESS"`
- Else if `clearanceLevel >= 3` OR `isAdmin` is true → print `"Access Level: PARTIAL ACCESS"`
- Else → print `"Access Level: DENIED"`
2. **Print admin status** using the ternary operator:
- Print `"Is Admin: True"` or `"Is Admin: False"` based on the `isAdmin` variable.
3. **Security check result** using logical NOT:
- If `isLockdown` is NOT true, print `"Security Check: PASSED"`
- Otherwise print `"Security Check: LOCKDOWN ACTIVE"`
Output must match **exactly**.
For Step 1, combine conditions: if (clearanceLevel >= 5 && isAdmin && !isLockdown). For Step 2, use the ternary: Console.WriteLine("Is Admin: " + (isAdmin ? "True" : "False")). For Step 3, use if (!isLockdown) to check the NOT condition.