Chapter 3 Lecture Notes (9/9/99)



Naming Constants

Constants can be named. Use the CONST keyword, followed by the list of constant-value pairs in your program, procedure, or function header. Example:

const BranchCount=10;

WindowCount=10;

InterestRate=0.06;

AccountCode=’S’;

These constants work much like standard constants, like 10 – you cannot modify them and their type is based on the value.

Comments

Comments are enclosed in curley braces. Useful for BEGIN/END pairs.

Formatted Output

With write and writeln, you can specify the number of spaces a value should be displayed in and the number of places after the decimal should be displayed (for reals).

Example:

writeln (‘Cost is: $‘,cost:6:2);

Output:

Cost is $ 12.63

(6 spaces, right justified, 2 places after the decimal point)

Maximum values

As we discussed in chapter one, depending on the number of bits a value is (and whether it’s signed or unsigned), there is a maximum value that an integer can be. Pascal uses maxint to specify this.

Simple Branching

To test one or more conditions and execute statements if the condition evaluates to true:

if <condition> then

<statement>

else

<statement>

Don’t use a semicolon after the statement directly before the else!

(remember, you must use BEGIN and END; for a compound statement.)

(another reminder, use END. At the end of your program.)

Conditions

Conditions usually take the form:

<variable1> <operator> <variable2>

Where the operator is one of the following:

= <> < <= > >=

You can also use the following boolean operators:

AND OR XOR

…and you can also use parentheses!

Simple Looping – While Statements

To do some simple looping, use the following syntax:

while <condition> do

<statement>

(use BEGIN/END;)

Repeat Statements

Usage:

repeat

<statement>

until <condition>;

For Statements

for <variable> := <start value> to <end value> do

<statement>

Standard functions

abs/round/trunc/sqr/sqrt (will explain in class)