assignment operator

Download 1478 free assignment operator icons in all design styles..

Get free Assignment operator icons in iOS, Material, Windows and other design styles for web, mobile, and graphic design projects. These free images are pixel perfect to fit your design and available in both PNG and vector. Download icons in all formats or edit them for your designs.

Also, be sure to check out new icons and popular icons .

  • User documentation

3D Illustrations

  • View all plugins

Smart Tools

Illustration Kit Editor

  • 3D Illustrations
  • Lottie Animations
  • Illustrations
  • Illustration Kits

22,155 Assignment Operator Icons

  • colored outline, 
  • dual tone, 
  • isometric, 
  • Assignment Operator 3D Illustrations, 
  • Assignment Operator Animations, 
  • Assignment Operator Illustrations
  • Illustration Kits NEW

Color Palette

  • Business Management
  • Design Shapes
  • Entertainment
  • Agriculture
  • Communication
  • Medical Equipment
  • Car Service
  • Cryptocurrency
  • Data Communication
  • Instructions
  • Artificial Intelligence (AI)
  • Data Security
  • Hand Gesture

Alignment Icons

Align icons, format icons, center icons, layout icons, design icons, right icons, assignment icons, paragraph icons, document icons, interface icons, align center icons, template icons, center align icons, center alignment icons, web layout icons, left alignment icons, left align icons, arrow icons, right align icons, align right icons, align left icons, right alignment icons, justify icons, vertical icons, explore assignment operator icon packs.

Assignment Operator Assignment Operators Icon

Browse by Categories

  • Browse More

Access the world's largest Design Ecosystem: Assets, Integrations, and Motion.

Sales banner

  • entertainment icon
  • icons of weightlifting
  • pill icons free
  • icons for dos
  • icons of torpedo
  • icons of pinterest
  • robotics icons
  • icon yammer
  • Apps & Plugins

Noun Project

  • Submit Icons
  • Submit Photos
  • Customer Support
  • Affiliate Program

Browse Assignment Operator Icons

assignment operator icon

  • Language Reference

Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

The value of an assignment expression is the value assigned. That is, the value of " $a = 3 " is 3. This allows you to do some tricky things: <?php $a = ( $b = 4 ) + 5 ; // $a is equal to 9 now, and $b has been set to 4. ?>

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic , array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: <?php $a = 3 ; $a += 5 ; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello " ; $b .= "There!" ; // sets $b to "Hello There!", just like $b = $b . "There!"; ?>

Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop.

An exception to the usual assignment by value behaviour within PHP occurs with object s, which are assigned by reference. Objects may be explicitly copied via the clone keyword.

Assignment by Reference

Assignment by reference is also supported, using the " $var = &$othervar; " syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere.

Example #1 Assigning by reference

The new operator returns a reference automatically, as such assigning the result of new by reference is an error.

The above example will output:

More information on references and their potential uses can be found in the References Explained section of the manual.

Arithmetic Assignment Operators

Example Equivalent Operation
$a += $b $a = $a + $b Addition
$a -= $b $a = $a - $b Subtraction
$a *= $b $a = $a * $b Multiplication
$a /= $b $a = $a / $b Division
$a %= $b $a = $a % $b Modulus
$a **= $b $a = $a ** $b Exponentiation

Bitwise Assignment Operators

Example Equivalent Operation
$a &= $b $a = $a & $b Bitwise And
$a |= $b $a = $a | $b Bitwise Or
$a ^= $b $a = $a ^ $b Bitwise Xor
$a <<= $b $a = $a << $b Left Shift
$a >>= $b $a = $a >> $b Right Shift

Other Assignment Operators

Example Equivalent Operation
$a .= $b $a = $a . $b String Concatenation
$a ??= $b $a = $a ?? $b Null Coalesce
  • arithmetic operators
  • bitwise operators
  • null coalescing operator

Improve This Page

User contributed notes 4 notes.

To Top

AI generator

Assignment Operators images

307 assignment operators stock photos, vectors, and illustrations are available royalty-free for download..

Smiling Caucasian worker in overalls and a helmet uses a walkie-talkie, looks at the camera sitting at desk in the control room. Automated workplace of dispatcher or operator of a modern enterprise Stock Photo

Python Tutorial

File handling, python modules, python numpy, python pandas, python matplotlib, python scipy, machine learning, python mysql, python mongodb, python reference, module reference, python how to, python examples, python assignment operators.

Assignment operators are used to assign values to variables:

Operator Example Same As Try it
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Related Pages

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

Breaking down the A-to-Zs of VBA to turbocharge your productivity.

Assignment operator in vba.

  • by Sola Bode
  • in VBA Coding Constructs
  • February 25, 2021

assignment operator image

In this article:

Vba operators.

VBA operators are special tokens that instruct the compiler to “ operate ” on values or program identifiers. They are also referred to as program symbols or code elements. Also, the program identifiers they operate on (e.g., addition) must be capable of holding values.

You may have heard of the term operand . These are the values (i.e., literals ) or identifiers (e.g., variables and constants) that operators act on. Based on their number of operands, operators are either unary or binary.

Unary VBA operators are operators that only act on a single operand at a time. Binary VBA operators are those that only act on two operands at a time.

VBA supports the following categories of operators:

  • Assignment Operator;
  • Member Access Operators ;
  • Concatenation Operators ;
  • Arithmetic Operators ;
  • Comparison Operators ;
  • Logical Operators .

This article takes an in-depth look at the VBA assignment operator.

VBA Operator Precedence

Often, an operation involves more than one operator, sometimes of different categories. So, it is worthwhile paying special attention to the order of precedence of the VBA operators. Otherwise, VBA’s default operator precedence applies.

The default precedence is as follows: arithmetic first, then concatenation, followed by comparison, then logical, and finally assignment operators.

Moreover, there is a pre-set precedence for operators within each category. How do you override this default order and remove any ambiguity? Easy, set the preferred precedence with open and close parentheses, ( ).

Assignment Operator

VBA supports a single assignment operator, =. It stores the value of the operand on its right in the operand on its left. Afterward, both operands hold the same value.

Moreover, the operand on the right can comprise several expressions. Note that expressions are combinations of operands and operators. Meanwhile, the identifier on the left must be a value-holding program identifier.

In any case, both operands must be of the same data type. Otherwise, they can’t hold the same values.

So, the assignment operator writes the value of the right-side expression to the memory address of the left-side program identifier.

The sample code below illustrates the assignment operator’s usage.

Explicit AssignmentOperator_Example01() Dim x As Integer, y As Integer, z As Integer 'left-side value gets written into right-side identifier" x = 1: y = 3: z = 5 Debug.Print "x = "; x & ", y = "; y & ", z = "; z 'assignments cannot be chained z = y = x = 10 Debug.Print "x = "; x & ", y = "; y & ", z = "; z Sub

The sample code also shows that, unlike in languages such as C, assignments cannot be chained in VBA. The results in the Immediate window (see the image below) demonstrate this.

Sample code showing the Assignment operator’s usage.

Besides the assignment operator, there are several expression statements that also perform assignments. These include the Let , Set , Get , Put , Input # , Line Input # , Print # , and Write # statements.

guest

Subscribe Now

To stay updated on our freshest Office & VBA articles and freebies.

Article Tags:

  • Operators , VBA Programming
  • Fresh Off The Stove

assignment operator image

VBA Statements: The Building Blocks of VBA Programs

The While – Wend statement’s syntax. Square brackets, [ ], indicate optional items.

The While – Wend Statement | VBA Iteration Statements

The Do – Loop Until statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.

The Do – Loop Until Statement | VBA Iteration Statements

The Do – Loop While statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.

The Do – Loop While Statement | VBA Iteration Statements

The Do Until – Loop statement’s syntax. Square brackets, [ ], indicate optional items.

The Do Until – Loop Statement | VBA Iteration Statements

  • Trending Articles
  • The Peoples' Favorites

assignment operator image

VBA Editor Customization: Menus, Toolbars and Toolbox

White and blue eye state in front of a building - Photo by Matthew T Rader on Unsplash

Getting Started with the Watch Window

Grey and brown local sign - Photo by Priscilla Du Preez on Unsplash

Getting Started with the Locals Window

assignment operator image

VBA Program Identifiers: Data Type, Scope, and Lifetime

Photo by Robert Ruggiero on Unsplash

Learn Excel VBA? Top Reasons Why You Should

Related articles.

assignment operator image

VBA programs are nothing but a sequence of VBA statements. They are the cornerstone of VBA programs. Here, we explore these vital elements of VBA coding.

The While – Wend statement’s syntax. Square brackets, [ ], indicate optional items.

VBA programs often include repetitive execution paths. The While – Wend statement enables such loops. Here, we explore this crucial code construct in-depth.

The Do – Loop Until statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.

VBA programs often include repetitive execution paths. The Do – Loop Until statement enables such loops. Here, we explore this vital code construct in-depth.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

We respect your privacy and take protecting it seriously

Examples logo

Using assignment operators to produce a value

Assignment operators in Java are fundamental tools for modifying values stored in variables. They enable efficient updates to variable states, which is crucial for controlling program flow and managing data. These operators include the basic assignment (‘ = ‘), as well as compound forms like ‘ += ‘, ‘ -= ‘, ‘ *= ‘,’ /= ‘, and ‘ %= ‘ which combine arithmetic operations with assignment. Mastery of these operators is essential for writing concise and effective code, especially in contexts like loops, conditionals, and data manipulation tasks found in the AP Computer Science A exam.

Learning Objectives

When studying the use of assignment operators to produce a value for the AP Computer Science A exam, focus on understanding the syntax and application of each operator, including ‘ = ‘, ‘ += ‘, ‘ -= ‘, ‘ *= ‘, ‘ /= ‘, and ‘ %= ‘. Learn how to efficiently update variable values, manage state changes within loops, and handle conditional logic operations. Practice implementing these operators in coding problems to enhance computational thinking and problem-solving skills, ensuring you can apply these concepts effectively in both straightforward and complex programming scenarios.

Using Assignment Operators to Produce a Value

Using Assignment Operators to Produce a Value

Assignment operators in Java, which is the programming language typically used in the AP Computer Science A exam, are crucial for manipulating and storing values in variables. Understanding how these operators work is essential for effective programming and problem-solving in the exam.

1. Basic Assignment Operator

‘ = ‘ (Equal Sign) : This is the most common assignment operator. It assigns the value on the right to the variable on the left.

2. Arithmetic Assignment Operators

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

‘ += ‘ (Add and Assign) : Adds the right operand to the left operand and assigns the result to the left operand.

‘ -= ‘ (Subtract and Assign) : Subtracts the right operand from the left operand and assigns the result to the left operand.

‘ *= ‘ (Multiply and Assign) : Multiplies the right operand with the left operand and assigns the result to the left operand.

‘ /= ‘ (Divide and Assign) : Divides the left operand by the right operand and assigns the result to the left operand.

‘ %= ‘ (Modulus and Assign) : Computes the remainder of dividing the left operand by the right operand and assigns the result to the left operand.

3. Increment and Decrement

These are unary operators that increase or decrease the value of a variable by one. They are particularly useful in loops and conditional operations.

‘ ++ ‘ (Increment) : Increases the value of the variable by one.

‘ — ‘ (Decrement) : Decreases the value of the variable by one.

Impact of Assignment Operators on Variable Scope and Lifetime

Impact of Assignment Operators on Variable Scope and Lifetime

Assignment operators not only manage values within variables but also influence variable scope and lifetime—key concepts in Java that can significantly affect program behavior and memory management.

Variable Scope

Scope of a Variable : Refers to the region of the program where a variable is accessible. Assignment operators are often used within this scope to manipulate the variable’s value.

Here, ‘ a ‘ is only accessible within ‘ exampleMethod ‘, and any assignments or modifications to ‘ a ‘ are confined to this method.

Variable Lifetime

Lifetime of a Variable : Pertains to the duration for which a variable holds allocated memory. In Java, local variables have a lifetime that extends as long as the block of code in which they are defined is executing.

Important Concepts to Understand

Important Concepts to Understand

  • Evaluation Order : Java evaluates the right-hand side of the assignment first and then assigns it to the left-hand side. This is crucial in understanding expressions like a = b = c = 5; where c is first assigned 5, then b is assigned the value of c, and finally a is assigned the value of b.
  • Compound Assignment Efficiency : Compound operators like += are not only shorthand but can also be more efficient in some programming contexts as they can reduce the amount of code and potential errors by modifying the original variable directly.
  • Type Compatibility : When using assignment operators, ensure that the data types on both sides of the operator are compatible or have been explicitly cast to match. For example, assigning a double to an int without casting will cause a compilation error.

Example 1: Incrementing a Counter

In this example, a counter variable ‘ count ‘ is incremented by 1 using the ‘ += ‘ operator. Initially set at 0, the ‘ count ‘ variable is updated to reflect each iteration in a loop or as an event count.

Example 2: Calculating Total Cost

Here, a variable ‘ totalCost ‘ is used to accumulate costs from different items. As each item’s cost is computed or retrieved, it is added to ‘ totalCost ‘ using the ‘ += ‘ operator, making the code more readable and concise.

Example 3: Decrementing Stock Quantity

In inventory management, when an item is sold, the stock quantity needs to be decremented. The -= operator is used to subtract the sold quantity from the ‘ stockQuantity ‘ variable.

Example 4: Applying Discount

To apply a discount percentage to a price, the ‘ *= ‘ operator can be used to adjust the original price by a discount factor. In this case, applying a 20% discount to ‘ originalPrice ‘ would be accomplished as follows:

Example 5: Normalizing a Value

When normalizing a value, such as scaling a score to be out of 100, the ‘ *= ‘ operator is handy. If ‘ score ‘ is out of 50, it can be normalized by multiplying by 2.

Multiple Choice Questions

What is the value of ‘ x ‘ after the following code segment is executed?

A) 5 B) 10 C) 15 D) 20

Correct Answer: C) 15

Explanation: The ‘ += ‘ operator adds the right-hand operand to the left-hand operand and assigns the result back to the left-hand operand. In this case, ‘ 5 ‘ is added to ‘ 10 ‘, and the result, ‘ 15 ‘, is stored back in ‘ x ‘.

Consider the following code:

What is the final value of a?

A) 0 B) 5 C) 10 D) 50

Correct Answer: D) 50

Explanation: The ‘ *= ‘ operator multiplies the operand on the left by the operand on the right and assigns the result to the left operand. Here, ‘ a ‘ is multiplied by ‘ b ‘ (5 * 10), resulting in ‘ 50 ‘, which is then assigned back to ‘ a ‘.

Which of the following statements correctly decreases the value of ‘ x ‘ by ‘ 2 ‘ and stores the new value in ‘ x ‘?

A) ‘ x =- 2 ‘; B) ‘ x -= 2 ‘; C) ‘ x = x – 2 ‘; D) Both B and C are correct.

Correct Answer: D) Both B and C are correct.

Explanation:

  • Option B uses the ‘ -= ‘ operator, which subtracts the right-hand side value from the left-hand side value and assigns the result back to the left-hand side. ‘ x -= 2 ‘ is equivalent to ‘ x = x – 2 ‘.
  • Option C directly uses the assignment operator ‘ = ‘ with the expression ‘ x – 2 ‘, achieving the same result.
  • Option A is incorrect because ‘ x =- 2 ‘; would be parsed as ‘ x = -2 ‘, setting ‘ x ‘ to ‘ -2 ‘ rather than decreasing its current value by 2.

These questions cover basic to intermediate uses of assignment operators, helping to ensure that students understand both the syntax and the semantics of these operators.

Assignment Operators in C

C++ Course: Learn the Essentials

Operators are a fundamental part of all the computations that computers perform. Today we will learn about one of them known as Assignment Operators in C. Assignment Operators are used to assign values to variables. The most common assignment operator is = . Assignment Operators are Binary Operators.

Types of Assignment Operators in C

LHS and RHS Operands

Here is a list of the assignment operators that you can find in the C language:

  • basic assignment ( = )
  • subtraction assignment ( -= )
  • addition assignment ( += )
  • division assignment ( /= )
  • multiplication assignment ( *= )
  • modulo assignment ( %= )
  • bitwise XOR assignment ( ^= )
  • bitwise OR assignment ( |= )
  • bitwise AND assignment ( &= )
  • bitwise right shift assignment ( >>= )
  • bitwise left shift assignment ( <<= )

Working of Assignment Operators in C

This is the complete list of all assignment operators in C. To read the meaning of operator please keep in mind the above example.

OperatorMeaning Of OperatorExampleSame as
=Simple assignment operatorx=yx=y
+=Add left operand to right operand then assign result to left operandx+=yx=x+y
-=subtract right operand from left operand then assign result to left operandx-=yx=x-y
*=multiply left operand with right operand then assign result to left operandx*=yx=x*y
/=divide left operand with right operand then assign result to left operandx/=yx=x/y
%=take modulus left operand with right operand then assigned result in left operandx%=yx=x%y
<<=Left Shift Assignment Operator means the left operand is left shifted by right operand value and assigned value to left operandx<<=yx=x<<y
>>=Right shift Assignment Operator means the left operand is right shifted by right operand value and assigned value to left operandx>>=yx=x>>y
&=Bitwise AND Assignment Operator means does AND on every bit of left operand and right operand and assigned value to left operandx&=yx=x&y
|=Bitwise inclusive OR Assignment Operator means does OR on every bit of left operand and right operand and assigned value to left operandx|=yx=x|y
^=Bitwise exclusive OR Assignment Operator means does XOR on every bit of left operand and right operand and assigned value to left operandx^=yx=x^y

Example for Assignment Operators in C

Basic assignment ( = ) :

Subtraction assignment ( -= ) :

Addition assignment ( += ) :

Division assignment ( /= ) :

Multiplication assignment ( *= ) :

Modulo assignment ( %= ) :

Bitwise XOR assignment ( ^= ) :

Bitwise OR assignment ( |= ) :

Bitwise AND assignment ( &= ) :

Bitwise right shift assignment ( >>= ) :

Bitwise left shift assignment ( <<= ) :

This is the detailed explanation of all the assignment operators in C that we have. Hopefully, This is clear to you.

Practice Problems on Assignment Operators in C

1. what will be the value of a after the following code is executed.

A) 10 B) 11 C) 12 D) 15

Answer – C. 12 Explanation: a starts at 10, increases by 5 to 15, then decreases by 3 to 12. So, a is 12.

2. After executing the following code, what is the value of num ?

A) 4 B) 8 C) 16 D) 32

Answer: C) 16 Explanation: After right-shifting 8 (binary 1000) by one and then left-shifting the result by two, the value becomes 16 (binary 10000).

Q. How does the /= operator function? Is it a combination of two other operators?

A. The /= operator is a compound assignment operator in C++. It divides the left operand by the right operand and assigns the result to the left operand. It is equivalent to using the / operator and then the = operator separately.

Q. What is the most basic operator among all the assignment operators available in the C language?

A. The most basic assignment operator in the C language is the simple = operator, which is used for assigning a value to a variable.

  • Assignment operators are used to assign the result of an expression to a variable.
  • There are two types of assignment operators in C. Simple assignment operator and compound assignment operator.
  • Compound Assignment operators are easy to use and the left operand of expression needs not to write again and again.
  • They work the same way in C++ as in C.
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

Assignment operator and deep copy

I'm learning C++ and there is something I don't get about assignment operators. As far as I understand, they are supposed to provide deep copies of object. Here is an example

Output is 22 but I expected '21'. What is the obvious thing I am missing here?

znat's user avatar

  • 3 There's no assignment going on, and you shouldn't need any pointers like that to create objects. Your assignment operator basically does what the default one does, and your constructor would be better off with a constructor initialization list. –  chris Commented May 25, 2013 at 18:53
  • 1 A copy assignment can do either a deep copy or a shallow copy. It all depends on the requirements of the class and you shouldn't assume one or the other is always supposed to happen. –  Captain Obvlious Commented May 25, 2013 at 18:55
  • 1 A copy always copies the value of an object. So the question is, what is the value of the object? For a string, or a vector, that is it's contents, thus it acts like a deep copy. A pointer however, the value is the address of the pointee, so that act like a shallow copy. –  Mooing Duck Commented May 25, 2013 at 19:09

Short answer

As Captain Obvlious suggests, call your assignment operator with

"Longer" answer

With this line

You are just making an assignment between pointers, so you are declaring a pointer to Test called t2 which will hold the same address held by the pointer t1 .

When you modify the object using t1 , you are actually changing the same object that also t2 is pointing to (this explains the output).

If you want to copy t1 , you should use one of the following two ways:

Create a new Test object and copy construct it with t1 :

This code will call the copy constructor of the class Test :

Use the assignment operator on an existing Test object:

As a general rule of thumb, when you need to define an operator= , then you probably need to define also a copy constructor and a destructor .

See the Rule of Three for more details.

Vincenzo Pii's user avatar

  • This. No copying of any kind is taking place, the assignment operator isn't getting called, etc. etc. etc. –  Carl Norum Commented May 25, 2013 at 18:56
  • 2 How about Test t2 = *t1 as a solution for the OP ;) –  Captain Obvlious Commented May 25, 2013 at 19:00
  • @CaptainObvlious will add that as a short answer :) –  Vincenzo Pii Commented May 25, 2013 at 19:04

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged c++ or ask your own question .

  • The Overflow Blog
  • Mobile Observability: monitoring performance through cracked screens, old...
  • Featured on Meta
  • Announcing a change to the data-dump process
  • Bringing clarity to status tag usage on meta sites
  • What does a new user need in a homepage experience on Stack Overflow?
  • Staging Ground Reviewer Motivation
  • Feedback requested: How do you use tag hover descriptions for curating and do...

Hot Network Questions

  • Does Gravity Well work with Blade Ward?
  • Can you give me an example of an implicit use of Godel's Completeness Theorem, say for example in group theory?
  • using chapter style in latex (book)
  • Held Action Sneak attack after action surge
  • Fast algorithm to obtain an orthogonal vector to a set of vectors
  • Journal keeps messing with my proof
  • Strong Completenss vs Finitely Strong Completenss
  • Accidentally Removed the Shader Editor: How to Restore It?
  • Need to know compatible derailleur to my Shimano RD-M3000 Acera 9 Speed
  • If Starliner returns safely on autopilot, can this still prove that it's safe? Could it be launched back up to the ISS again to complete its mission?
  • What unique phenomena would be observed in a system around a hypervelocity star?
  • Best way to explain the thinking steps from x^2 = 9 to x=±3
  • Replacing aircon capacitor, using off brand same spec vs lower/higher spec from correct brand?
  • Why is GParted distributed as an ISO image? Is it to accommodate Linux needs as well as Windows needs?
  • How did Oswald Mosley escape treason charges?
  • Took a pic of old school friend in the 80s who is now quite famous and written a huge selling memoir. Pic has ben used in book without permission
  • How to define a function for Schmitt trigger?
  • Is 3 ohm resistance value of a PCB fuse reasonable?
  • World Building Knowledgebase - How to write good Military World Building
  • Reference for proof about a result concerning Sobolev spaces and exponential growth
  • Train smartcards in the UK - which cards can you use on which networks?
  • How to frame certain cells with tabular?
  • Lore reasons for being faithless
  • What is happening when a TV satellite stops broadcasting during an "eclipse"?

assignment operator image

  • C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Assignment Operators In C++

In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.

The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.

The value can be a literal or another variable of the same data type.

 

Compound Assignment Operators

In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:

  • Addition Assignment Operator ( += )
  • Subtraction Assignment Operator ( -= )
  • Multiplication Assignment Operator ( *= )
  • Division Assignment Operator ( /= )
  • Modulus Assignment Operator ( %= )
  • Bitwise AND Assignment Operator ( &= )
  • Bitwise OR Assignment Operator ( |= )
  • Bitwise XOR Assignment Operator ( ^= )
  • Left Shift Assignment Operator ( <<= )
  • Right Shift Assignment Operator ( >>= )

Lets see each of them in detail.

1. Addition Assignment Operator (+=)

In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.

This above expression is equivalent to the expression:

   

2. Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.

   

3. Multiplication Assignment Operator (*=)

In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.

 

4. Division Assignment Operator (/=)

The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

       

5. Modulus Assignment Operator (%=)

The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.

     

6. Bitwise AND Assignment Operator (&=)

This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.

   

7. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

8. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

9. Left Shift Assignment Operator (<<=)

The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.

10. Right Shift Assignment Operator (>>=)

The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.

Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.

Please Login to comment...

Similar reads.

  • Geeks Premier League
  • Geeks Premier League 2023
  • How to Delete Discord Servers: Step by Step Guide
  • Google increases YouTube Premium price in India: Check our the latest plans
  • California Lawmakers Pass Bill to Limit AI Replicas
  • Best 10 IPTV Service Providers in Germany
  • Content Improvement League 2024: From Good To A Great Article

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

21.12 — Overloading the assignment operator

cppreference.com

Assignment operators.

(C++20)
(C++20)
(C++11)
(C++20)
(C++17)
(C++11)
(C++11)
General topics
(C++11)
-
-expression
block


/
(C++11)
(C++11)
(C++11)
(C++20)
(C++20)
(C++11)

expression
pointer
specifier

specifier (C++11)
specifier (C++11)
(C++11)

(C++11)
(C++11)
(C++11)
General
(C++11)
(C++20)
(C++26)
(C++11)
(C++11)
-expression
-expression
-expression
(C++11)
(C++11)
(C++17)
(C++20)
    

Assignment operators modify the value of the object.

Operator name  Syntax  Prototype examples (for class T)
Inside class definition Outside class definition
simple assignment Yes T& T::operator =(const T2& b);
addition assignment Yes T& T::operator +=(const T2& b); T& operator +=(T& a, const T2& b);
subtraction assignment Yes T& T::operator -=(const T2& b); T& operator -=(T& a, const T2& b);
multiplication assignment Yes T& T::operator *=(const T2& b); T& operator *=(T& a, const T2& b);
division assignment Yes T& T::operator /=(const T2& b); T& operator /=(T& a, const T2& b);
remainder assignment Yes T& T::operator %=(const T2& b); T& operator %=(T& a, const T2& b);
bitwise AND assignment Yes T& T::operator &=(const T2& b); T& operator &=(T& a, const T2& b);
bitwise OR assignment Yes T& T::operator |=(const T2& b); T& operator |=(T& a, const T2& b);
bitwise XOR assignment Yes T& T::operator ^=(const T2& b); T& operator ^=(T& a, const T2& b);
bitwise left shift assignment Yes T& T::operator <<=(const T2& b); T& operator <<=(T& a, const T2& b);
bitwise right shift assignment Yes T& T::operator >>=(const T2& b); T& operator >>=(T& a, const T2& b);

this, and most also return *this so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void). can be any type including .
Definitions Assignment operator syntax Built-in simple assignment operator Assignment from an expression Assignment from a non-expression initializer clause Built-in compound assignment operator Example Defect reports See also

[ edit ] Definitions

Copy assignment replaces the contents of the object a with a copy of the contents of b ( b is not modified). For class types, this is performed in a special member function, described in copy assignment operator .

replaces the contents of the object a with the contents of b, avoiding copying if possible (b may be modified). For class types, this is performed in a special member function, described in .

(since C++11)

For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment .

Compound assignment replace the contents of the object a with the result of a binary operation between the previous value of a and the value of b .

[ edit ] Assignment operator syntax

The assignment expressions have the form

target-expr new-value (1)
target-expr op new-value (2)
target-expr - the expression to be assigned to
op - one of *=, /= %=, += -=, <<=, >>=, &=, ^=, |=
new-value - the expression (until C++11) (since C++11) to assign to the target
  • ↑ target-expr must have higher precedence than an assignment expression.
  • ↑ new-value cannot be a comma expression, because its precedence is lower.

If new-value is not an expression, the assignment expression will never match an overloaded compound assignment operator.

(since C++11)

[ edit ] Built-in simple assignment operator

For the built-in simple assignment, the object referred to by target-expr is modified by replacing its value with the result of new-value . target-expr must be a modifiable lvalue.

The result of a built-in simple assignment is an lvalue of the type of target-expr , referring to target-expr . If target-expr is a bit-field , the result is also a bit-field.

[ edit ] Assignment from an expression

If new-value is an expression, it is implicitly converted to the cv-unqualified type of target-expr . When target-expr is a bit-field that cannot represent the value of the expression, the resulting value of the bit-field is implementation-defined.

If target-expr and new-value identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same).

If the type of target-expr is volatile-qualified, the assignment is deprecated, unless the (possibly parenthesized) assignment expression is a or an .

(since C++20)

new-value is only allowed not to be an expression in following situations:

is of a , and new-value is empty or has only one element. In this case, given an invented variable t declared and initialized as T t = new-value , the meaning of x = new-value  is x = t. is of class type. In this case, new-value is passed as the argument to the assignment operator function selected by .   <double> z; z = {1, 2}; // meaning z.operator=({1, 2}) z += {1, 2}; // meaning z.operator+=({1, 2})   int a, b; a = b = {1}; // meaning a = b = 1; a = {1} = b; // syntax error
(since C++11)

In overload resolution against user-defined operators , for every type T , the following function signatures participate in overload resolution:

& operator=(T*&, T*);
volatile & operator=(T*volatile &, T*);

For every enumeration or pointer to member type T , optionally volatile-qualified, the following function signature participates in overload resolution:

operator=(T&, T);

For every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:

operator=(A1&, A2);

[ edit ] Built-in compound assignment operator

The behavior of every built-in compound-assignment expression target-expr   op   =   new-value is exactly the same as the behavior of the expression target-expr   =   target-expr   op   new-value , except that target-expr is evaluated only once.

The requirements on target-expr and new-value of built-in simple assignment operators also apply. Furthermore:

  • For + = and - = , the type of target-expr must be an arithmetic type or a pointer to a (possibly cv-qualified) completely-defined object type .
  • For all other compound assignment operators, the type of target-expr must be an arithmetic type.

In overload resolution against user-defined operators , for every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:

operator*=(A1&, A2);
operator/=(A1&, A2);
operator+=(A1&, A2);
operator-=(A1&, A2);

For every pair I1 and I2 , where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:

operator%=(I1&, I2);
operator<<=(I1&, I2);
operator>>=(I1&, I2);
operator&=(I1&, I2);
operator^=(I1&, I2);
operator|=(I1&, I2);

For every optionally cv-qualified object type T , the following function signatures participate in overload resolution:

& operator+=(T*&, );
& operator-=(T*&, );
volatile & operator+=(T*volatile &, );
volatile & operator-=(T*volatile &, );

[ edit ] Example

Possible output:

[ edit ] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
C++11 for assignments to class type objects, the right operand
could be an initializer list only when the assignment
is defined by a user-defined assignment operator
removed user-defined
assignment constraint
C++11 E1 = {E2} was equivalent to E1 = T(E2)
( is the type of ), this introduced a C-style cast
it is equivalent
to E1 = T{E2}
C++20 compound assignment operators for volatile
-qualified types were inconsistently deprecated
none of them
is deprecated
C++11 an assignment from a non-expression initializer clause
to a scalar value would perform direct-list-initialization
performs copy-list-
initialization instead
C++20 bitwise compound assignment operators for volatile types
were deprecated while being useful for some platforms
they are not
deprecated

[ edit ] See also

Operator precedence

Operator overloading

Common operators

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b
a <=> b

a[...]
*a
&a
a->b
a.b
a->*b
a.*b

function call
a(...)
comma
a, b
conditional
a ? b : c
Special operators

converts one type to another related type
converts within inheritance hierarchies
adds or removes -qualifiers
converts type to unrelated type
converts one type to another by a mix of , , and
creates objects with dynamic storage duration
destructs objects previously created by the new expression and releases obtained memory area
queries the size of a type
queries the size of a (since C++11)
queries the type information of a type
checks if an expression can throw an exception (since C++11)
queries alignment requirements of a type (since C++11)

for Assignment operators
  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 25 January 2024, at 23:41.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

logo

We operate data-centric assignment based on state-of-the-art technology, programming, and data science principle.

assignment operator image

IMAGES

  1. Assignment Operators

    assignment operator image

  2. WebQuest: Programming with Python

    assignment operator image

  3. Assignment Operators

    assignment operator image

  4. Python Tutorials: Assignment Operators In python

    assignment operator image

  5. Assignment Operator in C Programming

    assignment operator image

  6. Assignment operator

    assignment operator image

VIDEO

  1. Lecture 11.11

  2. Week 2

  3. ASSIGNMENT OPERATOR IN C LANGUAGE

  4. python assignment operator

  5. Assignment operator #operator #operator in python #python #code #datascience #pythonfullcourse

  6. DotNet operators program

COMMENTS

  1. Assignment Operator images

    Find Assignment Operator stock images in HD and millions of other royalty-free stock photos, illustrations and vectors in the Shutterstock collection. Thousands of new, high-quality pictures added every day.

  2. Assignment operator clipart images in SVG, PNG, GIF

    Explore Assignment operator clipart images and illustrations for free in 30+ styles. Download Assignment operator clipart illustrations in PNG, SVG, GIF, JSON, AEP.

  3. Assignment operator Icons, Logos, Symbols

    Get free Assignment operator icons in iOS, Material, Windows and other design styles for web, mobile, and graphic design projects. These free images are pixel perfect to fit your design and available in both PNG and vector. Download icons in all formats or edit them for your designs. Also, be sure to check out new icons and popular icons.

  4. 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 ...

  5. JavaScript Assignment

    Use the correct assignment operator that will result in x being 15 (same as x = x + y ). Start the Exercise. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.

  6. Assignment Operators in C

    1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators. This operator first adds the current value of the variable on left to the value on the right and ...

  7. Assignment Operators Programs images

    Find Assignment Operators Programs stock images in HD and millions of other royalty-free stock photos, illustrations and vectors in the Shutterstock collection. Thousands of new, high-quality pictures added every day. ... 5 assignment operators programs stock photos, vectors, and illustrations are available royalty-free for download.

  8. 20,800 Assignment Operator Icons

    20,800 Assignment Operator Icons. design styles for web or mobile (iOS and Android) design, marketing, or developer projects. These royalty-free high-quality Assignment Operator Vector Icons are available in SVG, PNG, EPS, ICO, ICNS, AI, or PDF and are available as individual or icon packs.. You can also customise them to match your brand and ...

  9. Assignment Operator Icons

    Browse Assignment Operator Icons. Icons (2) Photos (322) assignment coding computer science operators programming. Find 2 Assignment Operator images and millions more royalty free PNG & vector images from the world's most diverse collection of free icons.

  10. c++

    A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&. So for example: struct X {. int a; // an assignment operator which is not a copy assignment operator. X &operator=(int rhs) { a = rhs; return *this; }

  11. Java Assignment Operators with Examples

    Note: The compound assignment operator in Java performs implicit type casting. Let's consider a scenario where x is an int variable with a value of 5. int x = 5; If you want to add the double value 4.5 to the integer variable x and print its value, there are two methods to achieve this: Method 1: x = x + 4.5. Method 2: x += 4.5.

  12. PHP: Assignment

    Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions ... In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression ...

  13. Assignment Operators royalty-free images

    Assignment Operators royalty-free images. 304 assignment operators stock photos, vectors, and illustrations are available royalty-free for download. Young woman Jane at a meeting. Assignment of tasks, scram, meeting, job delegation. Manager works with computer and phone. 3d vector illustration.

  14. Python Assignment Operators

    Python Assignment Operators. Assignment operators are used to assign values to variables: Operator. Example. Same As. Try it. =. x = 5. x = 5.

  15. Assignment Operator in VBA

    The sample code also shows that, unlike in languages such as C, assignments cannot be chained in VBA. The results in the Immediate window (see the image below) demonstrate this.. Sample code showing the Assignment operator's usage.. Besides the assignment operator, there are several expression statements that also perform assignments. These include the Let, Set, Get, Put, Input #, Line Input ...

  16. Using assignment operators to produce a value

    1. Basic Assignment Operator ' = ' (Equal Sign): This is the most common assignment operator. It assigns the value on the right to the variable on the left. int x = 5; // Assigns 5 to variable x. 2. Arithmetic Assignment Operators. These operators perform an arithmetic operation on the variable and then assign the result back to the variable.

  17. Assignment Operators in C

    A) 4 B) 8 C) 16 D) 32. Answer: C) 16 Explanation: After right-shifting 8 (binary 1000) by one and then left-shifting the result by two, the value becomes 16 (binary 10000). FAQs. Q. How does the /= operator function? Is it a combination of two other operators? A. The /= operator is a compound assignment operator in C++. It divides the left operand by the right operand and assigns the result to ...

  18. c++

    Test t2(t1) This code will call the copy constructor of the class Test: Test::Test(const Test& t); Use the assignment operator on an existing Test object: Test t2; t2 = t1; As a general rule of thumb, when you need to define an operator=, then you probably need to define also a copy constructor and a destructor.

  19. Assignment Operators In C++

    In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way. Syntax. variable += value; This above expression is equivalent to the expression: variable = variable + value; Example.

  20. 21.12

    21.12 — Overloading the assignment operator. Alex July 22, 2024. The copy assignment operator (operator=) is used to copy values from one object to another already existing object. As of C++11, C++ also supports "Move assignment". We discuss move assignment in lesson 22.3 -- Move constructors and move assignment .

  21. Assignment operators

    Correct behavior. CWG 1527. C++11. for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator. removed user-defined assignment constraint. CWG 1538. C++11. E1 ={E2} was equivalent to E1 = T(E2) (T is the type of E1), this introduced a C-style cast.

  22. Assignment Operator

    The following operators allow assignment and array indexing: . = is the assignment operator, χ = γ copies the value of y into x. In this example, if γ is undefined, χ becomes undefined. The assignment operator can be used with all four built-in data types. . [] is the array index operator. Strings can be indexed using the array index operator.

  23. Home

    Assignment Operator We assign true value to your assignment! We operate data-centric assignment based on state-of-the-art technology, programming, and data science principle. ... Data Consulting. Data Preparation. Machine Learning Model. Deep Learning Model. Predictive Analysis. Image Analysis. Optimization and Automation. Implementation and ...