Course Content
Introduction to C++ | Mobile-Friendly
Introduction to C++ | Mobile-Friendly
More Logical Operators
In the previous task you checked if the variable is greater than the number, but what should we do if we want to check if two variables are bigger than certain numbers simultaneously? For example, we want to drive today if the tank isn’t empty and there’s no traffic jump.
When you need to combine conditions you can use the following logical operators:
So the code to the example above will look like this:
We use here the operator &&
(AND) since we want that all of our conditions hold.
Let’s take a look at another example. The employee gets the salary twice a week: on the 15th and 30th. The following code prints a message by day of the month if the worker receives the wage on that day.
We used in this code the operator ||
(OR) since we need to check if at least one of these conditions is true.
To clarify which cases or
and and
lead to False
or True
you can use the following table. For instance, in the first row you can see the X = True, Y = True -> X or Y = True, X and Y = True.
X and Y can be any logical statement (True or False): X and Y can be any logical statement (True or False) :
X | Y | or | and |
True | True | True | True |
True | False | True | False |
False | True | True | False |
False | False | False | False |
Thanks for your feedback!