Coding/Learn/cpp/logical operators
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
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

Real-world conditions rarely depend on a single variable. Logical operators allow you to combine multiple checks: && (AND - both must be true), || (OR - at least one must be true), and ! (NOT - inverts truth value).

The Mental Model

"To board a flight, you need a valid ticket AND (&&) a passport. To pay for groceries, you can use cash OR (||) a credit card. If you are NOT (!) a member, you pay standard price."

Deep Dive: How It Works

&& (Logical AND): true only if BOTH operands evaluate to true. (age >= 18 && hasLicense).

|| (Logical OR): true if EITHER operand (or both) evaluates to true. (isWeekend || isHoliday).

! (Logical NOT): Flips true to false, and false to true. (!isGameOver).

Short-Circuit Evaluation: If the first condition in an && expression is false, C++ skips evaluating the second condition entirely because the whole expression cannot be true!

Syntax Blueprint

if (age >= 21 && hasID) { /* Enter */ }
if (isRainy || isSnowing) { /* Umbrella */ }
if (!isLocked) { /* Open door */ }

Combine multiple boolean checks into cohesive decision logic.

PointCore Rules to Remember

Point star
Short-Circuit Safety: Prevents crashes: if (ptr != nullptr && ptr->val > 0) is safe.
Point star
Operator Precedence: ! calculates first, then &&, then ||.
Point star
Parentheses for Clarity: Always group complex conditions with ( ) for readability.

Common Beginner Traps & How to Fix Them

Mistake:Writing if (10 < x < 20)

Why it happens: Math notation habit.

How to fix: In C++, write if (x > 10 && x < 20). (10 < x < 20 evaluates (10 < x) to 0 or 1, which is always < 20!).

Pro Tip: Use descriptive boolean variable names like hasAccess or canProceed to make if statements read like plain English!

Live Interactive Example

Hit Run Code to see it live
Access Control with Logical Operators
C++ 20
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: Since userAge >= 21 is true and isGuestList is true, access is granted.

Your Turn: Micro Challenge

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

Micro Exercise

Check range eligibility

  • PointDeclare int temperature = 72;
  • PointCheck if temperature >= 65 && temperature <= 80.
  • PointIf true, print "Ideal Weather".
1
2
3
4
5
6
7
8
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