Midterm Review: Pascal

October 1996


This is a summary of most of the key information we've covered. You're still responsible for everything in the book (Chapters 1-5, and Section 12.1 for the Midterm), even if it's not covered in this Web document.

Writing, Compiling, and Running a Program

You've written programs in the Turbo environment, compiled them, and run them.

You should understand that the Compiler reads the "source code" -- the stuff you write in Pascal -- and translates it into machine language, which the computer's CPU will be able to understand. (That machine language is sometimes called the "object code.")

One of the things the compiler does, as it translates Pascal to machine language, is to reserve memory locations for all your variables. In the machine code, the CPU's instructions refer to those locations, not to the variable names you originally used. That's why you have to put a var statement near the beginning of each program: so the compiler will know what memory locations to reserve, and what kind of data will be stored there.

Note that there is a Destination option under the Compile menu. It's usually set to Memory, but if you change it to Disk, then your compiled program will be written on the disk. If you had saved the source code as tax.pas, then the compiled program will get the name tax.exe. You can run that program just by typing "tax" at the DOS prompt. And you copy the tax.exe file onto a floppy and give it to other people to run on their machines, even if they don't have Turbo Pascal.

The Turbo environment: the debugger

For debugging in Pascal, you should know how to:

If you didn't have a debugger, you could do something similar by putting lots of writeln statements in your program.

The shape of a Pascal Program

The basic Pascal program has a name, a var statement, and some code bracketed by the words begin and end:

program hiThere;
var x: integer;
begin
  x:=5;
  writeln('Hi there, x is: ', x);
end.

More advanced Pascal programs have (1) a lot more code between the begin and end, and (2) other things before the first begin, including:

Semicolons

Semicolons in Pascal are statement SEPARATORS. Technically, they go between statements, not after every one.

If you put a semicolon after every statement you are USUALLY correct. (See Savitch, bottom of p. 88 for discussion.)

You can't have a semicolon before an ELSE statement, because that makes the compiler think the IF-THEN is finished. (See Savitch, bottom of p. 82.)

Variables: var statements

Variables are locations in memory that hold the data the program is working with.

When you write the program, you decide what variables you'll need and what to name them. Pascal has some limits on what kind of names you can use. Try to use names that are meaningful, such as "income" or "rate" instead of "x" or "y".

You MUST list every one of your variables in the var statement, to tell the compiler two things:

The var statement also helps you because:

Types

Data in memory and files is stored as strings of bits. Each bit can be either on or off (usually written as 1 or 0). So, a piece of data might be 01000001. That could stand for the integer 65, or it stand for the character A, or it could be part of a longer string of bits that make up a real number, such as 15.0000000E+65. Pascal has to know what kind of data you're working with, so it can interpret it correctly. To provide that information to Pascal, you declare every variable (and every function and file) to have a certain type.

Simple types include:

Two other types you should know about are:

There are additional simple types, for purposes such as working with larger integers or reals than the standard types allow. And there are complex types, made up of combinations of simple types. That's a later topic.

Pascal is a "strongly typed" language, which means the compiler is very picky about having you declare types and stick to your declarations. You can't add 3 to character A, even though you know A is really stored as the number 65. (In C, you could perform that addition.)

Pascal's arithmetic expressions are picky about types too. Add two reals and you get a real, which you can't put into an integer variable, even if the sum is simply 5.

There is some migration between types during arithmetic, though. Divide an integer by an integer and you get a real. See the Figure 2.6 in Savitch, p.53, for details.

There are also functions that let your convert, or "cast", types: ord('A') returns 65, while char(65) returns 'A'.

Constants

Constants are declared with the const statement near the beginning of the program, procedure, or function (See Savitch, p.69, for examples.)

A constant gives some of the same benefits as a subroutine (procedure or function):

Assignment statements (:=)

To put data into a variable, use the assignment statement. For example, x := 32 puts the number 32 into the memory location that the compiler has reserved for the variable named x.

Like everything else in Pascal, assignment is strongly typed. You can't put a character value into an integer variable, for example. There are a few exceptions: You can assign an integer value to a real variable (see Savitch, p.52).

Arithmetic

You should know how to use the basic arithmetic operators in Pascal: add, subtract, multiply, and divide. Issues to understand are type compatability (Savitch, pp. 52-53) and operator precedence (Savitch, p. 54). Remember, if you're not sure of operator precedence, you can always put in parentheses to make it clear.

writeln and write

The basic writeln() procedure sends data to the screen: writeln('hello world').

The "ln" at the end of writeln means, write this and then move to the next line. The write() procedure puts the data on screen and leaves the cursor at the end of the data.

You can also use write and writeln to send data into a file, after you prepare it for writing with the assign and rewrite (or append) commands. To write to files, the syntax is: writeln(outFileID, 'hello world'). The outFileID is the file identifier, associated with a specific file by the assign command.

There are lots of options for formatting data with write and writeln. You need to know:

readln and read

The basic readln() procedure gets data from the keyboard and puts it into the variables given as parameters: readln(n1,n2).

If you specify more than one variable in the parameter list, then readln will expect them to be separated by spaces, and it will expect a carriage return after the last one. If it only gets one value when it expects two, it will keep reading input -- including carriage returns -- until it gets a second value.

You can also use read and readln to get data from a file, after you prepare it for reading with the assign and reset commands. To read from files, the syntax is: readln(inFileID, n1,n2). The inFileID is the file identifier, associated with a specific file by the assign command. Readln will expect the data in the file to look exactly like the data that would have been typed in at the keyboard if no inFileID had been specified.

Branches: if then else

if..then..else is the simplest form of branch statement in Pascal. To use it, your programming plan includes two parts:

There is another branch statement in Pascal, called the case statement. It's useful when you need the program to go one of many different routes, depending on the value of a single variable. For example, you might decide how to calculate tuition depending on whether a student is an undergraduate, a graduate, a law student, a business student, or a continuing ed student. We'll look at the syntax of case statements later in the semester.

Loops: while

The while statement is the simplest form of looping construct in Pascal. It lets you do the same thing over and over (usually with slightly different data each time). To use the while statement, your programming plan includes three parts:

There are other looping statements in Pascal. The most important is the for loop, which lets you count down or up through a specified number of loops. For example, you might calculate depreciation for 5 years using a for statement that started at 1 and worked its way up to 5. We'll get to the syntax of for loops later in the semester, but for now keep in mind that: if you're doing something a specified number of times, the for loop will be more appropriate than the while loop.

Simple boolean expressions

Boolean expressions have the value true or false.

You use Boolean expressions in the test part of a while or if statement. A typical Boolean expression is: x = 3. That's true if the variable x actually holds the value 3, otherwise it's false.

You should know the simple Boolean tests shown in Figure 3.3 of Savitch, p. 82. We'll cover more sophisticated Boolean tests and operations later in the semester.

File i/o: assign, reset, rewrite, append

Files are collections of data, on hard disks, floppy disks, CD-ROMS, tape, or anywhere else.

Files can be rigorously structured or just a stream of bits. The file type tells the Pascal program how to interpret the data it reads from a file.

The files we have worked with are all of type TEXT, which is just a stream of characters, divided (optionally) into lines. A text file can be edited with the Turbo Pascal editor, or displayed on the screen with the DOS "type" command.

MS-DOS and Unix also treat the keyboard and screen as input and output files, so the i/o (input/output) statements you've used to communicate with the user -- readln and writeln -- will also work for files... with slight modifications.

Whenever a Pascal program needs to work with a file, it has to interact with the operating system (MS-DOS) to get access to that file. The procedure is this:

Procedures and Functions

This is probably the hardest part of the first half of this course. The best way to learn it is to read the material, think up some questions, and try out some short programs on the computer.

Overview

Modularization is essential in large programs. The modularization technique that's oldest and most common, found in virtually every programming language, is the subroutine. (Your book calls it a "subprogram" but that's nonstandard usage.) In Pascal, subroutines come in two flavors: procedures and functions.

A subroutine is a lot of lines of code, with a name, like: figureTax(); Whenever you want to use those lines, you just put the name in your main program. The computer recognizes the name, and it runs the code it stands for.

Since subroutines are defined and run "outside" of the main program, the big issue in every programming language is: How do the subroutines communicate with the main program and the other subroutines?

The three most common ways for subroutines to communicate are:

Variable parameters, value parameters, and global variables

Make sure you understand the material on the handout describing how procedures and functions communicate with the main program.


RETURN to CSCI 1200 Home Page

- John Riema