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// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program shows assingment statements
6
7#include <stdio.h>
8#include <string.h>
9
10int main() {
11 // variable definition
12 int numberOfStudents = 2;
13 float width = 32.5F;
14 float length = 10.0F;
15 float areaOfRectangle = 0;
16 char someWords1[] = "Hello";
17 char someWords2[] = "World!";
18 char helloWorld[13] = "";
19
20 // using assignment statements
21 numberOfStudents = numberOfStudents + 5;
22 areaOfRectangle = length * width;
23 strcat(helloWorld, someWords1);
24 strcat(helloWorld, ", ");
25 strcat(helloWorld, someWords2);
26
27 // output
28 printf("The number of students is: %d\n", numberOfStudents);
29 printf("The area of a rectangle is: %.2f cm²\n", areaOfRectangle);
30 printf("%s\n", helloWorld);
31
32 printf("\nDone.\n");
33 return 0;
34}
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program shows assingment statements
6
7#include <iostream>
8
9int main() {
10 // variable definition
11 int numberOfStudents = 2;
12 float width = 32.5F;
13 float length = 10.0F;
14 float areaOfRectangle = 0;
15 std::string someWords1 = "Hello";
16 std::string someWords2 = "World!";
17 std::string helloWorld = "";
18
19 // using assignment statements
20 numberOfStudents = numberOfStudents + 5;
21 areaOfRectangle = length * width;
22 helloWorld = someWords1 + ", " + someWords2;
23
24 // output
25 std::cout << "The number of students is: " << numberOfStudents << std::endl;
26 std::cout << "The area of a rectangle is: " << areaOfRectangle
27 << " cm²" << std::endl;
28 std::cout << helloWorld << std::endl;
29
30 std::cout << "\nDone." << std::endl;
31}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program shows assingment statements
4*/
5
6using System;
7
8/*
9 * The Program class
10 * Contains all methods for performing basic variable usage
11*/
12class Program {
13 public static void Main (string[] args) {
14 // variable definition
15 int numberOfStudents = 2;
16 float width = 32.5F;
17 float length = 10.0F;
18 float areaOfRectangle = 0F;
19 string someWords1 = "Hello";
20 string someWords2 = "World!";
21 string helloWorld = null;
22
23 // using assignment statements
24 numberOfStudents = numberOfStudents + 5;
25 areaOfRectangle = length * width;
26 helloWorld = someWords1 + ", " + someWords2;
27
28 // output
29 Console.WriteLine ("The number of students is: " + numberOfStudents);
30 Console.WriteLine ("The area of a rectangle is: " + areaOfRectangle + " cm²");
31 Console.WriteLine (helloWorld);
32
33 Console.WriteLine ("\nDone.");
34 }
35}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program shows assingment statements
5 */
6
7package main
8
9import "fmt"
10
11func main() {
12 // variable definition
13 numberOfStudents := 2
14 width := 32.5
15 length := 10.0
16 someWords1 := "Hello"
17 someWords2 := "World!"
18
19 // using assignment statements
20 numberOfStudents += 5
21 areaOfRectangle := length * width
22 helloWorld := someWords1 + ", " + someWords2
23
24 // output
25 fmt.Println("The number of students is:", numberOfStudents)
26 fmt.Println("The area of a rectangle is:", areaOfRectangle, "cm²")
27 fmt.Println(helloWorld)
28
29 fmt.Println("\nDone.")
30}
1/*
2 * This program program shows assingment statements
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9final class Main {
10 private Main() {
11 // Prevent instantiation
12 // Optional: throw an exception e.g. AssertionError
13 // if this ever *is* called
14 throw new IllegalStateException("Cannot be instantiated");
15 }
16
17 /** Constant number TWO. */
18 private static final int TWO = 2;
19
20 /** Constant number THIRTY_TWO_POINT_FIVE. */
21 private static final float THIRTY_TWO_POINT_FIVE = 32.5F;
22
23 /** Constant number TEN. */
24 private static final float TEN = 10.0F;
25
26 /** Constant number FIVE. */
27 private static final int FIVE = 5;
28
29 /**
30 * Main entry point into program.
31 *
32 * @param args nothing passed in
33 */
34 public static void main(final String[] args) {
35 // variable definition
36 int numberOfStudents = TWO;
37 float width = THIRTY_TWO_POINT_FIVE;
38 float length = TEN;
39 float areaOfRectangle = 0F;
40 String someWords1 = "Hello";
41 String someWords2 = "World!";
42 String helloWorld = null;
43
44 // using assignment statements
45 numberOfStudents = numberOfStudents + FIVE;
46 areaOfRectangle = length * width;
47 helloWorld = someWords1 + ", " + someWords2;
48
49 // output
50 System.out.println("The number of students is: " + numberOfStudents);
51 System.out.println("The area of a rectangle is: " + areaOfRectangle + " cm²");
52 System.out.println(helloWorld);
53
54 System.out.println("\nDone.");
55 }
56}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program shows assingment statements
4*/
5
6// variable definition
7numberOfStudents = 2
8width = 32.5
9length = 10.0
10someWords1 = "Hello"
11someWords2 = "World!"
12
13// using assignment statements
14numberOfStudents = numberOfStudents + 5
15areaOfRectangle = length * width
16helloWorld = someWords1 + ", " + someWords2
17
18// output
19console.log("The number of students is: " + numberOfStudents)
20console.log("The area of a rectangle is: " + areaOfRectangle + " cm²")
21console.log(helloWorld)
22
23console.log("\nDone.")
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module shows using the assignment statement
6"""
7
8
9def main() -> None:
10 """The main() function shows variable definition, returns None."""
11 number_of_students = 2
12 width = 32.5
13 length = 10.0
14 some_words1 = "Hello"
15 some_words2 = "World!"
16
17 # using assignment statements
18 number_of_students = number_of_students + 5
19 area_of_rectangle = length * width
20 hello_world = some_words1 + ", " + some_words2
21
22 # output
23 print("The number of students is: " + str(number_of_students))
24 print("The area of a rectangle is: " + str(area_of_rectangle) + " cm²")
25 print(hello_world)
26
27 print("\nDone.")
28
29
30if __name__ == "__main__":
31 main()
Example Output
