Assignment Operators
Arithmetic Operators
 Assignment operators are used to assign values to variables in
programming languages, including C++.
 They provide a concise and efficient way to update the value of a
variable based on a computation or another variable's value.
 In C++, there are different assignment operators available,
including the basic assignment operator (=) and compound
assignment operators (+=, -=, *=, /=, %=).
Different Assignment Operators
 Basic Assignment Operator (=): The basic assignment operator is
used to assign a value to a variable.
 It assigns the value on the right-hand side to the variable on the left-
hand side. Here's an example:
In the code, different
assignment operators
are used to modify
the value of num1
based on num2 and
display the updated
values.
Example
1. Write a program that asks for an integer number, adds 1, and
assigns to the number using the += operator.
Sample Output 1
Enter an integer number: 1
The updated number is: 2
Sample Output 2
Enter an integer number: 2
The updated number is: 3
Example
2. Write a program that asks for two integer numbers, x, and y. Then,
add y to x, and assign it to x using the += operator.
Sample Output 1
Enter x: 10
Enter y: 5
Result: 15
Sample Output 2
Enter x: 1
Enter y: 1
Result: 2

Lesson 2 - Introduction to Assignment Operators.pptx

  • 1.
  • 2.
    Arithmetic Operators  Assignmentoperators are used to assign values to variables in programming languages, including C++.  They provide a concise and efficient way to update the value of a variable based on a computation or another variable's value.  In C++, there are different assignment operators available, including the basic assignment operator (=) and compound assignment operators (+=, -=, *=, /=, %=).
  • 3.
    Different Assignment Operators Basic Assignment Operator (=): The basic assignment operator is used to assign a value to a variable.  It assigns the value on the right-hand side to the variable on the left- hand side. Here's an example:
  • 4.
    In the code,different assignment operators are used to modify the value of num1 based on num2 and display the updated values.
  • 5.
    Example 1. Write aprogram that asks for an integer number, adds 1, and assigns to the number using the += operator. Sample Output 1 Enter an integer number: 1 The updated number is: 2 Sample Output 2 Enter an integer number: 2 The updated number is: 3
  • 6.
    Example 2. Write aprogram that asks for two integer numbers, x, and y. Then, add y to x, and assign it to x using the += operator. Sample Output 1 Enter x: 10 Enter y: 5 Result: 15 Sample Output 2 Enter x: 1 Enter y: 1 Result: 2