0% found this document useful (0 votes)
117 views5 pages

C++ Prog and Appl

The document discusses various C++ operators including assignment, arithmetic, compound assignment, increment/decrement, relational, logical, conditional, bitwise, and type casting operators. It provides examples of how each operator works and the equivalent operations. Key operators covered include assignment (=), arithmetic (+ - * / %), relational (== != > < >= <=), logical (! && ||), and conditional (? :) operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views5 pages

C++ Prog and Appl

The document discusses various C++ operators including assignment, arithmetic, compound assignment, increment/decrement, relational, logical, conditional, bitwise, and type casting operators. It provides examples of how each operator works and the equivalent operations. Key operators covered include assignment (=), arithmetic (+ - * / %), relational (== != > < >= <=), logical (! && ||), and conditional (? :) operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications

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 assignation operator assigns a value to a variable.


a = 5;
This statement assigns the integer value 5 to the variable a. The part at the left of the assignation operator
(=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a
variable whereas the rvalue can be a constant, a variable, the result of an operation or any combination of
these.

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

// assignation operator a:4 b:7


#include <iostream>
using namespace std;

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;
}

2. Arithmetic operators ( +,-,*,/,% )

The five arithmetical operations supported by the C++ language are:


+ addition
- subtraction
* multiplication
/ division
% modulo

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

value += increase; value = value + increase;


a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);

And the same for all other operators. For example:

// 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;
}

4. Increase and decrease (++, --)

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,

(a == 5) // evaluates to false since a is not equal to 5.


(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.

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.

6. Logical operators ( !, &&, || )

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:

!(5 == 5) // evaluates to false because the expression (5 == 5) is true.


!(6 <= 4) // evaluates to true because (6 <= 4) would be false.
!true // evaluates to false
!false // evaluates to true.

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.

8. Bitwise Operators ( &,|,^,~,<<,>> )

Bitwise operators modify variables considering the bit patterns that represent the values they store.

operator asm equivalent description


& AND Bitwise AND
| OR Bitwise Inclusive OR
^ XOR Bitwise Exclusive OR
~ NOT Unary complement (bit inversion)
<< SHL Shift Left
>> SHR Shift Right

13
Dr. KAMDEM Paul
Uba/HTTTC Bambili – EPET4102 (EE Level 400) C++ programming and applications

9. Explicit type casting operator

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);

10. Precedence of operators

In C++, there is an established order with the priority of each operator. From greatest to lowest priority, the
priority order is as follows:

Level Operator Description Grouping


1 :: scope Left-to-right
2 () [] . -> ++ -- postfix Left-to-right
++ -- ~ ! unary (prefix)
3 * & indirection and reference (pointers) Right-to-left
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= != assignment Right-to-left

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

You might also like