C++ operators, compound assignments

Now we know how to use variables and constants, we can begin to use them with operators. Operators are integrated in the C++ language. The C++ operators are mostly made out of signs (some language use keywords instead.)

Assignment

We used this operator before and it should already be known to you. For the people that didn’t read the previous tutorials we will give a short description.

With an assignment (=) operator you can assign a value to a variable.
For example: A = 5; or B = -10; or A = B;
Let’s look at A = B : The value that is stored in B will be stored in A. The initial value of A will be lost.

So if we say:

	
        A=5;
	B=20;
	A=B;

Then A will contain the value twenty.

The following expression is also valid in C++: A = B = C = 10;
The variables A,B,C will now contain the value ten.

Calculations (arithmetic operators)

There are different operators that can be used for calculations which are listed in the following table:

Operator

Operation

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus(Remainder
of integer
division)

Now that we know the different operators, let’s calculate something:


	int main()
	{
		int a, b;
		A = 10;
		B = A + 1;
		A = B - 3;
		return 0;
	}

Note: The value stored in A at the end of the program will be eight.

Compound assignments

Compound assignments can be used when you want to modify the value of a variable by performing an operation on the value currently stored in that variable. (For example: A = A + 1 ).

  • Writing <var> += <expr> is the same as <var> = <var> + <expr>.
  • Writing <var> -= <expr> is the same as <var> = <var> – <expr>.
  • Writing <var> /= <expr> is the same as <var> = <var> / <expr>.
  • Writing <var> *= <expr> is the same as <var> = <var> * <expr>.

Decrease and increase operators

The increase operator (++) and the decrease operator (–) are used to increase or reduce the value
stored in the variable by one.

Example: A++; is the same as A+=1; or A= A + 1;

A characteristic of this operator is that it can be used as a prefix or as a suffix (before or after). Example: A++; or ++A; have exactly the same meaning. But in some expressions they can have a different result.

For instance: In the case that the decrease operator is used as a prefix (–A) the value is decreased before the result of the expression is evaluated. Example:

	
        My_var = 10;
	A = --My_var;

Note:My_var is decreased before the value is copied to A. So My_var contains 9 and A will contain 9.

In case that it is used as a suffix (A–) the value stored in A is decreased after being evaluated and therefore the value stored before the decrease operation is evaluated in the outer expression. Example:

	
        My_var = 10;
	A = My_var--;

Note:The value of My_var is copied to A and then My_var is decreased. So My_var will contain 9 and A will contain 10.

Relation or equal operators

With the relation and equal operators it is possible to make a comparison between two expressions. The result is a Boolean value that can be true or false. See the table for the operators:

==

Equal
to

!=

Not equal
to

>

Greater
than

<

Less
than

>=

Greater than or equal
to

<=

Less than or equal
to

You have to be careful that you don’t use one equal sign (=) instead of two equal signs (==). The first one is an assignment operator, the second one is a compare operator.

Logical operators

Logical operators are mainly used to control program flow. Usually, you will find them as part of an if, while, or some other control statement. The operators are:

  • <op1> || <op2> – A logical OR of the two operands
  • <op1> && <op2> – A logical AND of the two operands
  • ! <op1> – A logical NOT of the operand.

Logical operands allow a program to make decisions based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the statement. Take a look at the tables below:

Table: && operator (AND)

<op1>

<op2>

<op1> && <op2>

true

true

true

true

false

false

false

true

false

false

false

false

Table: || operator (OR)

<op1>

<op2>

<op1> || <op2>

true

true

true

true

false

true

false

true

true

false

false

false

Some examples:


!(10 <= 5) 			// (10 <= 5) is false but the NOT (!) makes it true. !true				// is false ( (10 = 10) && (5 > 10))	// is false. (true && false)
( (5 > 10) || ( 10 == 10) )	// is true. (false || true)

Bitwise operators

The bitwise operators are similar to the logical operators, except that they work with bit patterns. Bitwise operators are used to change individual bits in an operand.

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

That is all for this tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 5 responses to “C++ operators, compound assignments”

Why not let us know what you think by adding your own comment!

  1. GIRINDRA MOHAN on February 28th, 2011:

    good concept given for all ……

  2. Ricardo on October 11th, 2011:

    In the following note:

    Note:The value of My_var is copied to A and then My_var is increased. So My_var will contain 9 and A will contain 10.

    Don’t you mean the value of My_var is copied to A and then My_var is DECREASED? It may be a small typo (I hope) otherwise I am confused

  3. admin on October 11th, 2011:

    @Ricardo – You are right that is a typo. I’ve corrected the text to: The value of My_var is copied to A and then My_var is decreased. So My_var will contain 9 and A will contain 10. Thanks for your correction!

  4. Shay on January 28th, 2014:

    In the example of logical operator:
    ( (5 > 10) || ( 10 = 10)) // is true. (false || true)
    Shouldn’t we use double equal signs instead of single equal sign, because here we have comparison not assignment!?

  5. admin on February 5th, 2014:

    @shay: You are right, so i corrected the error.