1 the const definition const pi = 3.14159, city = ‘new york’; constant identifiers are used when...
TRANSCRIPT
1
The CONST definition
• CONST Pi = 3.14159, City = ‘New York’;
• Constant identifiers are used when you do not want the value of an identifier to change
• why not just put the value in the program?
• Note: The compiler automatically defines the type to the constant identifier by looking at the value assigned to it.
2
Reserved words
• Certain words have special meaning to the compiler. Therefore these words cannot be used as constant or variable identifiers.
• We have seen some examples:– DIV, MOD– VAR, CONST– PROGRAM, BEGIN, END
3
String Variables
• Note: This is not part of the Pascal standard. It was added to Turbo Pascal (and some other Pascal compilers)
• To declare a string variable we could write:– VAR Country: string[13];
• To assign a value to a string variable we could write:– Country := ‘United States’;
4
More about integers
• What if you need an integer larger than 32767?– Using integer type would be incorrect– another type called LongInt is used
• -2147483648 -> 2147483647
• other integer types are:– byte (0->255)– shortint (-128->127)– word (0->65535)
5
More about reals
• Reals also have other types– real 6 bytes 11 to 12 digits– single 4 bytes 7 to 8 digits– double 8 bytes 15 to 16 digits– extended 10 bytes 19 to 20 digits
6
Chapter 3 topics
• write / writeln
• readln
• VAR declaration
• CONST definition
• assignment statement (including arithmetic)
7
Formatting output: integers• Field: a group of columns in which results
are printed is called a field
• Field width: The number of columns in the field
• writeln(IntId:##)– IntId is an integer identifier or value– ## is the rightmost column to print the integer
(Compiler will ignore this value if its smaller than number of digits)
• can be written as any integer expression
• default is one
8
Formatting output: reals
• Writeln (RealId:#1:#2)– RealId is a real identifier or value– #1 is the rightmost column to print the real– #2 is the number of digits you want on the right
of the decimal point.• If #2 is omitted, scientific notation is used
9
Formatting output: strings
• Writeln (stringId:##)– stringId can be a string or character identifier or
value– ## is the rightmost column for the string to be
printed in
10
Formatting output: Boolean
• Similar to strings
• note: Although the compiler is not Cap sensitive, it always prints Boolean values in all CAPS.