cppreference.com

Assignment operators.

Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.

[ edit ] Simple assignment

The simple assignment operator expressions have the form

Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs .

Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non-lvalue (so that expressions such as ( a = b ) = c are invalid).

rhs and lhs must satisfy one of the following:

  • both lhs and rhs have compatible struct or union type, or..
  • rhs must be implicitly convertible to lhs , which implies
  • both lhs and rhs have arithmetic types , in which case lhs may be volatile -qualified or atomic (since C11)
  • both lhs and rhs have pointer to compatible (ignoring qualifiers) types, or one of the pointers is a pointer to void, and the conversion would not add qualifiers to the pointed-to type. lhs may be volatile or restrict (since C99) -qualified or atomic (since C11) .
  • lhs is a (possibly qualified or atomic (since C11) ) pointer and rhs is a null pointer constant such as NULL or a nullptr_t value (since C23)

[ edit ] Notes

If rhs and lhs overlap in memory (e.g. they are members of the same union), the behavior is undefined unless the overlap is exact and the types are compatible .

Although arrays are not assignable, an array wrapped in a struct is assignable to another object of the same (or compatible) struct type.

The side effect of updating lhs is sequenced after the value computations, but not the side effects of lhs and rhs themselves and the evaluations of the operands are, as usual, unsequenced relative to each other (so the expressions such as i = ++ i ; are undefined)

Assignment strips extra range and precision from floating-point expressions (see FLT_EVAL_METHOD ).

In C++, assignment operators are lvalue expressions, not so in C.

[ edit ] Compound assignment

The compound assignment operator expressions have the form

The expression lhs @= rhs is exactly the same as lhs = lhs @ ( rhs ) , except that lhs is evaluated only once.

[ edit ] References

  • C17 standard (ISO/IEC 9899:2018):
  • 6.5.16 Assignment operators (p: 72-73)
  • C11 standard (ISO/IEC 9899:2011):
  • 6.5.16 Assignment operators (p: 101-104)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.5.16 Assignment operators (p: 91-93)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.3.16 Assignment operators

[ edit ] See Also

Operator precedence

[ edit ] See also

  • 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 19 August 2022, at 08:36.
  • This page has been accessed 54,567 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

CProgramming Tutorial

  • C Programming Tutorial
  • C - Overview
  • C - Features
  • C - History
  • C - Environment Setup
  • C - Program Structure
  • C - Hello World
  • C - Compilation Process
  • C - Comments
  • C - Keywords
  • C - Identifiers
  • C - User Input
  • C - Basic Syntax
  • C - Data Types
  • C - Variables
  • C - Integer Promotions
  • C - Type Conversion
  • C - Constants
  • C - Literals
  • C - Escape sequences
  • C - Format Specifiers
  • C - Storage Classes
  • C - Operators
  • C - Decision Making
  • C - While loop
  • C - Functions
  • C - Scope Rules
  • C - Pointers
  • C - Strings
  • C - Structures
  • C - Bit Fields
  • C - Typedef
  • C - Input & Output
  • C - File I/O
  • C - Preprocessors
  • C - Header Files
  • C - Type Casting
  • C - Error Handling
  • C - Recursion
  • C - Variable Arguments
  • C - Memory Management
  • C - Command Line Arguments
  • C Programming Resources
  • C - Questions & Answers
  • C - Quick Guide
  • C - Useful Resources
  • C - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Assignment Operators in C

In C, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable or an expression. The value to be assigned forms the right hand operand, whereas the variable to be assigned should be the operand to the left of = symbol, which is defined as a simple assignment operator in C. In addition, C has several augmented assignment operators.

The following table lists the assignment operators supported by the C language −

Simple assignment operator (=)

The = operator is the most frequently used operator in C. As per ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed. You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

You can use a literal, another variable or an expression in the assignment statement.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

Augmented assignment operators

In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.

For example, the expression a+=b has the same effect of performing a+b first and then assigning the result back to the variable a.

Similarly, the expression a<<=b has the same effect of performing a<<b first and then assigning the result back to the variable a.

Here is a C program that demonstrates the use of assignment operators in C:

When you compile and execute the above program, it produces the following result −

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

4.6: Assignment Operator

  • Last updated
  • Save as PDF
  • Page ID 29038

  • Patrick McClanahan
  • San Joaquin Delta College

Assignment Operator

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

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.

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 assigned to the variable named: total_cousins.

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.

As we have seen, assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error. Different types of assignment operators are shown below:

  • “=” : This is the simplest assignment operator, which was discussed above. This operator is used to assign the value on the right to the variable on the left. For example: a = 10; b = 20; ch = 'y';

If initially the value 5 is stored in the variable a,  then:  (a += 6) is equal to 11.  (the same as: a = a + 6)

If initially value 8 is stored in the variable a, then (a -= 6) is equal to  2. (the same as a = a - 6)

If initially value 5 is stored in the variable a,, then (a *= 6) is equal to 30. (the same as a = a * 6)

If initially value 6 is stored in the variable a, then (a /= 2) is equal to 3. (the same as a = a / 2)

Below example illustrates the various Assignment Operators:

Definitions

 Adapted from:  "Assignment Operator"  by  Kenneth Leroy Busbee , (Download for free at http://cnx.org/contents/[email protected] ) is licensed under  CC BY 4.0

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Assignment operators (C# reference)

  • 11 contributors

The assignment operator = assigns the value of its right-hand operand to a variable, a property , or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand. The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.

The assignment operator = is right-associative, that is, an expression of the form

is evaluated as

The following example demonstrates the usage of the assignment operator with a local variable, a property, and an indexer element as its left-hand operand:

The left-hand operand of an assignment receives the value of the right-hand operand. When the operands are of value types , assignment copies the contents of the right-hand operand. When the operands are of reference types , assignment copies the reference to the object.

This is called value assignment : the value is assigned.

ref assignment

Ref assignment = ref makes its left-hand operand an alias to the right-hand operand, as the following example demonstrates:

In the preceding example, the local reference variable arrayElement is initialized as an alias to the first array element. Then, it's ref reassigned to refer to the last array element. As it's an alias, when you update its value with an ordinary assignment operator = , the corresponding array element is also updated.

The left-hand operand of ref assignment can be a local reference variable , a ref field , and a ref , out , or in method parameter. Both operands must be of the same type.

Compound assignment

For a binary operator op , a compound assignment expression of the form

is equivalent to

except that x is only evaluated once.

Compound assignment is supported by arithmetic , Boolean logical , and bitwise logical and shift operators.

Null-coalescing assignment

You can use the null-coalescing assignment operator ??= to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null . For more information, see the ?? and ??= operators article.

Operator overloadability

A user-defined type can't overload the assignment operator. However, a user-defined type can define an implicit conversion to another type. That way, the value of a user-defined type can be assigned to a variable, a property, or an indexer element of another type. For more information, see User-defined conversion operators .

A user-defined type can't explicitly overload a compound assignment operator. However, if a user-defined type overloads a binary operator op , the op= operator, if it exists, is also implicitly overloaded.

C# language specification

For more information, see the Assignment operators section of the C# language specification .

  • C# reference
  • C# operators and expressions
  • ref keyword
  • Use compound assignment (style rules IDE0054 and IDE0074)

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

Additional resources

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Now that you've learned how to declare and initialize variables, you probably want to know how to do something with them. Learning the operators of the Java programming language is a good place to start. Operators are special symbols that perform specific operations on one, two, or three operands , and then return a result.

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator " = " is far more common than the unsigned right shift operator " >>> ". With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common. Each discussion is accompanied by sample code that you can compile and run. Studying its output will help reinforce what you've just learned.

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.

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:

Related Pages

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

Learn Java practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn java interactively, java introduction.

  • Java Hello World
  • Java JVM, JRE and JDK
  • Java Variables and Literals
  • Java Data Types

Java Operators

  • Java Input and Output
  • Java Expressions & Blocks
  • Java Comment

Java Flow Control

  • Java if...else
  • Java switch Statement
  • Java for Loop
  • Java for-each Loop
  • Java while Loop
  • Java break Statement
  • Java continue Statement
  • Java Arrays
  • Multidimensional Array
  • Java Copy Array

Java OOP (I)

  • Java Class and Objects
  • Java Methods
  • Java Method Overloading
  • Java Constructor
  • Java Strings
  • Java Access Modifiers
  • Java this keyword
  • Java final keyword
  • Java Recursion

Java instanceof Operator

Java OOP (II)

  • Java Inheritance
  • Java Method Overriding
  • Java super Keyword
  • Abstract Class & Method
  • Java Interfaces
  • Java Polymorphism
  • Java Encapsulation

Java OOP (III)

  • Nested & Inner Class
  • Java Static Class
  • Java Anonymous Class
  • Java Singleton
  • Java enum Class
  • Java enum Constructor
  • Java enum String
  • Java Reflection
  • Java Exception Handling
  • Java Exceptions
  • Java try...catch
  • Java throw and throws
  • Java catch Multiple Exceptions
  • Java try-with-resources
  • Java Annotations
  • Java Annotation Types
  • Java Logging
  • Java Assertions
  • Java Collections Framework
  • Java Collection Interface
  • Java List Interface
  • Java ArrayList
  • Java Vector
  • Java Queue Interface
  • Java PriorityQueue
  • Java Deque Interface
  • Java LinkedList
  • Java ArrayDeque
  • Java BlockingQueue Interface
  • Java ArrayBlockingQueue
  • Java LinkedBlockingQueue
  • Java Map Interface
  • Java HashMap
  • Java LinkedHashMap
  • Java WeakHashMap
  • Java EnumMap
  • Java SortedMap Interface
  • Java NavigableMap Interface
  • Java TreeMap
  • Java ConcurrentMap Interface
  • Java ConcurrentHashMap
  • Java Set Interface
  • Java HashSet
  • Java EnumSet
  • Java LinkedhashSet
  • Java SortedSet Interface
  • Java NavigableSet Interface
  • Java TreeSet
  • Java Algorithms
  • Java Iterator
  • Java ListIterator
  • Java I/O Streams
  • Java InputStream
  • Java OutputStream
  • Java FileInputStream
  • Java FileOutputStream
  • Java ByteArrayInputStream
  • Java ByteArrayOutputStream
  • Java ObjectInputStream
  • Java ObjectOutputStream
  • Java BufferedInputStream
  • Java BufferedOutputStream
  • Java PrintStream

Java Reader/Writer

  • Java Reader
  • Java Writer
  • Java InputStreamReader
  • Java OutputStreamWriter
  • Java FileReader
  • Java FileWriter
  • Java BufferedReader
  • Java BufferedWriter
  • Java StringReader
  • Java StringWriter
  • Java PrintWriter

Additional Topics

  • Java Scanner Class
  • Java Type Casting
  • Java autoboxing and unboxing
  • Java Lambda Expression
  • Java Generics
  • Java File Class
  • Java Wrapper Class
  • Java Command Line Arguments

Java Tutorials

Java Operator Precedence

Java Ternary Operator

Java Bitwise and Shift Operators

  • Java if...else Statement
  • Java while and do...while Loop

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.

Operators in Java can be classified into 5 types:

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Unary Operators
  • Bitwise Operators

1. Java Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data. For example,

Here, the + operator is used to add two variables a and b . Similarly, there are various other arithmetic operators in Java.

Example 1: Arithmetic Operators

In the above example, we have used + , - , and * operators to compute addition, subtraction, and multiplication operations.

/ Division Operator

Note the operation, a / b in our program. The / operator is the division operator.

If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if one of the operands is a floating-point number, we will get the result will also be in floating-point.

% Modulo Operator

The modulo operator % computes the remainder. When a = 7 is divided by b = 4 , the remainder is 3 .

Note : The % operator is mainly used with integers.

2. Java Assignment Operators

Assignment operators are used in Java to assign values to variables. For example,

Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is assigned to the variable age .

Let's see some more assignment operators available in Java.

Example 2: Assignment Operators

3. java relational operators.

Relational operators are used to check the relationship between two operands. For example,

Here, < operator is the relational operator. It checks if a is less than b or not.

It returns either true or false .

Example 3: Relational Operators

Note : Relational operators are used in decision making and loops.

4. Java Logical Operators

Logical operators are used to check whether an expression is true or false . They are used in decision making.

Example 4: Logical Operators

Working of Program

  • (5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true .
  • (5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false .
  • (5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true .
  • (5 > 3) || (8 < 5) returns true because the expression (5 > 3) is true .
  • (5 < 3) || (8 < 5) returns false because both (5 < 3) and (8 < 5) are false .
  • !(5 == 3) returns true because 5 == 3 is false .
  • !(5 > 3) returns false because 5 > 3 is true .

5. Java Unary Operators

Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1 . That is, ++5 will return 6 .

Different types of unary operators are:

  • Increment and Decrement Operators

Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1 , while -- decrease it by 1 . For example,

Here, the value of num gets increased to 6 from its initial value of 5 .

Example 5: Increment and Decrement Operators

In the above program, we have used the ++ and -- operator as prefixes (++a, --b) . We can also use these operators as postfix (a++, b++) .

There is a slight difference when these operators are used as prefix versus when they are used as a postfix.

To learn more about these operators, visit increment and decrement operators .

6. Java Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits. For example,

Here, ~ is a bitwise operator. It inverts the value of each bit ( 0 to 1 and 1 to 0 ).

The various bitwise operators present in Java are:

These operators are not generally used in Java. To learn more, visit Java Bitwise and Bit Shift Operators .

Other operators

Besides these operators, there are other additional operators in Java.

The instanceof operator checks whether an object is an instanceof a particular class. For example,

Here, str is an instance of the String class. Hence, the instanceof operator returns true . To learn more, visit Java instanceof .

The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example,

Here's how it works.

  • If the Expression is true , expression1 is assigned to the variable .
  • If the Expression is false , expression2 is assigned to the variable .

Let's see an example of a ternary operator.

In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the Java ternary operator .

Now that you know about Java operators, it's time to know about the order in which operators are evaluated. To learn more, visit Java Operator Precedence .

Table of Contents

  • Introduction
  • Java Arithmetic Operators
  • Java Assignment Operators
  • Java Relational Operators
  • Java Logical Operators
  • Java Unary Operators
  • Java Bitwise Operators

Sorry about that.

Related Tutorials

Java Tutorial

  • Trending Now
  • Data Structures & Algorithms
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial
  • Web Development
  • Web Browser

Related Articles

  • Program to find the side length of a square given its area
  • Morse Code Tutorial
  • Program to calculate the length of the diagonal in a rectangle
  • Variables in Programming
  • Program to find the number of days in a given month
  • Program to Calculate Income Tax
  • Program to count number of days between two given months
  • Program to reverse order of words in a sentence
  • Difference between For Loop and While Loop in Programming
  • Print Happy New Year 2024
  • Program to reverse a word
  • Program to find the perimeter of a square given its area
  • Program to count the number of consonants in a word
  • Conditional Statements in Programming | Definition, Types, Best Practices
  • Error Handling in Programming
  • Write a Program to Print 2024 Calendar
  • Program to reverse a sentence
  • What is Programming? A Handbook for Beginners
  • Difference between While Loop and Do-While Loop in Programming

What are Operators in Programming?

Operators in programming are essential symbols that perform operations on variables and values , enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types.

Operators-in-Programming

Operators in Programming

Table of Content

  • Types of Operators in Programming
  • Operator Precedence and Associativity in Programming
  • Frequently Asked Questions (FAQs) related to Programming Operators

Operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables , constants , or values , and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.

Types of Operators in Programming:

Here are some common types of operators:

  • Arithmetic Operators: Perform basic arithmetic operations on numeric values. Examples: + (addition), – (subtraction), * (multiplication), / (division), % (modulo).
  • Comparison Operators: Compare two values and return a Boolean result (true or false). Examples: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to).
  • Logical Operators: Perform logical operations on Boolean values. Examples: && (logical AND), || (logical OR), ! (logical NOT).
  • Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=.
  • Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).
  • Bitwise Operators: Perform operations on individual bits of binary representations of numbers. Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).

These operators provide the building blocks for creating complex expressions and performing diverse operations in programming languages. Understanding their usage is crucial for writing efficient and expressive code.

Arithmetic Operators in Programming:

Arithmetic operators in programming are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:

Comparison Operators in Programming:

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:

These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.

Logical Operators in Programming:

Logical operators in programming are used to perform logical operations on Boolean values . These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:

These logical operators are frequently used in conditional statements (if, else if, else), loops, and decision-making constructs to create complex conditions based on multiple Boolean expressions. Understanding how to use logical operators is essential for designing effective and readable control flow in programming.

Assignment Operators in Programming:

Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:

Assignment operators are fundamental for updating variable values, especially in loops and mathematical computations, contributing to the dynamic nature of programming. Understanding how to use assignment operators is essential for effective variable manipulation in a program.

Increment and Decrement Operators in Programming:

Increment and decrement operators in programming are used to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:

These operators are frequently employed in loops, especially for iterating through arrays or performing repetitive tasks. Their concise syntax enhances code readability and expressiveness.

Bitwise Operators in Programming:

Bitwise operators in programming perform operations at the bit level , manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:

Bitwise operators are useful in scenarios where direct manipulation of binary representations or specific bit patterns is required, such as optimizing certain algorithms or working with hardware interfaces. Understanding bitwise operations is essential for low-level programming tasks.

Operator Precedence and Associativity in Programming :

Operator Precedence is a rule that determines the order in which operators are evaluated in an expression. It defines which operators take precedence over others when they are combined in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence and explicitly specify the order of evaluation.

Operator Associativity is a rule that determines the grouping of operators with the same precedence in an expression when they appear consecutively. It specifies the direction in which operators of equal precedence are evaluated. The two common associativities are:

  • Left to Right (Left-Associative): Operators with left associativity are evaluated from left to right. For example, in the expression a + b + c , the addition operators have left associativity, so the expression is equivalent to (a + b) + c .
  • Right to Left (Right-Associative): Operators with right associativity are evaluated from right to left. For example, in the expression a = b = c , the assignment operator = has right associativity, so the expression is equivalent to a = (b = c) .

Frequently Asked Questions (FAQs) related to Programming Operators :

Here are some frequently asked questions (FAQs) related to programming operators:

Q1: What are operators in programming?

A: Operators in programming are symbols that represent computations or actions to be performed on operands. They can manipulate data, perform calculations, and facilitate various operations in a program.

Q2: How are operators categorized?

A: Operators are categorized based on their functionality. Common categories include arithmetic operators (for mathematical operations), assignment operators (for assigning values), comparison operators (for comparing values), logical operators (for logical operations), and bitwise operators (for manipulating individual bits).

Q3: What is the difference between unary and binary operators?

A: Unary operators operate on a single operand, while binary operators operate on two operands. For example, the unary minus -x negates the value of x , while the binary plus a + b adds the values of a and b .

Q4: Can operators be overloaded in programming languages?

A: Yes, some programming languages support operator overloading, allowing developers to define custom behaviors for operators when applied to user-defined types. This is commonly seen in languages like C++.

Q5: How do logical AND ( && ) and logical OR ( || ) operators work?

A: The logical AND ( && ) operator returns true if both of its operands are true. The logical OR ( || ) operator returns true if at least one of its operands is true. These operators are often used in conditional statements and expressions.

Q6: What is the purpose of the ternary operator ( ?: )?

A: The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false. It is often used for concise conditional assignments.

Q7: How does the bitwise XOR operator ( ^ ) work?

A: The bitwise XOR ( ^ ) operator performs an exclusive OR operation on individual bits. It returns 1 for bits that are different and 0 for bits that are the same. This operator is commonly used in bit manipulation tasks.

Q8: What is the difference between = and == ?

A: The = operator is an assignment operator used to assign a value to a variable. The == operator is a comparison operator used to check if two values are equal. It is important not to confuse the two, as = is used for assignment, and == is used for comparison.

Q9: How do increment ( ++ ) and decrement ( -- ) operators work?

A: The increment ( ++ ) operator adds 1 to the value of a variable, while the decrement ( -- ) operator subtracts 1. These operators can be used as prefix ( ++x ) or postfix ( x++ ), affecting the order of the increment or decrement operation.

Q10: Are operators language-specific?

A: While many operators are common across programming languages, some languages may introduce unique operators or have variations in their behavior. However, the fundamental concepts of arithmetic, logical, and bitwise operations are prevalent across various languages.

Q11: What is the modulus operator ( % ) used for?

A: The modulus operator ( % ) returns the remainder when one number is divided by another. It is often used to check divisibility or to cycle through a sequence of values. For example, a % 2 can be used to determine if a is an even or odd number.

Q12: Can the same operator have different meanings in different programming languages?

A: Yes, the same symbol may represent different operators or operations in different programming languages. For example, the + operator is used for addition in most languages, but in some languages (like JavaScript), it is also used for string concatenation.

Q13: What is short-circuit evaluation in the context of logical operators?

A: Short-circuit evaluation is a behavior where the second operand of a logical AND ( && ) or logical OR ( || ) operator is not evaluated if the outcome can be determined by the value of the first operand alone. This can lead to more efficient code execution.

Q14: How are bitwise left shift ( << ) and right shift ( >> ) operators used?

A: The bitwise left shift ( << ) operator shifts the bits of a binary number to the left, effectively multiplying the number by 2. The bitwise right shift ( >> ) operator shifts the bits to the right, effectively dividing the number by 2.

Q15: Can you provide an example of operator precedence in programming?

A: Operator precedence determines the order in which operators are evaluated. For example, in the expression a + b * c , the multiplication ( * ) has higher precedence than addition ( + ), so b * c is evaluated first.

Q16: How does the ternary operator differ from an if-else statement?

A: The ternary operator ( ?: ) is a concise way to express a conditional statement with a single line of code. It returns one of two values based on a condition. An if-else statement provides a more extensive code block for handling multiple statements based on a condition.

Q17: Are there any operators specifically designed for working with arrays or collections?

A: Some programming languages provide specific operators or methods for working with arrays or collections. For example, in Python, the in operator is used to check if an element is present in a list.

Q18: How can bitwise operators be used for efficient memory management?

A: Bitwise operators are often used for efficient memory management by manipulating individual bits. For example, bitwise AND can be used to mask specific bits, and bitwise OR can be used to set particular bits.

Q19: Can operators be overloaded in all programming languages?

A: No, not all programming languages support operator overloading. While some languages like C++ allow developers to redefine the behavior of operators for user-defined types, others, like Java, do not permit operator overloading.

Q20: How do you handle operator precedence in complex expressions?

A: Parentheses are used to explicitly specify the order of operations in complex expressions, ensuring that certain operations are performed before others. For example, (a + b) * c ensures that addition is performed before multiplication.

In conclusion, operators in programming are essential tools that enable the manipulation, comparison, and logical operations on variables and values. They form the building blocks of expressions and play a fundamental role in controlling program flow, making decisions, and performing calculations. From arithmetic and comparison operators for numerical tasks to logical operators for decision-making, and bitwise operators for low-level bit manipulation, each type serves specific purposes in diverse programming scenarios.

Please Login to comment...

  • Programming
  • tarunsarawgi_gfg

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

  • Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free
  • English (US)

Assignment (=)

The assignment ( = ) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables.

A valid assignment target, including an identifier or a property accessor . It can also be a destructuring assignment pattern .

An expression specifying the value to be assigned to x .

Return value

The value of y .

Thrown in strict mode if assigning to an identifier that is not declared in the scope.

Thrown in strict mode if assigning to a property that is not modifiable .

Description

The assignment operator is completely different from the equals ( = ) sign used as syntactic separators in other locations, which include:

  • Initializers of var , let , and const declarations
  • Default values of destructuring
  • Default parameters
  • Initializers of class fields

All these places accept an assignment expression on the right-hand side of the = , so if you have multiple equals signs chained together:

This is equivalent to:

Which means y must be a pre-existing variable, and x is a newly declared const variable. y is assigned the value 5 , and x is initialized with the value of the y = 5 expression, which is also 5 . If y is not a pre-existing variable, a global variable y is implicitly created in non-strict mode , or a ReferenceError is thrown in strict mode. To declare two variables within the same declaration, use:

Simple assignment and chaining

Value of assignment expressions.

The assignment expression itself evaluates to the value of the right-hand side, so you can log the value and assign to a variable at the same time.

Unqualified identifier assignment

The global object sits at the top of the scope chain. When attempting to resolve a name to a value, the scope chain is searched. This means that properties on the global object are conveniently visible from every scope, without having to qualify the names with globalThis. or window. or global. .

Because the global object has a String property ( Object.hasOwn(globalThis, "String") ), you can use the following code:

So the global object will ultimately be searched for unqualified identifiers. You don't have to type globalThis.String ; you can just type the unqualified String . To make this feature more conceptually consistent, assignment to unqualified identifiers will assume you want to create a property with that name on the global object (with globalThis. omitted), if there is no variable of the same name declared in the scope chain.

In strict mode , assignment to an unqualified identifier in strict mode will result in a ReferenceError , to avoid the accidental creation of properties on the global object.

Note that the implication of the above is that, contrary to popular misinformation, JavaScript does not have implicit or undeclared variables. It just conflates the global object with the global scope and allows omitting the global object qualifier during property creation.

Assignment with destructuring

The left-hand side of can also be an assignment pattern. This allows assigning to multiple variables at once.

For more information, see Destructuring assignment .

Specifications

Browser compatibility.

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

  • Assignment operators in the JS guide
  • Destructuring assignment

IMAGES

  1. Assignment Operators in C

    assignment operators are

  2. Assignment Operators in C++

    assignment operators are

  3. Assignment Operators in C++

    assignment operators are

  4. PPT

    assignment operators are

  5. Assignment Operators in C Detailed Explanation

    assignment operators are

  6. Assignment Operators in Python

    assignment operators are

VIDEO

  1. assignment 1 (C)

  2. Storing values with assignment operator

  3. SE1-24 Operators and Expressions

  4. 12

  5. 5_Assignment1

  6. Assignment Operators in C#

COMMENTS

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

  2. Assignment operators

    Assignment operators modify the value of the object. Syntax Over load able Prototype examples (for replaces the contents of the object is not modified). For class types, this is performed in a special member function, described in For non-class types, copy and move assignment are indistinguishable and are referred to as

  3. Assignment Operators In C++

    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 ( |= )

  4. Assignment operators

    Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right. becomes equal to becomes equal to the addition of becomes equal to the subtraction of becomes equal to the product of becomes equal to the division of becomes equal to the remainder of

  5. Assignment operators

    01/24/2023 7 contributors Feedback In this article Syntax Remarks Example Simple assignment Show 3 more Syntax expression assignment-operator expression assignment-operator: one of = *= /= %= += -= <<= >>= &= ^= |= Remarks Assignment operators store a value in the object specified by the left operand. There are two kinds of assignment operations:

  6. Assignment Operators in C

    Assignment Operators in C The following table lists the assignment operators supported by the C language − Example Try the following example to understand all the assignment operators available in C − Live Demo

  7. C Assignment Operators

    Syntax assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression assignment-operator: one of = *= /= %= += -= <<= >>= &= ^= |= The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: Expand table

  8. C++ Assignment Operators

    Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( =) to assign the value 10 to a variable called x: Example int x = 10; Try it Yourself » The addition assignment operator ( +=) adds a value to a variable: Example int x = 10; x += 5; Try it Yourself »

  9. What is Assignment Operator?

    An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands. Advertisements

  10. 4.6: Assignment Operator

    Different types of assignment operators are shown below: "=": This is the simplest assignment operator, which was discussed above. This operator is used to assign the value on the right to the variable on the left. For example: a = 10; b = 20; ch = 'y'; +=: This operator is combination of '+' and '=' operators. This operator first ...

  11. Assignment operators

    The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.

  12. Python's Assignment Operator: Write Robust Assignments

    The central component of an assignment statement is the assignment operator. This operator is represented by the = symbol, which separates two operands: A variable ; A value or an expression that evaluates to a concrete value; Operators are special symbols that perform mathematical, logical, and bitwise operations in a programming language.

  13. Java Assignment Operators with Examples

    1. (=) operator: This is the most straightforward assignment operator, which is used to assign the value on the right to the variable on the left. This is the basic definition of an assignment operator and how it functions. Syntax:

  14. JavaScript Assignment

    Assignment operators assign values to JavaScript variables. Shift Assignment Operators Bitwise Assignment Operators Logical Assignment Operators Note The Logical assignment operators are ES2020 . The = Operator The Simple Assignment Operator assigns a value to a variable. Simple Assignment Examples let x = 10; Try it Yourself » let x = 10 + y;

  15. What Are Assignment Operators

    Learning about assignment operators is foundational to programming. They allow us to: - Initiate and change variable values. - Create interactive and responsive programs. - Write more concise and readable code. They are not just symbols on the screen; they pave the way toward mastering programming logic and efficiency.

  16. Operators (The Java™ Tutorials > Learning the Java Language

    In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>".With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common.

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

  18. Assignment Operators in Python

    1) Assign: This operator is used to assign the value of the right side of the expression to the left side operand. Syntax: x = y + z Example: Python3 a = 3 b = 5 c = a + b print(c) Output: 8 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:

  19. Expressions and operators

    This chapter describes JavaScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary and more. At a high level, an expression is a valid unit of code that resolves to a value. There are two types of expressions: those that have side effects (such as assigning values) and those that ...

  20. What are the differences between "=" and "<-" assignment operators?

    800 The difference in assignment operators is clearer when you use them to set an argument value in a function call. For example: median(x = 1:10) x

  21. Java Operators: Arithmetic, Relational, Logical and more

    Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication. Operators in Java can be classified into 5 types: Arithmetic Operators. Assignment Operators.

  22. What are Operators in Programming?

    Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=. Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).

  23. Assignment (=)

    The assignment (=) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables. Try it. Syntax. js. x = y