Increment , Decrement and Assignment Operators

COS1511 - Introduction To Programming I · Learning C++

Increment, Decrement and Assignment Operators

In C++, operators are special symbols that perform operations on variables and values. This topic focuses on three important types of operators: increment operators, decrement operators, and assignment operators.

Assignment Operators

The assignment operator is used to assign a value to a variable. In C++, the assignment operator is the equal sign (=). For example:

int my_var = 10;

This line of code creates an integer variable named my_var and assigns it the value of 10. You can also assign the value of one variable to another:

int another_var = my_var;

Now, another_var will also hold the value 10.

Remember: The assignment operator (=) does not mean equality. It assigns a value to a variable.

Increment Operators

The increment operator increases the value of a variable by 1. There are two forms of the increment operator: the prefix increment (++) and the postfix increment (++)

1. **Prefix Increment**: Increments the variable before it is used in an expression.

++my_var;

This increases my_var by 1 before it is used in any expression.

2. **Postfix Increment**: Increments the variable after it is used in an expression.

my_var++;

This increases my_var by 1 after its current value is used.

Example of Increment Operators

Consider the following code:

int my_var = 5; // my_var is 5
int result = ++my_var; // result will be 6, my_var is now 6
int another_result = my_var++; // another_result will be 6, my_var is now 7

In this example, result gets the incremented value of my_var, while another_result gets the original value before the increment.

Watch out: Be careful when using increment operators in complex expressions. The order of operations can affect the final result.

Decrement Operators

The decrement operator decreases the value of a variable by 1. Similar to the increment operator, there are prefix and postfix forms.

1. **Prefix Decrement**: Decreases the variable before it is used.

--my_var;

2. **Postfix Decrement**: Decreases the variable after it is used.

my_var--;

Example of Decrement Operators

Consider this code:

int my_var = 5; // my_var is 5
int result = --my_var; // result will be 4, my_var is now 4
int another_result = my_var--; // another_result will be 4, my_var is now 3

Again, result gets the decremented value, while another_result gets the original value before the decrement.

Summary

  • The assignment operator (=) assigns values to variables.
  • The increment operator (++) increases a variable's value by 1.
  • The decrement operator (--) decreases a variable's value by 1.
  • Both increment and decrement operators have prefix and postfix forms.

Check your understanding

  1. What is the difference between the prefix and postfix increment operators?
  2. Write a code snippet that demonstrates the use of the decrement operator.
  3. Explain why the assignment operator is not the same as the equality operator.
  4. What happens if you use the increment operator multiple times in a single expression?