Home -> Programming -> Processing -> Assignment Statements

Assignment Statements

Assignment statements are the simplest programming statement and they are available in every programming language. They are used to put data into variables or properties. They are used to perform arithmetic. A lot of the code of any program consists of assignment statements. This command is probably the most commonly used code statement in all our programming.

In programming we often speak of programming patterns. These are standard ways in which programming commands can be used. There are several standard assignment patterns which are used by assignment statements. The most common ones are as follows:

  • A = B - read as: store B in A.
  • This is a plain assignment and the most commonly used assignment command. It puts the value of B into variable A. For example storing a literal into a label:

    lblOutput.Text = "Mary Jane" - here A is the property lblOutput.Text and B is the literal"Mary Jane". The literal is assigned into the Property lblOutput.Text or
    lblOutput.Text = theName - here A is lblOutput.Text and B is a variable theName. In this example the contents of theName is assigned into variable A.

    Notice that the first example uses quote marks around the persons name because this is actual data. When we refer to actual data like this we call it a literal. A text literal must be enclosed inside quotes. A numeric literal has no quotes. The second example does not use quotes because we are referring to the contents of a variable named theName. In both cases we are storing something into the control property named lblOutput.Text. Do not forget the .text as this is the property of the lblOutput object into which string data is actually stored.

  • A = B op C - read as perform calculation on right side of the equals sign and store the result in A.
  • This is an assignment statement where some mathematical calculation is taking place and the result is stored in A. It is probably the second most commonly used command in programming since mathematical manipulation of variables is common in programming. Here op stands for operator such as add, subtract, multiply, divide, exponent and so forth. In fact you can arrange for several calculations to be performed with the result being assigned into A. To do this you must follow the Order of Precedence rules. These are stated in the section on Order of Precedence below. The two most commonly used calculation patterns are:
    • A = A + 1 read as: Add 1 to A and store the result back into A.
    • This is a counting assignment pattern and increments A by 1. For example incrementing the variable NumberOfGroups would be: NumberOfGroups = NumberOfGroups + 1 This increases the value stored in the variable NumberOfGroups by 1.
      There is another way to write this expression which will be more familiar if you know the programming language C. The alternative form is A += 1 You can use either form interchangeably. For most people who are newcomers to programming the first form is easier to understand. the second form was developed as a shortcut which requires less typing.

    • A = A + B - read as: Add B to A and store the result back into A.
    • This is an accumulation assignment pattern, and is used situations where we need running totals. For example in creating a cash register program, we might have a variable total to track a customer's total purchases and another variable sale which represents the item just entered or scanned. The code to accumulate each sale as it is processed in the total would be: total = total + sale - here A is total and B is the variable sale. The alternative form of this is A +=B.
      Note! this pattern can also concatenate text (add text to the end of an existing string. This can be done either using the + operator or by using the & operator.

  • A = B op C - read as: compare B to C and store the boolean result of the comparison in A. The stored result must be either the value True or the value False. Here op must be a logical comparison. The operators are listed below.
  • This is a combination of a comparison and an assignment. The comparison is: B op A where op can be equal (=), not equal (<>), less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=). The result of the comparison must be either True or False and that result is then assigned into the container or variable A.
There are also some rules for using assignment statements which must be followed. In these patterns, A, B and C must be as follows:

Rules of Precedence

Arithmetic expressions are evaluated from left to right. Operations are performed in accordance with the acronym BEDMAS. Each of these letters represent an arithmetic operation.

B - Brackets are given precedence over all other arithmetic operators.

E - Exponentiation is given second precedence. This means powers and roots are calculated second.

D - Division receives third precedence.

M - Multiplication receives fourth precedence.

A - Addition receives fifth precedence.

S - Subtraction receives last precedence.

In actual fact if there is Division and multiplication intermixed the expression is merely evaluated from left to right. Similarly Addition and Subtraction, if intermingled they are performed form left to right. A more technically correct way to represent the acronym would be B|E|DM|AS. So there are four levels of precedence with division and multiplication being resolved together and Addition and subtraction being resolved together.