Compound Assignment Operators C++ Example Tutorial

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:

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:

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:

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:

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:

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.

HACKED BY SudoX — HACK A NICE DAY.

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! or use to share this post with others.

Before writing a program to solve a problem, you should have a thorough understanding of the problem and a carefully planned approach. In this chapter, you will learn the theory and principles of structured programming with control statements.

This chapter is from the book 
  • Let's all move one place on.
  • Lewis Carroll
  • The wheel is come full circle.
  • William Shakespeare
  • How many apples fell on Newton's head before he took the hint?
  • Robert Frost

4.1 Introduction

Before writing a program to solve a problem, you should have a thorough understanding of the problem and a carefully planned approach. When writing a program, it's also important to know the available building blocks and to use proven program-construction principles. In this chapter and the next, we present the theory and principles of structured programming with control statements. We introduce the ..., ......, ..., ... and ... statements—five of the building blocks that allow you to specify the logic required for methods to perform their tasks. We introduce the control and use it to display and process data, such as a list of grades in a class-average program. We also demonstrate how to "stack" and "nest" control statement to solve problems. The chapter concludes with an introduction to the Visual Studio debugger. You'll learn how to view the values of variables during a program's execution, how to step through code one statement at a time and how the debugger can help you locate logic errors in your programs.

One thought on “Compound Assignment Operators C++ Example Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *