NOTE: Also review the Pascal notes for the Midterm.
Review the handout and the book on functions, procedures, parameters and passing information between a subroutine and the program that calls it. Can you say "shared global variable?" I can...
Briefly review the material about Jump Statements in Turbo Pascal.
Use := for assignment. Use = to test equality.
Use = in const and type declarations. Use : in var declarations.
Don't use a semicolon before 'else' in if-then-else, but you can put one before 'else' in the case statement.
Put a semicolon after the 'end' of a procedure. Put a period after the 'end' of a program.
Turn around twice and whistle before sitting down to use the computer on days with an R in their name.
Booleans are variables that hold one of two values: true or false. They can be combined with and, or, not, and parentheses. You will usually name your Boolean variables something like 'found' or 'sorted' or 'graphicsOK'. But remember that the compiler can't read English. You have to use the Boolean variable in a way that will make your program work, no matter what the variable is called.
Tests for equality and inequality (x=3, y<2, etc.) also return a Boolean value, and these also can be combined with parentheses, not, and, or, etc. Booleans like these are tested in if-then, while-do, and repeat-until statements.
A case statement is similar to an if-then, but case statements they let the program choose among more than two options. You should know the syntax of a case statement (that is, how to write one). Remember the end that terminates it and the optional else part. Also note that the label tested by the case statement must be an ordinal type, typically integer or character or a subrange -- it cannot be real.
A for loop lets you repeat a statement many times. The 'statement' repeated can be a single line, or it can be a compound statement that includes many lines, marked off with begin and end. You should know the syntax of a for loop, including the downto option. Be able to tell how many times a loop will execute.
For-loops are an alternative to while-do loops. You would use a while-do loop if, when the program first enters the loop, it is hard to predict how many times the loop will execute. An example is reading a list of items from a file where you don't know how many items the file contains. A for-loop, on the other hand, should be used where you can predict exactly how many times the program needs to loop. An example of this is calculating the sum of all the numbers in an array, where you know the exact array size.
Note that anything you can do with a for-loop could also be done with a while-loop that includes a counter. However, for programming readability, you should use for- loops when you are going to step through a known number of cases.
The Turbo Pascal and ISO Pascal specs both state that, if you write a for-loop as:
for i := x to y do
begin
{ code not shown }
end;
then: (1) The values of x and y are evaluated only once, at the beginning of the first loop.
(2) If the value of i is changed by an assignment statement within the loop, then the
behavior of the loop is undefined. (3) Finally, the value of i after the loop terminates is
undefined.
Arrays are lists of similar variables, all of the same type. For example, you might have a list of 100 scores or 31 temperatures. You should know how to declare an array, both as a named and an anonymous type, and how to read an array declaration.
You should know how to read and write the value of an array variable. For example, just use the array element score[3] the way you would use the variable score3. You should also know some of the simple algorithms used with arrays, such as linear search and finding the sum of all the elements in the array.
If you read an array variable outside the defined range of the array, then you are reading data that is either garbage or in use by another part of the program. If you write to an array variable outside the defined range, you may cause the program or even the entire computer to crash. Range checking is an option in Turbo Pascal, but it slows the program is doesn't really help the user much -- it just causes the program to crash when a reference is out of bounds.
Arrays are usually passed to procedures as var parameters, even if the procedure only needs to read data from the array. This is because it would take too long to copy the entire array into the procedure's parameter stack, and large arrays might even cause stack overflow.
A string, in Pascal or any other language, is basically an array of characters. The problem with strings is that they come in all different lengths, and you never know ahead of time how long each one will be. (The name 'bob' is three characters long, but 'robert' is six characters, etc.)
Code that works with strings, such as writeln or < or even :=, needs to know how long each string is. There are three common solutions to this problem:The other problem with strings coming in variable lengths is that it's hard to store them efficiently. If you declare every string variable as an array of 255 characters, then only use a few characters at the beginning for most strings, you are wasting huge amounts of space in RAM. When you have a lot of strings, like you would in a database or word processing program, this becomes a real problem. Three of the possible solutions to this problem are:
Records are data structures that hold several variables, like arrays. However, unlike arrays, the variables in records can be of different types. For example, a single record might hold a student's name, ID, total hours, and tution. Since each record is defined as a named type in Pascal, you can have arrays of records. For example, you could have an array of 100 student records.
Chapter 11 in Savitch tells how to define a record. You are not required to know how for this class. However, you should know how records and fields are typically referred to in the code, in case you see Pascal code that uses them. If the record is named NewStudent, and it has fields named Name and ID, then you can refer to those in code as:
NewStudent.Name := 'joe smith'; NewStudent.ID := 2234 writeln(NewStudent.Name, ' ', NewStudent.ID);Most other programming languages and database programs use the same syntax (record_name.field_name) to refer to their records. In Pascal, you can also do things in code with the entire record:
StudentTemp := NewStudent
The obvious use of records is for databases -- students, customers, products, etc. However, records are also useful in many other programming applications. An operating system will use record structures to keep track of files, users, and passwords. A graphics program will use records to keep track of graphic objects. A graphical user interface uses records to keep track of menus. Even things as simple as information about the mouse-pointer's current shape and position are typically held in a record structure, so all the information can be passed to a procedure as a single variable.
Turbo Pascal uses memory on a PC in roughly the same way that any programming language does. The memory map looks something like this:
----- . . . . heap (open memory used by getMem/Malloc/etc.) ----- stack (parameters of current procedures) . . ----- locals (variables and constants of current procedures) ----- globals (variables and constants of main program) ----- . . executable (machine code of program and all procedures) . . ----- run-time/OS (code use to interact with the operating system) -----
One thing you should notice is that there are lots of uses of memory, all jammed closely together (the 'stack' is only 16K unless you tell the compiler you want it to be larger). This means that if you write past the end of an array in the main program space, you are probably writing onto some other variable used by the main program or a procedure. You may even manage to damage the operating system and crash the machine.
Also, since the stack has limited size, passing large arrays as procedure value parameters will cause stack overflow. (The stack grows down, so it will bump into the space for local variables.) That's one reason that you should usually pass arrays as var parameters.