Language - Assignment

About

An assignment is an expression that gives a value to a variable.

An assignment is not a statement but an expression.
The assignment is generally contained in one statement but not always, you may have several assignment expression in one statement.

Type and syntax

Implicit

Simple form

In its simplest form, an assignment statement has a variable on the left of the = sign and an expression on the right. This ordered form, and similar variations, is quite prevalent in modern programming languages.

variableName = value
  • Other variations exists but are less prevalent.
value -> variableName
// or
value <- variableName

<WRAP tip round> Generally in a language syntax,

</note>

Tuple

A tuple assignment allows several variables to be assigned at once.

Example: x becomes y and vice versa.

x, y = y, x

Operator (Compound)

An assignment operator (also known as Compound operator) is a short cut language structure that combine a math operation with assignment,

Example:

x = x * scale

can be rewritten as

x *= scale

The same is true for all:

Example of compound operator:

  • +=,
  • -=,
  • *=,
  • /=

Explicit

Assignment statements are an explicit form of assignment, but there are many places in a program where an assignment occurs implicitly:

  • a function call implicitly assigns the argument values to the corresponding parameter variables;
  • a return statement implicitly assigns the return operands to the corresponding result variables;
  • and a literal expression for a composite type such as an array

Go Example:

medals := []string{" gold", "silver", "bronze"}

implicitly assigns each element, as if it had been written like this:

medals[ 0] = "gold" 
medals[ 1] = "silver" 
medals[ 2] = "bronze"

Documentation / Reference


Powered by ComboStrap