In C++, data type bool is used to represent Boolean data. Each bool constant or variable contains one of two values: true or false. true and false are two C++ constants. true has the value 1. false has the value 0.
If a testing expression is not of bool type, it is coerced to bool type automaticaly when it is evaluated. A nonzero value is coerced to true and a zero value is coerced to false.
Relational Operators C++ Symbol DescriptionFor example, The Boolean expression
== Equal to.
!= Not equal to.
> Greater than.
< Less than.
>= Greater than or equal to.
<= Less than or equal to.
number1 < number2is evaluated to true if the value stored in number1 is less than the value stored in number2, and evaluated to false otherwise.
When a relational operator is applied between variables of type char, the assertion is in terms of where the two operands fall in the collating sequence of a particular character set. For example,
character1 < character2is evaluated to true if the value stored in character1 comes before the character stored in character2 in the collating sequence of the machine on which the expression is being evaluated. You can think of collating sequence as being in alphabetic order to help you understand it. ASCII character set is widely used.
A simple Boolean expression is either a Boolean variable or a Boolean constant or an expression involving the relational operators that evaluates to either true or false. These simple Boolean expressions can be combined using logical operations defined on Boolean values. There are three Boolean operators: AND, OR and NOT. Here is a table showing how they are used in C++.
Logical Operator C++ Symbol Meaning
&& AND is a binary Boolean operator. If both operands are true, the result is true. Otherwise, the result is false.
|| OR is a binary Boolean operator. If at least one of the operands is true, the result is true. Otherwise, if both operands are false, the result is false.
! NOT is a unary Boolean operator. NOT changes value of its operand. If the operand is true, the result is false. If the operand is false, the result is true.
The following table summarizes the precedence of all the C++ operators we have seen so far.
Highest Precedence | ( ) | | ++x --x | | ! Unary + Unary - | | * / % | | + - | | << >> | | < <= > >= | | == != | | && | | || | | = | | x++ x-- | V Lowest PrecedenceOperators in the same line in the table have the same precedence. If an expression contains several operators with the same precedence, most of the operators group from left to right. Some operators have different precedence based on where variables are in relation to them, for these an x represents the variable.