5.7. Logical operatorsΒΆ

There are three logical operators in C++: AND, OR and NOT, which are denoted by the symbols β€˜β€™&&’’, β€˜β€™||’’ and β€˜β€™!’’. The semantics (meaning) of these operators is similar to their meaning in English. For example β€˜β€™x > 0 && x < 10’’ is true only if x is greater than zero AND less than 10.

β€˜β€™evenFlag || n%3 == 0’’ is true if either of the conditions is true, that is, if evenFlag is true OR the number is divisible by 3.

Finally, the NOT operator has the effect of negating or inverting a bool expression, so !evenFlag is true if evenFlag is false; that is, if the number is odd.

Logical operators often provide a way to simplify nested conditional statements.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 4 activities on this page