General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Operators

Assignment

Assignment Operators in Computer Programming

Assignment operators are used in programming to assign values to variables. The most basic assignment operator is the equal sign (=), but there are also compound assignment operators that combine assignment with another operation, such as addition, subtraction, multiplication, or division.

Basic Assignment Operator:

  • Equal (=): Assigns the value on the right to the variable on the left.

    • Example: a = 5 assigns the value 5 to the variable a.

Compound Assignment Operators:

These operators perform an operation on the variable and then assign the result back to the variable.

  1. Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.

    • Example: a += 3 is equivalent to a = a + 3.

  2. Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

    • Example: a -= 2 is equivalent to a = a - 2.

  3. Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.

    • Example: a *= 2 is equivalent to a = a * 2.

  4. Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.

    • Example: a /= 2 is equivalent to a = a / 2.

  5. Modulo Assignment (%=): Calculates the remainder of dividing the left operand by the right operand and assigns the result to the left operand.

    • Example: a %= 2 is equivalent to a = a % 2.

Examples in Programming Context:

# Example variable
a = 10

# Basic Assignment
a = 5  # a is now 5

# Addition Assignment
a += 2  # a is now 7 (5 + 2)

# Subtraction Assignment
a -= 3  # a is now 4 (7 - 3)

# Multiplication Assignment
a *= 2  # a is now 8 (4 * 2)

# Division Assignment
a /= 4  # a is now 2 (8 / 4)

# Modulo Assignment
a %= 2  # a is now 0 (2 % 2)

Usage:

Assignment operators are fundamental in programming for manipulating and storing values in variables. They are used extensively in various scenarios, from simple calculations to complex algorithmic computations. Compound assignment operators make the code more concise and often more readable by combining an arithmetic operation with assignment in a single step.

Efficiency:

In some cases, especially in low-level languages, compound assignment operators can be more efficient than their two-step counterparts, as they may reduce the number of operations performed. However, in high-level languages, this efficiency difference is usually handled by the compiler or interpreter.

PreviousLogicalNextConstants and Variables

Last updated 1 year ago

Was this helpful?