Variable a memory location whose contents can vary

• Variable: a memory location whose contents can vary; also called an identifier • Each programming language has it own rules for naming identifiers, including: – Legal characters – Maximum length – Use of upper or lower case • Variable name must be a single word, but can be formed from several words – rate, interest. Rate, interest_rate • Constant: value does not change – Values that do not change while the program is running – Have identifiers, and can be used like variables for calculations, but cannot be assigned new values • Literal: is use of data itself (ex: pi – 3. 1474)

Variables, Constants and Literals Variable: Name of a collection of memory cells that are treated as a unit. Contains data that can be changed by the programmer. A container for data that can be changed. Constant: A named data item that contains information that the programmer does not change. The information stays the same throughout the program execution. A container for data that does not change. Literal: A constant whose name is the written representation of its value. Example: add 50 to total_accumulator

Variables, Constants and Literals Variables: Contains data that can be changed - OR - Container

Variables, Constants and Literals Constant: Contains data that can NOT be changed - OR - Container of Milk

Declaring Variables Assign identifiers to memory locations Specify the name and data type Use meaningful names and follow standards Prefixes may be used to group related variables Declare a variable for each field in a data file

Declaring Variables • Initializing (or defining) the variable: providing an initial value • Some languages provide default initial values • Other languages leave variables with an unknown or garbage value • Variables representing data fields in files do not need to be initialized

Declaring Variables • Local variables: declared within a module • Global variables: declared at the beginning of the program, and used in all modules • Annotation box: flowchart symbol containing notes • Data Dictionary: list of variables used in a program, with their type, size, and description
- Slides: 7