1 the const definition const pi = 3.14159, city = ‘new york’; constant identifiers are used when...

10
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.

Upload: ralf-hancock

Post on 29-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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.

Page 2: 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

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

Page 3: 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

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’;

Page 4: 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

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)

Page 5: 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

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

Page 6: 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

6

Chapter 3 topics

• write / writeln

• readln

• VAR declaration

• CONST definition

• assignment statement (including arithmetic)

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

10

Formatting output: Boolean

• Similar to strings

• note: Although the compiler is not Cap sensitive, it always prints Boolean values in all CAPS.