Variables and Constants Notes

Variables and Constants

  • A variable is a named memory location that can vary.
  • A named constant is assigned a value only once and cannot be changed. It is used to assign a useful name to a value that does not change, like pi.
  • One can also use literal constants, like 2 in the previous example.
  • Constants and variables have data types:
  • Numeric (integer, float, double)
  • String (0 or more characters) – a string with 0 characters is null
  • Boolean (true/false)
  • A data type is a classification that defines:
  • What values can be stored in the variable
  • How the variable is stored in computer memory
  • What operations can be performed on the data item
  • The name of a variable or constant is known as its identifier.
  • The data assigned is its value.

Declarations

  • In most programming languages, the variables and constants used in the program must be declared before being used.
  • A declaration is a statement that provides a data type and an identifier for a variable.
    • num mySalary
    • string myName
  • A language that enforces declarations of variables before usage and does not allow the data type to change is called a strongly-typed

Initialization

  • Before using a variable it is good practice to initialize
  • An initialization statement is one assigning a value to a variable and is usually part of the declaration
    • string myName = “Sharyn”
  • Some programming languages initialize numeric values to zero and string values to null but you CANNOT always rely on this.
  • If a language does not initialize variables and you do not explicitly initialize a variable, it will either be consider to be undefined, or may contain garbage – a value that cannot be processed.

Declarations/Initializations

  • To add declarations / Initializations to Pseudocode or flowcharts:
    • start
      • Declarations
        • num originalNumber
        • num calculatedAnswer
      • input originalNumber

Naming Variables and Constants

  • Most programming languages have syntax rules regarding legal names for variables and constants.
  • For example, it is illegal to use reserved words, such as if, else, while or return as variable or constant names.
  • It is good practice to use a naming convention to differentiate constants and variables (some languages enforce this in their syntax).
    • constant num PI = 3.14159
    • string myName = “Sharyn”
    • constant string STATE = “Pennsylvania”
    • num myZip = “16803”

 

Naming Variables and Constants

  • It is good practice to use meaningful names for variables and constants.
  • Example  Not MEANINGFUL:
    • start
      • Declarations
        • num a, b, c = 0
        • constant num d = 3.1415
      • input a
      • b = a * 2
      • c = b * d
      • output a, b, c
    • end
  • MEANINGFUL
    • start
      • Declarations
        • num radius = 0
        • num diameter = 0
        • num circumference = 0
        • constant num PI = 3.1415
      • input radius
      • set diameter to radius * 2
      • set circumference to diameter * PI
      • output radius, diameter, circumference
    • end

Download PDF