//suppose that San is tired
bool sanIsTired = true;
A D V E R T I S E M E N T
//but San doesn't have to wake up early
bool sanMustWakeUpEarly = false;
//will San go to sleep now?
bool bedTime = sanIsTired && sanMustWakeUpEarly;
What will this chunk of the code do? It initializes the variables, sanIsTired to true
and sanMustWakeUpEarly to false, The third line of code (comments not included), we
determine that San is going to sleep if and only if "and" operation is true -- that is,
if both the inputs to "and" operation are true. In this case, first input is true and
second input is false. Since the "and" requires both inputs to be true in order for
output to be true, but one of an inputs is false, output will become false. So,
variable bedTime will store a false value.
"not" operator is used by placing symbol "!", before the boolean value.
//suppose that Julie stayed up late
bool julieStayedUpLate = true;
//will Julie be peppy tomorrow?
bool julieIsPeppy = !julieStayedUpLate;
This code illustrates the "not" operation. At the end of the code, variable
julieIsPeppy will take an opposite value of julieStayedUpLate. If
julieStayedUpLate were false, then the variable julianIsPeppy would be true. In
this case, opposite is true, so the julieIsPeppy gets the value false.
The Arithmetic Operators
In addition to boolean operators, C++ has many arithmetic operators. They are listed
int sum = 4 + 7
float difference = 18.55 - 14.21
float product = 5 * 3.5
int quotient = 14 / 3
int remainder = 10 % 6
These operators probably look familiar with an exception of mod (%) operator. Mod is simply a
remainder produced by dividing the two integers. In example shown in the table above,
if we treat a 10 / 6 as the integer divison, quotient is 1 (rather than being 1.666) and
remainder is 4. Hence, a variable remainder will get a value 4.
An equality operator is used to tests the condition like "is less than",
"is greater than", and "is equal to". it is useful to to compare two numbers
using the expressions like "x<y".
is less than
bool result = (4 < 7)
is greater than
bool result = (3.1 > 3.1)
is equal to
bool result = (11 == 8)
is less than or equal to
bool result = (41.1 <= 42)
is greater than or equal to
bool result = (41.1 >= 42)
is not equal to
bool result = (12 != 12)
Once using the equality operator is known,it is easy to use all the others. They
all will work in the same way: they take an expressions on the either side of them,
and will either returns true or false.
The Assignment Operators
In case of an assignment operators return value is simply the value which is stored in
a variable on the left-hand-side.
x = 5;
y = 9;
cout << "The value of 'x' is "<< x << endl;
cout << "The value of 'y' is " << y << endl;
sum = x + y;
cout << "The sum of 'x' and 'y' is " << sum << endl;
This block of code shows why one might want to throw the return value of the
operator. Look at third line, x = 5. We are using an assignment operator here to
place a value 5 in a variable x. Since the expression x = 5 returns the value, and
we are not been using it, then you can say we are ignoring a return value.
What is an Operator Precedence?
An Operator precedence refers to a order in which the operators are resolved.
An operator with a high precedence will be used before the operator with the lower
precedence. Here is an example:
operators have same precedence as the other operators in their group, and have the
higher precedence than the operators in the lower groups.