About Me

Operators in C++

Operators

In all computer languages, expressions consist of two types of components: operands and operators. 

Operands are the objects that are manipulated and operators are the symbols that represent specific actions. 

For example, in the expression


X + Y

X and Y are operands and + is an operator. All expressions have at least one operand.


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. 











  • Assignment (=) 
  • Arithmetic operators ( +, -, *, /, % ) 
  • Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=,^=, |=) 
  • Increase and decrease (++, --) 
  • Relational and equality operators ( ==, !=, >, <, >=, <= ) 
  • Logical operators ( !, &&, || ) 
  • Conditional operator ( ? ) 
  • Comma operator ( , ) 
  • Bitwise Operators ( &, |, ^, ~, <<, >> ) 




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 value (left value) and the right one as the value (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: 
a = b; 





  • Arithmetic operators ( +, -, *, /, % ) 




Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) 




Increase and decrease (++, --)

A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluated as a value in an outer expression they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is
evaluated and therefore the increased value is considered in the outer expression; in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the outer expression. 
Notice the difference:



In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.



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. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. 
Here is a list of the relational and equality operators that can be used in C++:



Logical operators ( !, &&, || )

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:
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:


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.





Comma operator ( , )

The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
For example, the following code:

a = (b=3, b+2);


Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the 
value 5 while variable b would contain value 3.



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

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




Explicit type casting operator

Type casting operators allow you to convert a data type of 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 (()):
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the typecasting operator was (int).

Post a Comment

0 Comments