Course Content
C++ Introduction
C++ Introduction
3. Introduction to Operators
4. Introduction to Program Flow
5. Introduction to Functions
Logical Operators
You have to use the logical operators NOT (!
), AND (&&
), and OR (||
) to evaluate multiple conditions concurrently. Let's illustrate using logical operators in real-life scenarios:
- I will take my wallet for a walk if there is a bank AND a shop on the way;
- I won't take my wallet for a walk if there is NOT bank on the way;
- I will take my wallet for a walk if there is a bank OR a shop on the way.
main
#include <iostream> int main() { bool bank = true; bool shop = true; //using AND (&&) operator bool wallet1 = (bank && shop); std::cout << "There is a bank and a shop, reason to have wallet = " << wallet1 << std::endl; //using NOT (!) operator bool wallet0 = (!bank); std::cout << "There is not bank, reason to have wallet = " << wallet0 << std::endl; //using OR (||) operator bool wallet2 = (bank || shop); std::cout << "There is a bank or a shop, reason to have wallet = " << wallet2 << std::endl; }
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 3