C++ Prog and Appl
C++ Prog and Appl
Chapter Three
C++ Operators
Knowing the existence and the utility of variables and constants, we can begin to operate with them. For that
purpose, the C++ language integrates operators.
1. Assignation ( = )
The most important rule of assignation is the right-to-left rule: The assignation operation always takes place
from right to left, and never the other way. Let us have a look at the following code
int main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
cout << "a:";
cout << a;
cout << " b:";
cout << b;
return 0;
}
10
Dr. KAMDEM Paul
Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications
Modulo (%) is the operation that gives the remainder of a division of two integer values.
For example, if we write: R = 11 % 2;
The variable R will contain the value 1, since 1 is the remainder from dividing 11 between 2.
3. Compound assignation (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
When we want to modify the value of a variable by performing an operation on the value currently stored in that
variable we can use compound assignation operators:
expression is equivalent to
// compound assignation 5
#include <iostream>
using namespace std;
int main ()
{
int a,b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
return 0;
}
The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a
variable. They are equivalent to +=1 and to - =1, respectively. Thus:
c++; c+=1; c=c+1;
are all equivalent in its functionality: the three of them increase by one the value of c.
5. Relational and equality operators ( ==, !=, >, <, >=, <= )
In order to evaluate a comparison between two expressions we can use the relational and equality operators.
The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean
result.
Here is a list of the relational and equality operators that can be used in C++:
11
Dr. KAMDEM Paul
Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Here there are some examples:
(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Of course, instead of using only numeric constants, we can use any valid expression, including variables.
Suppose that a=2, b=3 and c=6,
Note that The operator = (one equal sign) is not the same as the operator == (two equal signs), the first one is an
assignation operator (assigns the value at its right to the variable at its left) and the other one (==) is the
equality operator that compares whether both expressions in the two sides of it are equal to each other.
The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located
at its right. Basically, it returns the opposite Boolean value of evaluating its operand. For example:
The logical operators && and || are used when evaluating two expressions to obtain a single relational result.
The operator && corresponds with Boolean logical operation AND. This operation results true if both its two
operands are true, and false otherwise. The following panel shows the result of operator && evaluating the
expression a && b:
&& OPERATOR
a b a && b
true true true
true false false
false true false
false false false
12
Dr. KAMDEM Paul
Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications
The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its
two operands is true, thus being false only when both operands are false themselves. Here are the possible
results of a || b:
|| OPERATOR
a b a || b
true true true
true false true
false true true
false false false
For example:
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).
7. Conditional operator ( ? )
The conditional operator evaluates an expression returning a value if that expression is true and a different one
if the expression is evaluated as false. Its format is:
condition ? result1 : result2
If condition is true the expression will return result1, if it is not it will return result2.
// conditional operator 7
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
return 0;
}
The expression being evaluated (a>b) was not true, thus the first value specified after the question mark was
discarded in favor of the second value which was b, with a value of 7.
Bitwise operators modify variables considering the bit patterns that represent the values they store.
13
Dr. KAMDEM Paul
Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications
Type casting operators allow you to convert a given type to another. There are several ways to do this in C++.
The simplest one, which has been inherited from the C language, is to precede the expression to be converted
by the new type enclosed between parentheses ( ):
int i;
float pi = 3.14;
i = (int)pi;
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Another way to
do in C++ is using the functional notation: preceding the expression to be converted by the type and
enclosing the expression between parentheses ( ):
i = int(pi);
In C++, there is an established order with the priority of each operator. From greatest to lowest priority, the
priority order is as follows:
If you want to write complicated expressions and you are not completely sure of the precedence levels, always
include parentheses. It will also become a code easier to read.
14
Dr. KAMDEM Paul