Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
VBA Logical Operators in Excel
Next article icon

C++ Logical Operators

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).

In C++, there are three logical operators:

Table of Content

  • Logical AND Operator ( && )
  • Logical OR Operator ( || )
  • Logical NOT Operator ( ! )

Let's discuss each of the operators in detail.

1. Logical AND Operator ( && )

The C++ logical AND operator (&&) is a binary operator that returns true if both of its operands are true. Otherwise, it returns false. Here's the truth table for the AND operator:

Operand 1

Operand 2

Result

true

true

true

true

false

false

false

true

false

false

false

false

Note: In C, false is represented by 0 while the true is represented as any non-zero value, generally 1.

Syntax

expression1 && expression2

Example

C++
#include <iostream> using namespace std;  int main() {     int age = 25;     bool isStudent = true;      // Using AND operator in if condition     if (age > 18 && isStudent) {         cout << "Student";     }     else {         cout << "Not Student";     } 	return 0; } 

Output
Student

Explanation: In the above code, we have used AND operator in the if condition to check whether the age is greater than 18 and the person is a check. If both conditions are true, the message "Student" will be printed. Otherwise, the else statement is executed.

2. Logical OR Operator ( || )

The C++ logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. It returns false only when both operands are false. Here's the truth table for the OR operator:

Operand 1

Operand 2

Result

true

true

true

true

false

true

false

true

true

false

false

false

Syntax

expression1 || expression2

Example

C++
#include <iostream> using namespace std;  int main() {      int n = 7;      // using logical or for conditional statement     if (n <= 0 || n >= 10) {         cout << "n is not in range [0, 10]";     }     else {         cout << "n is in range [0, 10]";     }      return 0; } 

Output
n is in range [0, 10]

Explanation: In the above code, the condition num < 0 || num > 10 checks whether the number is either less than equal to 0 or greater than equal to 10. If either of these conditions is true, the message "The number is outside the range of 0 to 10." will be printed otherwise else statement is printed.

3. Logical NOT Operator ( ! )

The C++ logical NOT operator ( ! ) is a unary operator that is used to negate the value of a condition. It returns true if the condition is false, and false if the condition is true. Here's the truth table for the NOT operator:

Operand 1

Result

true

false

false

true

Syntax

! expression

Example

C++
#include <iostream> using namespace std;  int main() {      bool isLoggedIn = false;      // using logical not operator     if (!isLoggedIn) {         cout << "Please log in.";     }     else {         cout << "Welcome to GeeksforGeeks!";     }      return 0; } 

Output
Please log in.

Explanation: In the above code, the condition '!isLoggedIn' checks whether the user is not logged in. If the condition is true (i.e., the user is not logged in), the message "Please log in to access this feature." will be displayed otherwise else statement will be printed.

Conclusion

Logical operators in C++ enable you to combine conditions and create expressive logic. They are invaluable when working with decision-making and branching in your code. By mastering these logical operators, you can write more robust and flexible programs.


Next Article
VBA Logical Operators in Excel
author
sagar99
Improve
Article Tags :
  • C++
  • cpp-operator
Practice Tags :
  • CPP
  • cpp-operator

Similar Reads

  • SQL - Logical Operators
    SQL Logical Operators are essential tools used to test the truth of conditions in SQL queries. They return boolean values such as TRUE, FALSE, or UNKNOWN, making them invaluable for filtering, retrieving, or manipulating data. These operators allow developers to build complex queries by combining, n
    9 min read
  • Operators in C++
    C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language. Example: [GFGTABS] C++ //Driver Code Starts{ #include <iostream> using namespace std; int main() { //Driver Code E
    10 min read
  • VBA Logical Operators in Excel
    Logical operators are used for performing logical and arithmetic operations on a set of values or variables. VBA allows you to use the Logical operators AND, OR, NOT, and XOR to compare values. The operators are considered "Boolean" which means they return True or False as a result. In Excel VBA, lo
    6 min read
  • Logical Operators in math.js
    In Math.js, we can use logical operators to perform boolean operations on values. The math.and function perform a logical AND, math.or handles logical OR, and math.not provides logical NOT. These operators allow us to evaluate boolean expressions and handle logical conditions effectively in mathemat
    3 min read
  • Logical Operators in Solidity
    Logical Operators are used to combining two or more conditions. Solidity has the following types of logical operators: Logical AND: Logical AND takes two operands and gives the valid Boolean result. The logical AND operator evaluates to true when all the operands are true (non-zero) otherwise false(
    2 min read
  • C++ Logical (&&, ||, !) Operator Overloading
    Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi
    3 min read
  • Operators in LISP
    Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can
    5 min read
  • Logical Operators in Programming
    Logical Operators are essential components of programming languages that allow developers to perform logical operations on boolean values. These operators enable developers to make decisions, control program flow, and evaluate conditions based on the truthiness or falsiness of expressions. In this a
    4 min read
  • logical_or in C++
    logical_or in C++ is a binary function object class which returns the result of the logical "or" operation between its two arguments (as returned by operator ||). Syntax: template struct logical_or : binary_function { T operator() (const T& a, const T& b) const {return a||b;} }; Parameters:
    2 min read
  • C++ Comparison Operators
    Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result. There are mainly 6 Comparison Operators namely: Greater than (>) : this operator checks whether operand1 is greater than operand2. If the result t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences