Expressions and Assignment Statements

A sarcastic image to emphasize the last chunk even more


  • Finally, the assignment statement, from its simplest form to all of its variations, is covered, including assignments as expressions and mixed-mode assignments.

Assignment Statements

The assignment statement is one of the central constructs in imperative languages. It provides the mechanism by which the user can dynamically change the bindings of values to variables.

Simple Assignments

Ada and ALGOL 60 use := as the assignment operator to avoid confusion between assignment and equality. In Fortran and Ada, an assignment can appear only as stand-alone statement, and the destination is restricted to a single variable.

Conditional Targets

Perl allows conditional targets on assignment statements.

Example: ($flag ? $count1 : $count2) = 0;

Compound Assignment Operators

Definition: A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment.

Compound assignment operators were introduced by ALGOL 68, were later adopted in a slightly different form by C, and are part of the other C-based languages, as well as Perl, JavaScript, Python, and Ruby

Example: sum += value; is equivalent to sum = sum + value;

Unary Assignment Operators

The C-based languages, Perl, and JavaScript include two special unary arithmetic operators that are actually abbreviated assignments.

Example: this is based on prefix, but the same logic is applied for postfix

is equivalent to the below code
is equivalent to the above code

When two unary operators apply to the same operand, the association is right to left.

Example: -count ++

Explanation: · count is first incremented and then negated. So, it is equivalent to - (count ++)

Assignment as an Expression

In the C-based languages, Perl, and JavaScript, the assignment statement produces a result, which is the same as the value assigned to the target. This design treats the assignment operator much like any other binary operator, except that it has the side effect of changing its left operand.

Example: while ((ch = getchar()) != EOF) { … }

Explanation: In this statement, the next character from the standard input file, usually the keyboard, is gotten with getchar and assigned to the variable ch. The result, or value assigned, is then compared with the constant EOF. If ch is not equal to EOF, the compound statement {…} is executed. Note that the assignment must be parenthesized — in the languages that support assignment as an expression, the precedence of the assignment operator is lower than that of the relational operators. Without the parentheses, the new character would be compared with EOF first. Then, the result of that comparison, either 0 or 1, would be assigned to ch.


  • It provides yet another kind of expression side effect. This type of side effect can lead to expressions that are difficult to read and understand.
  • There is a loss of error detection in the C design of the assignment operation that frequently leads to program errors.
Rather than testing a relational expression, the value that is assigned to x is tested (in this case, it is the value of y that reaches this statement)

Java and C# allow only boolean expressions in their if statements, disallowing this problem

Multiple Assignments

Several recent programming languages, including Perl and Ruby provide multiple-target, multiple-source assignment statements.

The semantics is that 20 is assigned to $first, 40 is assigned to $second, and 60 is assigned to $third. If the values of two variables must be interchanged, this can be done with a single assignment, as with

Assignment in Functional Programming Languages

All of the identifiers used in pure functional languages and some of them used in other functional languages are just names of values. As such, their values never change.

Example: in ML, names are bound to values with the val declaration, whose form is exemplified in the following:

Crucial point: If cost appears on the left side of a subsequent val declaration, that declaration creates a new version of the name cost, which has no relationship with the previous version, which is then hidden.

Mixed-Mode Assignment

The design question is: Does the type of the expression have to be the same as the type of the variable being assigned, or can coercion be used in some cases of type mismatch?

Answer: C, C++, and Perl use coercion rules for mixed-mode assignment that are similar to those they use for mixed-mode expressions; that is, many of the possible type mixes are legal, with coercion freely applied.

Additional: Note that in Python and Ruby, types are associated with objects, not variables, so there is no such thing as mixed-mode assignment in those languages.

Answer: Java and C# allow mixed-mode assignment only if the required coercion is widening**.

Explanation: an int value can be assigned to a float variable, but not vice versa. Disallowing half of the possible mixed-mode assignments is a simple but effective way to increase the reliability of Java and C#, relative to C and C++

Additional: **Not quite true: If an integer literal, which the compiler by default assigns the type int, is assigned to a char, byte, or short variable and the literal is in the range of the type of the variable, the int value is coerced to the type of the variable in a narrowing conversion. This narrowing conversion cannot result in an error


  1. Sebesta, R. W. (n.d.). Expressions and Assignment Statements. In Concepts of Programming Languages (12th ed., pp. 320–327).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store