Main menu

Pages

C++ Increment and Decrement Operators - learn CPP

C++ Increment and Decrement Operators - learn CPP, Some applications use counters for specific purposes, and they usually start with zero or any other number, and increase or decrease by one (or more) each time. In C++, there are two very important factors: the increment operator ++ ), and the decreasing factor — (decrement), and both are unimodal, meaning that they work on one operand, and the increase factor is usually written as follows: count=count+1;


C++ Increment and Decrement Operators - learn CPP


Increase and Decrease Factor


C++ has provided a single operator for shortening for this purpose since this operator is widely used and it is (++) for the purposes of increasing by one or (–) for purposes of decreasing by one, where this operator is used in two ways: Either it precedes the variable like (j++) Or it is followed by (j++), and each has a special meaning when the variable precedes the increment factor, the variable increases its value by one and then is used, but if the increment factor comes after the variable, the variable is used according to its current value and then its value is increased by one.


As for the (–) operator, it works in the same way as the increased operator, with the difference that its use reduces the value of the variable by one, for example:


If we assume the variable (b=7) and the variable (a=2), what is the value of the variable (c) in the following expression?


C=a*++b;


Here the compiler will increment the value of (b) to (8) and then calculate the result of the expression, so the value of (c = 16).


As for its value in the following expression:


C=a*b++;


Here the compiler will use the real value of the variable (b) and then calculate the result of the expression, and then the variable (b) is incremented to become (8), so the value of (c = 14).


c=a*–b;


Here the value of (c) is (12) where the compiler will reduce the value of (b) by one to be (6) and then substitute its value in the expression to find the value of (c).


As for its value in the following expression:


c=a*b-;


It is (14) where you will use the real value of (b) (7) to find the value of (c), then reduce the value of (b) to be (6).


Comma in C++


The comma (,) as operators the comma, which is a binary tool, occupies the last precedence in a ladder and takes the general form: Expression 1, Expression 2. It is also used to separate two expressions to the right of equality.


Arithmetic Priorities


  • The addition of two texts when they come before the number —, ++.
  • Parentheses ().
  • The negative sign (-).
  • Division and the remainder of division and multiplication (the operation closest to the left is presented).
  • Addition and subtraction.
  • Equality =.
  • The late increase and decrease after the number ++, -.