# Class 03: A Quick Introduction to Pascal (1)

Held: Monday, 26 January 2004

Summary: Today we begin our study of Pascal, the source language for our compiler.

Notes:

• Any questions on the homework?
• Cool convo Thursday; Extra credit for attending.
• Strange panel Thursday at 7:30; Extra credit for attending.
• Support self governance. Be a senator!

Overview:

• A short history of Pascal
• Parts of the language to consider
• Variables and Types
• Basic Operations
• Conditionals

## A Short History of Pascal

• Pascal is a descendendant of Algol.
• Algol was one of the first four big programming languages:
• Fortran: Designed for mathematical computation. Popular primarily in Physics.
• COBOL: Designed for information processing. Popular primarily in Business and Government.
• LISP: Designed for AI. Popular primarily at MIT.
• Algol: Designed for comptuer scientists to use to express algorithms. Popular primarily in Europe.
• Algol is an imperative language. You should know what that means.
• Niklaus Wirth designed Pascal with two goals in mind
• Getting rid of many things he didn't like that his colleagues had added to Algol
• Creating a language good for teaching
• Many computer scientits still use a Pascal-like syntax when writing imperative algorithms.

## What belongs in an imperative language?

• Pascal is an imperative language, so we should consider the key aspects of imperative languages as a way of helping us decide what to look at.
• Here are some things I expect in an imperative language:
• Variables
• Types for those variables (sometimes implicit, sometimes explicit)
• Type definitions
• Basic operations for input, output, and assignment
• A mechanism for sequencing operations
• Control structures for conditional executation
• Control structures for repeated evaluation
• Procedures (that permit recursion)
• We'll cover as many of these issues as we can at a high level and return to more precise details throughout the semester.

## Overall Structure

• Pascal programs have an interesting structure.
• The structure looks something like the following
```program name(ports);

const
constant declarations

type
type declarations

var
variable declarations

procedure and function definitions

begin
sequence of statements separated by semicolons
end .
```
• All of the definitions and declarations are optionsl.

## Variables and Types

• Variable (and other) names in Pascal look much like they do in C and Java: Sequences of alphanumerics that begin with an alphabetic character. (I'll admit that I never remember whether other characters are permitted, and implementations seem to differ.)
• Variables must be declared (as in Java and C) and typed.
• Once you've indicated that you are declaring variables with the `var` keyword, you write variables names (separated by commas), a colon, and a type. For exmaple
```var
x, y: integer;
z: real;
```
• The basic types are `integer`, `real`, and `Boolean`.
• The primary compound type is the array, written as
```array[lowest-index..highest-index] of type
```
• One can also build records (something like classes, but without the procedures) with
```record variable-declarations end
```
• Pascal also makes it easy to define types that are collections of names (enumerated types). For example
```type
weekdays = (Monday, Tuesday, Wednesday, Thursday, Friday);
```

## Simple Statements

• Assignment is done with the `:=` operation. For example,
```  x := x+1;
```
• Input is done with `read` and `readln`.
```  read(x);
```
• The extra `ln` indicates that the end-of-line character should be read.
• Output is done with `write` and `writelin`.
• The extra `ln` indiates that the end-of-line character should be printed.
• Neighboring statements are separated by semicolons.
• Sequences of statements can be grouped together into a compound statmeent by surrounding the sequence with the keywords `begin` and `end`.

## Conditionals

• Simple
```if test then statement
```
• With alternative
```if test then statement else statement
```
• Case statements
```case exp of
val1: statement1
val2: statement2
...
valn: statementn
end
```
• Wirth felt that the values should encompass all possible values of the expression.
• Implementors have usually added a `default` or `else` case.
• Many implementors have allowed ranges and groups of values on the left.
• Case statements can be a lot of fun to compile. (We'll return to the issue later in the semester.)

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 5 11:46:55 2004.
The source to the document was last modified on Tue Jan 20 23:06:45 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2004S/Outlines/outline.03.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu