assignment operator computer science

An operator is symbol that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression) [2] .

There are many different types of operators, but the ones we primarily concern ourselves with are boldfaced below. If you'd like to learn more about different types of operators in PHP, please click here .

  • Operator Precedence
  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Error Control Operators
  • Execution Operators
  • Incrementing/Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators
  • 1 Assignment operators
  • 2 Arithmetic operators
  • 3 Comparison operators
  • 5 The way the IB wants you to use operators
  • 6 Python operators cheatsheet
  • 7 Standards
  • 8 References

Assignment operators [ edit ]

An assignment operator assigns a value to its left operand based on the value of its right operand. [3]

Arithmetic operators [ edit ]

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). [4]

An example of some arithmetic operators in PHP can be found below [5] .

An example of some arithmetic operators in Python can be found below [6] .

Comparison operators [ edit ]

Comparison operators, as their name implies, allow you to compare two values. [7]

An example of some conditional operators in PHP can be found below [8] .

An example of some conditional operators in Python can be found below [9] .

A video [ edit ]

This video references the C programming language and scratch, but the ideas about operators are excellent. In the case of conditionals, PHP and C share similar syntax (but not exact).

The way the IB wants you to use operators [ edit ]

Please know all code submitted to the IB (with the exception of object oriented programming option) is in pseudocode . The way we write operators in pseudocode is different than the way we might write them in the real world.

Click here to view a file describing approved notation, including pseudocode This is the approved notation sheet from the IB.

Below is a graphic that is taken from the above PDF file to help you understand how the IB wants operators to look.

Ib operators.png

Python operators cheatsheet [ edit ]

Click here for an excellent python cheatsheet

Standards [ edit ]

  • Define the terms: variable, constant, operator, object.
  • Define common operators.
  • Analyse the use of variables, constants and operators in algorithms.

References [ edit ]

  • ↑ http://www.flaticon.com/
  • ↑ http://php.net/manual/en/language.operators.php
  • ↑ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators
  • ↑ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
  • ↑ http://php.net/manual/en/language.operators.arithmetic.php
  • ↑ https://www.geeksforgeeks.org/basic-operators-python/
  • ↑ https://www.php.net/manual/en/language.operators.comparison.php
  • ↑ http://php.net/manual/en/language.operators.comparison.php

A natural number, a negative of a natural number, or zero.

anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution

Give the precise meaning of a word, phrase, concept or physical quantity.

Break down in order to bring out the essential elements or structure. To identify parts and relationships, and to interpret information to reach conclusions.

  • Computational thinking
  • Programming

CS101: Introduction to Computer Science I

Assignment operators.

Read this introductory article.

Simple Assignment Operator

The assignment statement is one of the central constructs in programming languages. Using an assignment statement, the programmer can change the binding of values to the variables in the program. In the simple assignment statements listed below, variables x and y are being assigned values 0 and 1 respectively. The variables are of type integer in this case.

int x = 0; int y = 1;

Compound Assignment Operators

The assignment operator can be combined with various arithmetic and logic operators to provide compound assignments where arithmetic and logic operations are part of the assignment operation. The assignment can be combined with the following arithmetic and logic operations (the variable x is of type integer, z is a Boolean vector, and y is of type Boolean in these examples).

Unary Assignment Operators

Java has two special unary arithmetic operators, which are special cases of compound assignment operators. These are the increment and the decrement operators. They combine increment and decrement operations (++ and --) with the assignment operation. For example:

x = ++count;

is equivalent to

count = count + 1; x = count;

Assignment for Object Creation

In object-oriented programming, classes provide a blueprint or a factory for creating objects. An object is an instance of a class. Using the new operation in Java, a programmer can create an instance of an object using the simple assignment operator. For example, if we have a rectangle class that uses length and width attributes, then:

rectangle R1 = new Rectangle(5, 10);

creates a new rectangle object named R1 with length 5 and width 10.

rectangle R2 = new Rectangle(7, 8);

creates another new rectangle object named R2 with length 7 and width 8.

Creative Commons License

Logo for Rebus Press

Want to create or adapt books like this? Learn more about how Pressbooks supports open publishing practices.

Kenneth Leroy Busbee

An assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. [1]

The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within most programming languages the symbol used for assignment is the equal symbol. But bite your tongue, when you see the = symbol you need to start thinking: assignment. The assignment operator has two operands. The one to the left of the operator is usually an identifier name for a variable. The one to the right of the operator is a value.

Simple Assignment

The value 21 is moved to the memory location for the variable named: age. Another way to say it: age is assigned the value 21.

Assignment with an Expression

The item to the right of the assignment operator is an expression. The expression will be evaluated and the answer is 14. The value 14 would be assigned to the variable named: total_cousins.

Assignment with Identifier Names in the Expression

The expression to the right of the assignment operator contains some identifier names. The program would fetch the values stored in those variables; add them together and get a value of 44; then assign the 44 to the total_students variable.

  • cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
  • Wikipedia: Assignment (computer science) ↵

Programming Fundamentals Copyright © 2018 by Kenneth Leroy Busbee is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License , except where otherwise noted.

Share This Book

pep

Assignment operator

The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.

Think of the assignment operator as a delivery person who takes a package from one place and delivers it to another place. The equals sign (=) acts like their hands, transferring the value from one side to another.

Related terms

Arithmetic operators : These are operators used for mathematical calculations such as addition (+), subtraction (-), multiplication (*), and division (/).

Comparison operators : These are operators used to compare two values or variables, such as greater than (>), less than (<), equal to (==), not equal to (!=).

Compound assignment operators : These are shorthand versions of assignment operators combined with other arithmetic or bitwise operations, such as +=, -=, *=, /=.

" Assignment operator " appears in:

Study guides ( 1 ).

AP Computer Science A - 1.3 Expressions and Assignment Statements

Additional resources ( 2 )

AP Computer Science A - Unit 1 Overview: Primitive Types

AP Computer Science A - Unit 2 Overview: Using Objects

Practice Questions ( 1 )

What is the assignment operator?

Fiveable

Stay Connected

© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

  • A named space for holding data/information
  • The name is often referred to as the identifier
  • A representation of a datum
  • A symbol indicating that an operation is to be performed (on one or more operands)
  • A combination of variables, operators, and literals that represent a single value
  • The smallest syntactically valid construct that conveys a complete command
  • Placing a value into the memory identified by a variable/constant
  • The assignment operator is a binary operator; the left-side operand is a variable/constant and the right-side operand is a literal or an expression (i.e., something that evaluates to a value)
  • initial = 'H'
  • initial = 'H';
  • ok = false;
  • A variable can only contain one value at a time
  • int i; i = 5; // i contains the value 5 i = 27; // i now contains the value 27
  • Confusing the assignment operator with the notion of "equality"
  • Attempting to use expressions like 21 = age (which has an inappropriate left-side operand)
  • The assignment operation evaluates to the value of the right-side operand (so, is an expression)
  • int i, j; i = j = 5;
  • It can cause confusion in more complicated statements, especially those involving arithmetic operators and relational operators
  • In the previous example, which assignment is performed first?
  • Associativity - determines whether (in the absence of other determinants) an expression is evaluated from left to right or right to left
  • The assignment operator has right to left associativity, so i = j = 5 is equivalent to i = (j = 5)
  • Ensuring that the type of the right-side operand is the same as the type of the left-side operand
  • Java does not allow implicit losses of precision
  • Java does allow widenings (though you should refrain from using them)
  • double weight; int age; age = 21.0; // Not allowed (will generate a compile-time error) weight = 155; // Allowed but not recommended (use 155. instead)
  • ( type ) expression
  • Examples: double real; int small; long large; // ... small = (int)large; // ... small = (int)real;
  • Loss of precision
  • Possible loss of magnitude
  • Possible change of sign
  • Indicate that a value can only be assigned to a variable once
  • final type variable [, variable ]... ;
  • final boolean CORRECT;
  • final double BUDGET;
  • final int CAPACITY;
  • Same as for other variables
  • Must be all uppercase
  • Underscores between "words" improve readability
  • An error will be generated if an identifier that is declared to be final is the left-side operand of more than one assignment operator
  • Include the assignment in the declaration
  • The value of the right-side operand is assigned to the left-side operand
  • The address of the right-side operand is assigned to the left-side operand

Library homepage

  • school Campus Bookshelves
  • menu_book Bookshelves
  • perm_media Learning Objects
  • login Login
  • how_to_reg Request Instructor Account
  • hub Instructor Commons
  • Download Page (PDF)
  • Download Full Book (PDF)
  • Periodic Table
  • Physics Constants
  • Scientific Calculator
  • Reference & Cite
  • Tools expand_more
  • Readability

selected template will load here

This action is not available.

Engineering LibreTexts

5.4: Arithmetic Assignment Operators

  • Last updated
  • Save as PDF
  • Page ID 10264

  • Kenneth Leroy Busbee
  • Houston Community College via OpenStax CNX

Overview of Arithmetic Assignment

The five arithmetic assignment operators are a form of short hand. Various textbooks call them "compound assignment operators" or "combined assignment operators". Their usage can be explaned in terms of the assignment operator and the arithmetic operators. In the table we will use the variable age and you can assume that it is of integer data type.

Demonstration Program in C++

Creating a folder of sub-folder for source code files.

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the Demo Program

Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the soruce code file(s) in conjunction with other learning materials.

Download from Connexions: Demo_Arithmetic_Assignment.cpp

Assignment Operators

Let's take a look at the assignment operator and a few related shorthands.

Introduction

  • Compound assignment operators
  • Increment and decrement operators

We have already seen the assignment operator, which is used to assign a value to a variable or update its value. The “equal to” sign( = ) is used for this operator. Anything on the right of = is assigned to the variable on its left.

Therefore, you always need to place a variable on the left side of = , whereas on the right side can be a value or another variable. Another thing you need to be careful about is the type. You can assign a double value or another double type variable only to a double type variable.

Let’s look at some use cases of the assignment operator.

Get hands-on with 1200+ tech skills courses.

Compound-Assignment Operators

  • Java Programming
  • PHP Programming
  • Javascript Programming
  • Delphi Programming
  • C & C++ Programming
  • Ruby Programming
  • Visual Basic
  • M.A., Advanced Information Systems, University of Glasgow

Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand.

Compound-Assignment Operators in Java

Java supports 11 compound-assignment operators:

Example Usage

To assign the result of an addition operation to a variable using the standard syntax:

But use a compound-assignment operator to effect the same outcome with the simpler syntax:

  • Conditional Operators
  • Java Expressions Introduced
  • Understanding the Concatenation of Strings in Java
  • The 7 Best Programming Languages to Learn for Beginners
  • Math Glossary: Mathematics Terms and Definitions
  • The Associative and Commutative Properties
  • The JavaScript Ternary Operator as a Shortcut for If/Else Statements
  • Parentheses, Braces, and Brackets in Math
  • Basic Guide to Creating Arrays in Ruby
  • Dividing Monomials in Basic Algebra
  • C++ Handling Ints and Floats
  • An Abbreviated JavaScript If Statement
  • Using the Switch Statement for Multiple Choices in Java
  • How to Make Deep Copies in Ruby
  • Definition of Variable
  • The History of Algebra
  • Python Basics
  • Interview Questions
  • Python Quiz
  • Popular Packages
  • Python Projects
  • Practice Python
  • AI With Python
  • Learn Python3
  • Python Automation
  • Python Web Dev
  • DSA with Python
  • Python OOPs
  • Dictionaries

Assignment Operators in Python

  • Augmented Assignment Operators in Python
  • Python Arithmetic Operators
  • Division Operators in Python
  • Python Bitwise Operators
  • Chaining comparison operators in Python
  • Increment and Decrement Operators in Python
  • Python Operators
  • Python: Operations on Numpy Arrays
  • Python Membership and Identity Operators
  • Modulo operator (%) in Python
  • Python NOT EQUAL operator
  • Assignment Operators in C
  • Assignment Operators In C++
  • Assignment Operators in Programming
  • Solidity - Assignment Operators
  • C++ Assignment Operator Overloading
  • JavaScript Assignment Operators
  • Java Assignment Operators with Examples
  • Compound assignment operators in Java
  • Adding new column to existing DataFrame in Pandas
  • Python map() function
  • Read JSON file using Python
  • How to get column names in Pandas dataframe
  • Taking input in Python
  • Read a file line by line in Python
  • Dictionaries in Python
  • Enumerate() in Python
  • Iterate over a list in Python
  • Different ways to create Pandas Dataframe

Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, bitwise computations. The value the operator operates on is known as Operand .

Here, we will cover Assignment Operators in Python. So, Assignment Operators are used to assigning values to variables. 

Now Let’s see each Assignment Operator one by one.

1) Assign: This operator is used to assign the value of the right side of the expression to the left side operand.

2) Add and Assign: This operator is used to add the right side operand with the left side operand and then assigning the result to the left operand.

Syntax: 

3) Subtract and Assign: This operator is used to subtract the right operand from the left operand and then assigning the result to the left operand.

Example –

 4) Multiply and Assign: This operator is used to multiply the right operand with the left operand and then assigning the result to the left operand.

 5) Divide and Assign: This operator is used to divide the left operand with the right operand and then assigning the result to the left operand.

 6) Modulus and Assign: This operator is used to take the modulus using the left and the right operands and then assigning the result to the left operand.

7) Divide (floor) and Assign: This operator is used to divide the left operand with the right operand and then assigning the result(floor) to the left operand.

 8) Exponent and Assign: This operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.

9) Bitwise AND and Assign: This operator is used to perform Bitwise AND on both operands and then assigning the result to the left operand.

10) Bitwise OR and Assign: This operator is used to perform Bitwise OR on the operands and then assigning result to the left operand.

11) Bitwise XOR and Assign:  This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.

12) Bitwise Right Shift and Assign: This operator is used to perform Bitwise right shift on the operands and then assigning result to the left operand.

 13) Bitwise Left Shift and Assign:  This operator is used to perform Bitwise left shift on the operands and then assigning result to the left operand.

Please Login to comment...

Similar reads.

author

  • Python-Operators

advertisewithusBannerImg

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Assignment Operators in C

    assignment operator computer science

  2. PPT

    assignment operator computer science

  3. Assignment Operators in C

    assignment operator computer science

  4. Assignment Operators in C » PREP INSTA

    assignment operator computer science

  5. PPT

    assignment operator computer science

  6. Assignment Operators in C

    assignment operator computer science

VIDEO

  1. assignment operators in c language

  2. C++ Assignment Operators Practice coding

  3. UPP Computer Operator Computer L15

  4. UPP Computer Operator Computer Lecture 10|SAIL OCTT 2024 CS/IT computer MCQ|UP Police Re Exam 2024

  5. how to use the Logical And Assignment Operator (&&=) #coding #javascript #tutorial #shorts

  6. "Mastering Assignment Operators in Python: A Comprehensive Guide"

COMMENTS

  1. Assignment Operators in Programming

    Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...

  2. Assignment (computer science)

    Assignment (computer science) In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location (s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct.

  3. 4.6: Assignment Operator

    This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left. Example: (a -= b) can be written as (a = a - b) If initially value 8 is stored in the variable a, then (a -= 6) is equal to 2. (the same as a = a - 6)

  4. 4.5: Assignment Operator

    Discussion. The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within C++ programming language the symbol used is the equal symbol.

  5. Operators

    An assignment operator assigns a value to its left operand based on the value of its right operand. Arithmetic operators . Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and ...

  6. CS101: Assignment Operators

    The assignment operator can be combined with various arithmetic and logic operators to provide compound assignments where arithmetic and logic operations are part of the assignment operation. The assignment can be combined with the following arithmetic and logic operations (the variable x is of type integer, z is a Boolean vector, and y is of ...

  7. Assignment

    The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within most programming languages the symbol used for assignment is the equal symbol.

  8. Assignment operator

    AP Computer Science A. Assignment operator. Assignment operator. Definition. The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side. Analogy.

  9. The Assignment Operator

    The Operation: Placing a value into the memory identified by a variable/constant; The Operator (in Java): = Operands: The assignment operator is a binary operator; the left-side operand is a variable/constant and the right-side operand is a literal or an expression (i.e., something that evaluates to a value)

  10. 5.4: Arithmetic Assignment Operators

    This page titled 5.4: Arithmetic Assignment Operators is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee ( OpenStax CNX) . The five arithmetic assignment operators are a form of short hand. Various textbooks call them "compound assignment operators" or "combined assignment operators". Their usage ...

  11. Assignment (computer science)

    Assignment (computer science) In the context of programming and computer science, an assignment operator is a symbol or operator used to assign a value to a variable. It is a fundamental concept in most programming languages. It is used to store a value in a variable so that it can be manipulated and used in computations later in the code.

  12. Assignment Operators

    Introduction. We have already seen the assignment operator, which is used to assign a value to a variable or update its value. The "equal to" sign(=) is used for this operator.Anything on the right of = is assigned to the variable on its left.. Therefore, you always need to place a variable on the left side of =, whereas on the right side can be a value or another variable.

  13. Assignment (computer science)

    In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location (s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct. Today, the most commonly used notation ...

  14. Assignment Operator

    C-like assignment operators have been added to NASL for convenience.. ++ and — NASLsupports the incrementing and decrementing operators. ++ increases the value of a variable by Í, and decreases the value of a variable by Í.There are two ways to use each of these operators. . When used as a postfix operator (e.g., x++ or χ—) the present value of the variable is returned before the new ...

  15. Javascript Assignment Operators

    All other combined assignment operators work in a similar way to the += operator. They each perform the arithmetical operation on their two operands first, then assign the result of that operation to the first variable - so that becomes its new stored value. The === equality operator compares values and is fully explained here.

  16. What Is a Compound-Assignment Operator?

    Compound assignment operators provide a shorter syntax to assign the results of the arithmetic and bitwise operators. ... Computer Science Expert. M.A., Advanced Information Systems, University of Glasgow; Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based ...

  17. Operators

    Operators. An operator is a character, or characters, that determine what action is to be performed or considered. There are three types of operator that programmers use: mathematical operators ...

  18. Assignment Operators in Python

    A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. ... So, Assignment Operators are used to assigning values to variables. Operator. Description. Syntax = Assign value of right side of ...

  19. Relational operator

    In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities.These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).. In programming languages that include a distinct boolean data type in their type system, like Pascal, Ada, or Java, these operators usually evaluate to true ...

  20. Arrays

    A better method is to use an array close array A set of data values of the same type, stored in a sequence in a computer program. Also known as a list. .An array is a data structure that holds ...