Assignment Statement

Programs can have many variables. Usually information is gathered from the user, stored in a variable, processed with other variables, saved back to one/some variable(s) and then returned to the user. Variables are changed or initially assigned a value by the use of an assignment statement. Assignment statements are usually read in reverse order from what we are use to in math class. A variable on the left side of the assignment statement will receive the value that is on the right hand side of the assignment statement. Note that different programming languages use different symbols to represent the assignment statement (for example in Alpha it is” ←”, in Pascal it is” :=”). No matter what the symbol is, you always read it as, “is assigned”. This is particularly important in many languages where the assignment symbol is an equal sign ( = ) and people are use to reading this as “is equal to”. In many of these language when you actually want to check for “equality” you then use (==).

Here are a few examples of assignment statements:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program shows assingment statements

#include <stdio.h>
#include <string.h>

int main() {
    // variable definition
    int numberOfStudents = 2;
    float width = 32.5F;
    float length = 10.0F;
    float areaOfRectangle = 0;
    char someWords1[] = "Hello";
    char someWords2[] = "World!";
    char helloWorld[13] = "";

    // using assignment statements
    numberOfStudents = numberOfStudents + 5;
    areaOfRectangle = length * width;
    strcat(helloWorld, someWords1);
    strcat(helloWorld, ", ");
    strcat(helloWorld, someWords2);

    // output
    printf("The number of students is: %d\n", numberOfStudents);
    printf("The area of a rectangle is: %.2f cm²\n", areaOfRectangle);
    printf("%s\n", helloWorld);

    printf("\nDone.\n");
    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program shows assingment statements

#include <iostream>

int main() {
    // variable definition
    int numberOfStudents = 2;
    float width = 32.5F;
    float length = 10.0F;
    float areaOfRectangle = 0;
    std::string someWords1 = "Hello";
    std::string someWords2 = "World!";
    std::string helloWorld = "";

    // using assignment statements
    numberOfStudents = numberOfStudents + 5;
    areaOfRectangle = length * width;
    helloWorld = someWords1 + ", " + someWords2;

    // output
    std::cout << "The number of students is: " << numberOfStudents << std::endl;
    std::cout << "The area of a rectangle is: " << areaOfRectangle
              << " cm²" << std::endl;
    std::cout << helloWorld << std::endl;

    std::cout << "\nDone." << std::endl;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program shows assingment statements
*/

using System;

/*
 * The Program class
 * Contains all methods for performing basic variable usage
*/
class Program {
    public static void Main (string[] args) {
        // variable definition
        int numberOfStudents = 2;
        float width = 32.5F;
        float length = 10.0F;
        float areaOfRectangle = 0F;
        string someWords1 = "Hello";
        string someWords2 = "World!";
        string helloWorld = null;

        // using assignment statements
        numberOfStudents = numberOfStudents + 5;
        areaOfRectangle = length * width;
        helloWorld = someWords1 + ", " + someWords2; 

        // output
        Console.WriteLine ("The number of students is: " + numberOfStudents);
        Console.WriteLine ("The area of a rectangle is: " + areaOfRectangle + " cm²");
        Console.WriteLine (helloWorld);

        Console.WriteLine ("\nDone.");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program shows assingment statements
 */

package main

import "fmt"

func main() {
	// variable definition
	numberOfStudents := 2
	width := 32.5
	length := 10.0
	someWords1 := "Hello"
	someWords2 := "World!"

	// using assignment statements
	numberOfStudents += 5
	areaOfRectangle := length * width
	helloWorld := someWords1 + ", " + someWords2

	// output
	fmt.Println("The number of students is:", numberOfStudents)
	fmt.Println("The area of a rectangle is:", areaOfRectangle, "cm²")
	fmt.Println(helloWorld)

	fmt.Println("\nDone.")
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * This program program shows assingment statements
 *
 * @author  Mr Coxall
 * @version 1.0
 * @since   2020-09-01
 */

final class Main {
  private Main() {
    // Prevent instantiation
    // Optional: throw an exception e.g. AssertionError
    // if this ever *is* called
    throw new IllegalStateException("Cannot be instantiated");
  }

  /** Constant number TWO. */
  private static final int TWO = 2;

  /** Constant number THIRTY_TWO_POINT_FIVE. */
  private static final float THIRTY_TWO_POINT_FIVE = 32.5F;

  /** Constant number TEN. */
  private static final float TEN = 10.0F;

  /** Constant number FIVE. */
  private static final int FIVE = 5;

  /**
   * Main entry point into program.
   *
   * @param args nothing passed in
   */
  public static void main(final String[] args) {
    // variable definition
    int numberOfStudents = TWO;
    float width = THIRTY_TWO_POINT_FIVE;
    float length = TEN;
    float areaOfRectangle = 0F;
    String someWords1 = "Hello";
    String someWords2 = "World!";
    String helloWorld = null;

    // using assignment statements
    numberOfStudents = numberOfStudents + FIVE;
    areaOfRectangle = length * width;
    helloWorld = someWords1 + ", " + someWords2;

    // output
    System.out.println("The number of students is: " + numberOfStudents);
    System.out.println("The area of a rectangle is: " + areaOfRectangle + " cm²");
    System.out.println(helloWorld);

    System.out.println("\nDone.");
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Created by: Mr. Coxall
 * Created on: Sep 2020
 * This program shows assingment statements
*/

// variable definition
numberOfStudents = 2
width = 32.5
length = 10.0
someWords1 = "Hello"
someWords2 = "World!"

// using assignment statements
numberOfStudents = numberOfStudents + 5
areaOfRectangle = length * width
helloWorld = someWords1 + ", " + someWords2

// output
console.log("The number of students is: " + numberOfStudents)
console.log("The area of a rectangle is: " + areaOfRectangle + " cm²")
console.log(helloWorld)

console.log("\nDone.")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
"""
Created by: Mr. Coxall
Created on: Sep 2020
This module shows using the assignment statement
"""


def main() -> None:
    """The main() function shows variable definition, returns None."""
    number_of_students = 2
    width = 32.5
    length = 10.0
    some_words1 = "Hello"
    some_words2 = "World!"

    # using assignment statements
    number_of_students = number_of_students + 5
    area_of_rectangle = length * width
    hello_world = some_words1 + ", " + some_words2

    # output
    print("The number of students is: " + str(number_of_students))
    print("The area of a rectangle is: " + str(area_of_rectangle) + " cm²")
    print(hello_world)

    print("\nDone.")


if __name__ == "__main__":
    main()

Example Output

Code example output