Coding/Learn/csharp/logical operators
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnLogical Operators (&&, ||, !)
8 / 20 0%
Lesson 08 of 20
5 mins read

Logical Operators (&&, ||, !)

PreviousNext Lesson
In Plain English

Logical operators combine multiple boolean expressions into cohesive conditions: && (AND - both must be true), || (OR - at least one must be true), and ! (NOT - inverts truth).

The Mental Model

"To purchase a video game on Steam, your account must have funds OR (||) a linked credit card, AND (&&) you must NOT (!) be banned."

Deep Dive: How It Works

&& (AND): true only if both sides evaluate to true. (isAdult && hasTicket).

|| (OR): true if either side (or both) evaluates to true. (hasCoupon || isSaleDay).

! (NOT): Inverts boolean state. (!isGameOver).

Short-Circuiting: If the left side of && is false, C# skips evaluating the right side completely because the condition is guaranteed to fail.

Syntax Blueprint

if (hasKey && !isLocked) { /* Open door */ }
if (isWeekend || isHoliday) { /* Sleep in */ }

Combine multiple conditions using &&, ||, and !.

PointCore Rules to Remember

Point star
Short-Circuit Safety: Prevents null reference exceptions: if (user != null && user.IsActive).
Point star
Operator Precedence: ! evaluates first, followed by &&, and then ||.
Point star
Parentheses for Clarity: Use parentheses ( ) to group complex logic explicitly.

Common Beginner Traps & How to Fix Them

Mistake:Using single & or | instead of && or ||

Why it happens: Confusion with bitwise operators.

How to fix: Always use double && and || for standard boolean logical checks.

Pro Tip: In C# 9+, you can use natural language keywords: if (score is >= 70 and <= 100)!

Live Interactive Example

Hit Run Code to see it live
Multi-Condition Security Check
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: The user has clearance level 4 and a keycard, satisfying the first condition.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Validate access range

  • PointDeclare int battery = 45; and bool isCharging = true;
  • PointCheck if battery < 20 && !isCharging; if true print "Low Battery", else print "Power OK".
1
2
3
4
5
6
7
8
9
10
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates