Introduction to Programming/Numeric Variables

From Wikiversity
Jump to navigation Jump to search

Integer variables[edit | edit source]

Integer values and variables are exact representations of numbers from the mathematical set of integers. The range of values that can be stored is finite and is determined by the size of the memory location used. Unsigned integers can store only positive numbers and 0. Signed integers can store both positive and negative numbers. Signed integers are stored using a format called two's complement and have a range that is roughly symmetrical and centered on 0.

Most strongly typed programming languages support a variety of integer types. Java, for example has five.

Java integer types [1]
label size (bytes) smallest value largest value
byte 1 -128 127
short 2 -32768 32767
int 4 -2147483648 2147483647
long 8 -9223372036854775808 9223372036854775807
char 2 0 65535

Although Java does not generally use unsigned types, it does have the char type (short for character), an unsigned two byte integer. Many programming languages treat characters as integers. In C, for example, 'A' is not really a capital letter A but the numeric value 65 and the numeric expression 'C'—'A' is a perfectly valid calculation with the value 2. When reading input or writing output, the numeric value of a character variable is converted to or from the corresponding text character.

Integer literals[edit | edit source]

In most programs, the programmer will need to give the program information. In this C like construct:

    int numberOfMonths = 12;

The programmer declares a variable of type int, giving it the name "numberOfMonths", and initializes it with the value 12. The value 12 is an example of an integer literal. The exact rules for integer literals vary from language to language but most generally, they may start with a sign (the plus character '+' is redundant, but allowed) followed by a sequence of digits. A decimal point is not permitted. The presence of a decimal point is one thing that differentiates integer literals from floating point literals.

Integer Calculations[edit | edit source]

Calculations involving integers can be as simple as 1 + 1 or as complicated as anything that the programmer can imagine but the rules are fairly straightforward and should be moderately familiar to anyone who has had high school algebra.

"1 + 1" is an example of an integer expression—so called because the final answer that the computer gets for the calculation is an integer. In this example, there are two operands (integer literals in this case) and an operator, the plus character. The process of performing the calculation is called evaluating the expression and the answer is the result or the value of the expression.

Every programming language that supports integers has, at a minimum, four integer operators—one each for addition, subtraction, multiplication and division. Some languages, such as Pascal, have an exponent operator. Some languages, such as C++, allow a programmer to define new operators.

Of the four standard operators, the one that can cause the most trouble is division. What is 3 divided by 2? The answer that you get will depend on the language and the operator. In some languages (Java, C, C++), if both operands are integers, the result will be integer and so 3 / 2 will be 1. In some languages (Pascal), there are different operators for integer division and floating point division and so 3 / 2 is 1.5 (and not an integer) but 3 div 2 is 1.

Operator Precedence[edit | edit source]

In pretty much all languages that support integer expressions, some operators are evaluated before others. In high school algebra, everyone learns that multiplication and division are done before addition and subtraction—unless there are parentheses involved. The rules that dictate which operations are performed first is called precedence.

Parentheses have the highest precedence. The exact order after that depends on what operators are supported by the language—many programming languages have a variety of operators.

What about operators of equal precedence? In math, it doesn't matter what order 1 + 2 + 3 is evaluated in, but in a computer, for some expressions, it does. Every programming language will also have rules for associativity—the direction in which a given order associates. Addition generally associates left-to-right, so the expression 1 + 2 + 3 would be evaluated (1 + 2) + 3.

Now, look at this code:

    int i = 5;
    int j = 7;
    int k;

    k = i + (j * 3) * 4;

How many operators are in the last line? Yes, the equal sign is an operator, so there are four.

Most programming languages have an assignment operator that causes the value calculated on the right side to be stored in the memory location identified by the variable on the left side. This process is known as assignment.

In most programming languages where there is an assignment operator, they have a lower precedence than addition and subtraction. Also, they associate from right to left. The expression a = b = c would associate a = (b = c)

It is a good idea to study the precedence and association rules for each programming language that you learn.

Review of Concepts[edit | edit source]

The following terms were introduced in this learning activity:

  • assignment – the process of storing a value into the memory location identified by a variable
  • assignment operator – the character sequence in a given programming language that directs the computer to perform an assignment
  • associativity – the rules that define the order in which two adjacent operators of equal precedence are evaluated (see also precedence)
  • evaluation – the process where the computer performs the calculation called for by an operator upon its operands
  • operator – a sequence of one or more characters that directs the computer to perform some kind of activity, such as a calculation, usually involving a number of values, called operands
  • precedence – the rules that define when one operator is evaluated before another (also see associativity)
  • signed integer – an integer variable that can store both positive and negative numbers
  • two's complement – the binary form that is used for storing negative integers
  • unsigned integer – an integer variable that can store only zero and positive numbers

Exercise: Integer Variables[edit | edit source]

Now would be a good time to see how integers, integer constants and integer expressions are handled in an actual programming language.

References[edit | edit source]

  1. The Java Language Specification, Third Edition