0% found this document useful (0 votes)
7 views

Lesson 2B Basic Operator in C

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 2B Basic Operator in C

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Basic Operator in

C++
GEADLITE – LIVING IN THE INFORMATION
TECHNOLOGY ERA
Operator
Once we know of the existence of variables and constants, we
can begin to operate with them. For that purpose, C++ integrates
operators. Unlike other languages whose operators are mainly
keywords, operators in C++ are mostly made of signs that are not
part of the alphabet but are available in all keyboards. This makes
C++ code shorter and more international, since it relies less on
English words, but requires a little of learning effort in the
beginning.
You do not have to memorize all the content of this page. Most
details are only provided to serve as a later reference in case you
need it.
Assignment (=)
The assignment 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 assignment 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 either a constant, a variable, the
result of an operation or any combination of these. The most important
rule when assigning is the right-to-left rule: The assignment operation
always takes place from right to left, and never the other way:
Assignment (=)
a = b;

This statement assigns to variable a (the lvalue) the value contained in


variable b (the rvalue). The value that was stored until this moment in a is
not considered at all in this operation, and in fact that value is lost.
Assignment (=)
For example, let us have a look at the following code - I have
included the evolution of the content stored in the variables as
comments:
Binary Arithmetic Operator
Operator Meaning Example
+ Addition 1+2=3
- Subtraction 4–1=3
* Multiplication 4 * 3 = 12
/ Division 12 / 4 = 3
% Modulus 15 % 4 = 3
(Remainder) 255 % 10 = 5
Unary Arithmetic Operators

Operator Meaning Example

++ Increment by 1 ++x or x++

-- Decrement by 1 --x or x--


Effect of prefix and postfix
notation
Precedence of Arithmetic
Operators
Other Assigned Operator
Symbols Meaning Normal Compressed
Form Form

+= Plus Equal i=i+2 i+=2


-= Minus Equal p=p-2 p-=2
*= Multiply Equal r=r*4 r*=4
/= Divide Equal j=j/2 j/=2
%= Modulus Equal n=n%2 n%=2
Relational Operators
Symbols Significance
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal
!= Not Equal
Example for comparisons:

Comparison Result
5 >= 6 False
1.7 < 1.8 True
4 + 2 == 5 False
2 * 4 != 7 True

You might also like