Lokang 

C++ and MySQL

Assignment operators

Assignment operators in C++ are used to assign values to variables. The most basic assignment operator is the equal sign (=), which assigns the value on the right-hand side to the variable on the left-hand side. However, C++ also provides several compound assignment operators that combine arithmetic operations with assignment.

Types of Assignment Operators

Simple Assignment (=)

  • Description: Assigns the value of the right-hand operand to the left-hand operand.
  • Example: a = b assigns the value of b to a.

Add and Assign (+=)

  • Description: Adds the right-hand operand to the left-hand operand and then assigns the result to the left-hand operand.
  • Example: a += b is equivalent to a = a + b.

Subtract and Assign (-=)

  • Description: Subtracts the right-hand operand from the left-hand operand and then assigns the result to the left-hand operand.
  • Example: a -= b is equivalent to a = a - b.

Multiply and Assign (*=)

  • Description: Multiplies the left-hand operand by the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a *= b is equivalent to a = a * b.

Divide and Assign (/=)

  • Description: Divides the left-hand operand by the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a /= b is equivalent to a = a / b.

Modulus and Assign (%=)

  • Description: Takes the modulus of the left-hand operand with the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a %= b is equivalent to a = a % b.

Bitwise AND and Assign (&=)

  • Description: Performs a bitwise AND operation on the left-hand operand and the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a &= b is equivalent to a = a & b.

Bitwise OR and Assign (|=)

  • Description: Performs a bitwise OR operation on the left-hand operand and the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a |= b is equivalent to a = a | b.

Bitwise XOR and Assign (^=)

  • Description: Performs a bitwise XOR operation on the left-hand operand and the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a ^= b is equivalent to a = a ^ b.

Left Shift and Assign (<<=)

  • Description: Shifts the bits of the left-hand operand to the left by the number of positions specified by the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a <<= b is equivalent to a = a << b.

Right Shift and Assign (>>=)

  • Description: Shifts the bits of the left-hand operand to the right by the number of positions specified by the right-hand operand and then assigns the result to the left-hand operand.
  • Example: a >>= b is equivalent to a = a >> b.

Example Code

Let us look at an example to see how these assignment operators work:

#include <iostream>
using namespace std;
int main() {
   int a = 10, b = 5;
   // Simple assignment
   a = b;  // a becomes 5
   cout << "a = b; a = " << a << endl;
   // Add and assign
   a += b; // a = a + b; a becomes 10
   cout << "a += b; a = " << a << endl;
   // Subtract and assign
   a -= b; // a = a - b; a becomes 5
   cout << "a -= b; a = " << a << endl;
   // Multiply and assign
   a *= b; // a = a * b; a becomes 25
   cout << "a *= b; a = " << a << endl;
   // Divide and assign
   a /= b; // a = a / b; a becomes 5
   cout << "a /= b; a = " << a << endl;
   // Modulus and assign
   a %= b; // a = a % b; a becomes 0
   cout << "a %= b; a = " << a << endl;
   return 0;
}

Explanation of the Example

  1. Simple Assignment (=): Initially, a is assigned the value of b, so a becomes 5.
  2. Add and Assign (+=): a is then increased by b (a = a + b), so a becomes 10.
  3. Subtract and Assign (-=): a is decreased by b (a = a - b), so a becomes 5 again.
  4. Multiply and Assign (*=): a is multiplied by b (a = a * b), so a becomes 25.
  5. Divide and Assign (/=): a is divided by b (a = a / b), so a becomes 5.
  6. Modulus and Assign (%=): Finally, a is taken modulus b (a = a % b), which results in a becoming 0.

This code demonstrates how the assignment operators work and how they can simplify code by combining operations with assignments.

Conclusion

Assignment operators in C++ are powerful tools that allow you to simplify and streamline your code. By combining arithmetic and bitwise operations with assignments, these operators make your code more concise and easier to read. Understanding and practicing with these operators will help you write more efficient C++ programs.