< previous page page_60 next page >

Page 60
Although character and string literals are put in quotes, literal integers and floating point numbers are not, because there is no chance of confusing them with identifiers. Why? Because identifiers must start with a letter or underscore, and numbers must start with a digit.
An alternative to the literal constant is the named constant (or symbolic constant), which is introduced in a declaration statement. A named constant is just another way of representing a literal value. Instead of using the literal value in an instruction, we give it a name in a declaration statement, then use that name in the instruction. For example, we can write an instruction that multiplies the literal values 3.14159 and 4.5. Or we can define a constant in a declaration statement for each of those values, and then use the constant names in the instruction. For example, we can use either
3.14159 × 4.5 or PI × RADIUS
but the latter is more descriptive.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Named Constant A location in memory, referenced by an identifier, where a data value that cannot be changed is stored.
It may seem easier to use the literal value of a constant than to give the constant a name and then refer to it by that name. But, in fact, named constants make a program easier to read because they make the meaning of literal constants clearer. And named constants also make it easier to change a program later on.
This is the syntax template for a constant declaration:
ConstantDeclaration
const DataType Identifier = LiteralValue;

Notice that the reserved word const begins the declaration, and an equal sign (=) appears between the identifier and the literal value.
These are valid constant declarations:

 
< previous page page_60 next page >