Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Logical Operators | Introduction to Operators
C++ Introduction
course content

Course Content

C++ Introduction

C++ Introduction

1. Getting Started
2. Introduction to Operators
3. Variables and Data Types
4. Introduction to Program Flow
5. Introduction to Functions

book
Logical Operators

You can use logical operators AND (&&), OR (||) and NOT (!) to evaluate multiple conditions simultaneously.

  • AND (&&) returns true only if both conditions are true. For example, checking if an account balance is sufficient and the withdrawal amount is positive;

  • OR (||) returns true if at least one condition is true. For example, checking if a balance is sufficient or the user has a credit card;

  • NOT (!) negates a condition, turning true into false and vice versa. For example, checking if an account is not locked.

Imagine you need to create conditions for user authentication. Let’s explore some scenarios.

ConditionAuthorization StatusState of Password, Login, Phone Call, and Internet
Password AND login are correctAuthorized (true)Password: true
Login: true
Phone Call: N/A
Internet: N/A
Password AND login are correct, OR with a phone call identificationAuthorized (true)Password: true
Login: false
Phone Call: true
Internet: N/A
There is NO internetNot Authorized (false)Password: N/A
Login: N/A
Phone Call: N/A
Internet: false
h

logical_and

h

logical_or

h

logical_not

copy
12345678910
#include <iostream> int main() { // Using AND (&&) operator for password and login // Login is correct = `true` // Password is correct = `true` std::cout << "User authorized (password AND login correct)" << (true && true) << std::endl; }
1. Which logical operator is used to check if both conditions are true?
2. Which logical operator is used to check if at least one condition is true?
3. What does the `!` operator do?
Which logical operator is used to check if both conditions are true?

Which logical operator is used to check if both conditions are true?

Select the correct answer

Which logical operator is used to check if at least one condition is true?

Which logical operator is used to check if at least one condition is true?

Select the correct answer

What does the `!` operator do?

What does the ! operator do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt