operators - sluczak/cpp_by_example GitHub Wiki

Return to the previous lesson assignments and casting

Operators


Examples for this part available on git after calling the command:

git checkout -b origin operators

What are operators used for?

Operators are used to interact with the variables in order to assure the proper application flow and/or change value of variables. There are:

  • arithmetic,
  • logical,
  • relation and
  • other operators.

Arithmetic operators

Two-variable operators

Arithmetic operators are mostly the two-variable operators. They represent the arithmetic operations on variables.

Arithmetic operations may be organized with use of parenthesis ( and ). Code below shows the usage of such operators:

   int a = 2 - 1;       // subtraction
   int b = a + 6;       // addition
   int c = a * (b + 1); // multiplication with nested operation 
   int d = c / 1;       // division

One-variable operators

The simpliest one-variable operator available in C++ is a negation of value; c++ int i = 2; int b = -i; // b = -2

C++ has also some arithmetic one-argument operators, which are used to easily increment or decrement integer value.

++i is a pre-increment operator. Value of i will be incremented before accessing the variable

i++ is a post-increment operator. Value of i will be incremented, but the current access to the variable will return the value before incrementation.

and analogously --i pre-decrement and i-- post-decrement.

The subtle difference between pre and post operations are shown in the source code below:

int a = 5;
int b = ++a; // a = 6; b = 6; - pre-incrementation

int c = 5;
int d = c++; // c = 6; d = 5; - post-incrementation

The post-incrementation operator might be a little bit confusing, because:

First, d will be assigned value of c before incrementation, so d = 5;

Second, c will be incremented, so c = 6;

Operation with assignment

There is also third type of arithmetic operators, which combine the arithmetic operation on variable and the instant assignment of the result value: +=, -=, *=, /=.

Those operators and its results are shown in the source code below:

 int a = 5;
 int b = 3;
 a += 3;    // "add 3 to a and assign result to a"        a = 5 + 3 = 8;
 a *= b;    // "multiply a by b and assign to a"          a = 8 * 3 = 24;
 a /= b;    // "divide a by b and assign to a"            a = 24 / 3 = 8;
 b -= 1;    // sybstract 1 from b and assign result to b" b = 3 - 1 = 2;

Relation operators

In order to control the operations flow, c++ has the common relation operators. Their result is always a value of boolean logic: true or false. Here are listed all the relation operators available:

a < b - if a value is lower than b, then return true. Else return false

a > b - if a value is greater than b, then return true. Else return false

a <= b - if a value is lower than, or equal to b, then return true. Else return false

a >= b - if a value is greater than, or equal to b, then return true. Else result false

While operators above doesn't work with logical bool variables, operators below can be used for both: numerical and boolean values.

a == b - if a value is not equal to b, then return true. Else return false

a != b - if a value is not equal to b, then return true. Else return false

The relational operators are useful in flow control instructions:

int a = 2;
int b = 3;
bool isBusy = false;

if(a > b) {
   //do something
}

if(a != b) {
   //do something else
}

if(isBusy == false) {
   //rest
}

Logical operators

Logical operators are used to perform operations on boolean-only variables and values. The logical negation operator - ! exclamation mark is used to invert the value of variable.

   bool isBusy = true;
   if(!isBusy) { //if is not busy
       //rest
   }

   bool logicalValue = true;
   bool negationOfLogicalValue = !logicalValue; // negationOfLogicalValue = false;

In case of situations, where several conditions has to be met simultaneously, programmer may use the two-variable operators listed below:

&& - logical AND

|| - logical OR

conditionA && conditionB returntrue only if conditionA and conditionB are true

conditionA || conditionB return true if both: conditionA and conditionB are true or conditionA is true or conditionB is true

Other operators

C++ has also a three-argument conditional operator:

condition ? doInResultForTrue : doInResultForFalse;

if-condition will be covered in flow control topic

This operator is a shortened version of if-condition, but because of low readability, I suggest to use normal if-condition instead of it.

There are also bitwise operators and stream operators, but we won't cover them in here.

stream operators will be shown with streams topic

Proceed to the next leson: namespaces