🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #47

Logical Operators


Logical Operators in Conditions

You can combine or reverse conditions using logical operators. These work together with if, else, and else if to build more complex decisions.

  • && (AND) - all conditions must be true
  • || (OR) - at least one condition must be true
  • ! (NOT) - reverses a condition (truefalse, falsetrue)

AND ( && )

Use AND (&&) when both conditions must be true:

Example

int a = 200;
int b = 33;
int c = 500;

if (a > b && c > a) {
  cout << "Both conditions are true";
}

OR ( || )

Use OR (||) when at least one of the conditions can be true:

Example

int a = 200;
int b = 33;
int c = 500;

if (a > b || a > c) {
  cout << "At least one condition is true";
}

NOT ( ! )

The NOT operator (!) reverses a condition:

  • If a condition is true, ! makes it false.
  • If a condition is false, ! makes it true.

This is useful when you want to check that something is not the case:

Example

int a = 33;
int b = 200;

if (!(a > b)) {
  cout << "a is NOT greater than b";
}

Real-Life Example

In real programs, logical operators are often used for access control. For example, to get access to a system, there are specific requirements:

You must be logged in, and then you either need to be an admin, or have a high security clearance (level 1 or 2):

Example

bool isLoggedIn = true;
bool isAdmin = false;
int securityLevel = 3; // 1 = highest

if (isLoggedIn && (isAdmin || securityLevel <= 2)) {
  cout << "Access granted.";
} else {
  cout << "Access denied.";
}

// Try changing securityLevel and isAdmin to test different outcomes:
// securityLevel 1 = Access granted
// securityLevel 2 = Access granted
// securityLevel 3 = Access denied
// securityLevel 4 = Access denied
// If isAdmin = true, access is granted.

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Logical Operators – FAQs

Quick answers about learning Logical Operators in C++.

This free note from CodingNow 2.0 explains Logical Operators in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including Logical Operators, is 100% free with no signup required.
With focused practice, most students grasp Logical Operators in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now