home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-08 | 45.3 KB | 1,281 lines |
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 25
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 3. PROGRAM STRUCTURE
-
- OBJECTIVES - In this lesson you will learn about:
-
- 1. Program structure
- 2. How to compile a program
- 3. How to run a program
- 4. Write and WriteLn statements
- 5. Comments - at beginning of program
- 6. Comments - at end of statement line
- 7. Comments - deactivate a section of code temporarily
-
-
- 1. Program structure.
-
- PASCAL programs are written in a certain way to make them easier to
- write and easier to read. There are two parts to any PASCAL
- program: the DECLARATIONS part and the PROGRAM BODY.
-
- The PROGRAM BODY is where the processing statements occur, and any
- data item used there must first be defined in the DECLARATIONS
- section. The form of a PASCAL program is
-
- PROGRAM Demo; (Program name - not required)
-
- USES (Units used by the program are listed here)
-
- LABEL (Labels, if used, go here)
-
- CONST (Constants are defined here)
-
- TYPE (Define your own data types here)
-
- VAR (ALL variables must be defined here)
-
-
-
- BEGIN (Processing statements occur between
-
- END. BEGIN and END.)
-
-
-
- The simplest PASCAL program is:
-
- BEGIN
- END.
-
- In the next section you will learn to compile and run a program
- using this "smallest program".
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 26
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- 2. How to Compile a Program
-
- Before you can execute a program, it has to be translated from
- Pascal to a form the computer can understand. This is the process
- called "compiling" the program.
-
- ##### DO:
-
- Type in the program:
-
- BEGIN
- END.
-
- ##### DO:
-
- Press Alt-C to pull down the Compile menu window. The highlight
- should be on the menu option: [Compile Alt-F9]. If it isn't,
- move the highlight to that line. Press Enter to start the compile
- process.
-
- The box you see on the screen now lets you know how your compile
- worked. If nothing was wrong, the word "Success" appears in the
- lower left corner and "Press any key" blinks for your attention.
-
- Let's see what happens with an incorrect program. A missing period
- after END in your program would cause a problem for Pascal since it
- looks for the period to mark the end of your program.
-
- ##### DO:
-
- Remove the period after END and compile the program again.
-
- What happened?
-
- The compile box appeared briefly, then an error message appeared at
- the top of the screen:
-
- Error 10: Unexpected end of file
-
- The Pascal compiler was still looking for the period to mark the
- end of your program when it bumped into the end of the editor work
- file where your program was stored.
-
- ##### DO:
-
- Put the period back in your program after "End". The error message
- disappears when you start editing. Compile the program again to be
- sure you have a correct program again.
-
- ##### DO:
-
- If you haven't tried the "Hot key", Alt-F9, try it now. This lets
- you bypass the compiler pull-down menu. Many of the menu options
- have hot keys listed to the right of the option in the menu.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 27
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Next, you need to learn how to "Run" or "Execute" a program.
-
-
- 3. How to Run a Program
-
- In the last section you "compiled" your BEGIN END program. Now
- let's run it to see what it does.
-
- ##### DO:
-
- Press Alt-R to pull down the Run menu. The highlight should be on
- [Run Ctrl-F9]. Press <Enter> to run the program.
-
- What did the program do? Maybe you better try that again.
-
- ##### DO:
-
- Run the program again several times. What did it do?
-
- Mine did the same thing--nothing! Since there are no statements
- between BEGIN and END nothing is exactly what it should do.
-
- Definitely not the most useful program, but it illustrates a point:
- the MAIN BODY is always required and as much of the DECLARATIONS
- section as needed to define the data used.
-
- Since this simple program uses no data, the DECLARATIONS section
- can be omitted.
-
- In the next section you start meeting the statements that are used
- to build a Pascal program.
-
-
- 4. Write and WriteLn statements.
-
- These processing statements are introduced first, because they can
- be used with messages which require no previous data declaration.
- This allows you to experiment with some programming in the MAIN
- BODY without any concern for the DECLARATIONS part.
-
- ##### DO:
-
- Insert the WriteLn statement as shown between BEGIN and END in your
- program and run the program.
- BEGIN
- WriteLn('HELLO');
- END.
-
- To see the output from your program, you need to make "Output" the
- window at the bottom of the screen. The following DO's will help
- you find your way around Turbo's windows. Notice the Function key
- helps at the bottom of the screen: F6-Switch.
-
- ##### DO:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 28
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Press F6 several times.
-
- Notice the active window (double line and highlighted window name)
- switching between top and bottom window.
-
- ##### DO:
-
- Press F6 once more if necessary to leave the bottom window active.
-
- The bottom window is either "Watch" or "Output". For now, you will
- want "Output" to occupy the bottom window.
-
- ##### DO:
-
- With the bottom window active, press Alt-F6 several times. Leave
- the "Output" window as the bottom window.
-
- ##### DO:
-
- Press F6 to switch to Edit as the active window.
-
- ##### DO:
-
- Run the program several times and watch the results in the bottom
- window.
-
- ##### DO:
-
- Change "HELLO" to "Hi" in your program and run the program again.
-
- ##### DO:
-
- If you haven't tried the Run "Hot Key", try it now: Ctrl-F9.
-
- You may have already discovered that the Turbo Environment "knows"
- when a new compile is needed. If you change your program and try
- to run it before compiling, it's OK. A compile will be done before
- running the program if it is needed.
-
- ##### DO:
-
- Change the "Hi" to "Lo" and run (Ctrl-F9) without first compiling.
-
- Did you see the compile box?
-
- ##### DO:
-
- Change the "Lo" back to "Hi" and run the program several times as
- you watch for the compile box.
-
- When did the compile happen? Only the first time you ran the
- program after changing it.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 29
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Now let's explore the difference between the "Write" and WriteLn"
- statement.
-
- ##### DO:
-
- Remove the "Ln" from your WriteLn statement. The program should
- appear:
-
- BEGIN
- Write('Hi");
- END.
-
- ##### DO:
-
- Run the program several times.
-
- What do you think the "Ln" part of the WriteLn statement does? If
- you're not sure, check out the next DO.
-
- ##### DO:
-
- Put the Ln back--change "Write" to "WriteLn". The statement should
- be: WriteLn('Hi'); Run the program several times.
-
- You're right! The Ln adds a linefeed--moves the cursor to the left
- side of the screen at the next line.
-
- NOTE: The way I write WriteLn is not required--you could use
- writeln or WRITELN or even wRiTeLn if you cared to. I prefer
- WriteLn because it provides a visual reminder of the two actions of
- this statement: Write (print whatever is in the parenthesis) and
- Ln, provide a Linefeed.
-
- ##### DO:
-
- Load PROG3 and use the editor to examine the program. A part of
- PROG3 is printed below:
-
- BEGIN
- WriteLn('* * * * * * * * * * * * * * * * *'); {Note that }
- WriteLn('* *'); {comments }
- WriteLn('* TURBO-LESSON 3 *'); {placed at the }
- WriteLn('* Edited, Compiled, Executed *'); {end of a }
- WriteLn('* by *'); {statement line}
- WriteLn('* (put your name here) *'); {must be }
- WriteLn('* *'); {terminated on }
- WriteLn('* * * * * * * * * * * * * * * * *'); {the same line.}
-
-
- In this section of PROG3, the WriteLn statement is used to display
- messages.
-
- ##### DO:
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 30
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- RUN the program to see the messages displayed on the screen. (Use
- Alt-F5 to see the full screen of output. Press Alt-F5 again to get
- back to the split screen so you can edit the program.)
-
- ##### DO:
-
- Use the editor to insert your name in the line indicated and run
- the program again.
-
- To illustrate the difference between WriteLn and Write statements,
- another portion of the program is included below:
-
- WriteLn;
- Write ('Check carefully when ');
- Write ('you run this program. ');
- Write ('How many lines ');
- WriteLn('are printed');
- WriteLn('by this last set of Write and WriteLn statements?');
-
- The "Ln" in WriteLn may be viewed as a linefeed or "return the
- cursor to the beginning of the next line".
-
- Any message, or other data item within the parentheses following
- WriteLn is printed first, the "Write" part, then the cursor is
- moved to the next line, the "Ln" part.
-
- Notice the first WriteLn in the program segment above. There is
- nothing in parentheses to print, so this statement is a way to
- print a blank line.
-
- The next three Write statements write on the same line. Then the
- following WriteLn statement writes it's message and causes the
- cursor to return to the beginning of the next line.
-
- The last WriteLn displays it's message on the line where the cursor
- is positioned, and returns the cursor to the next line.
-
- ##### DO:
-
- Add some WriteLn statements after the BEGIN to insert several blank
- lines at the top of the screen to push the boxed message down the
- screen. (Don't forget to put a semicolon after each WriteLn you
- add).
-
- Run the program. Did it work right?
-
-
- 5. Comments - at the beginning of program.
-
- There are two ways to indicate comments in a PASCAL program. The
- curly brackets, { }, may be used to enclose comments. The original
- comment delimiters, from standard PASCAL are (* *).
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 31
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- At the beginning of a program, or anywhere that multiple line
- comments are needed, it is convenient to mark the whole block of
- comments with the delimiters at the left margin. This makes it
- easy to insert or delete comments within the block.
-
- ##### DO:
-
- Add some comments at the beginning of the program. Try both types
- of comment delimiters, { }, and (* *). Be sure your comments are
- not "inside" any comments which are already in the program.
-
-
- 6. Comments - at the end of a statement line.
-
- Look at the comments following the WriteLn statements in PROG3.
- Short comments may be inserted to clarify the action of one or more
- statements. NOTICE CAREFULLY that each of these comments has to be
- completed on the same line, if the next line is another active
- processing statement.
-
- ##### DO:
-
- Add a short comment, {Blank Line}, after one of the WriteLn
- statements which prints a blank line.
-
-
- 7. Comments - deactivate a section of code.
-
- Having two sets of comment delimiters makes it possible to use one
- set for most comments, while reserving the second set for
- "deactivating" sections of statements when debugging a program.
-
- I prefer to use the { } for most comments, and (* *) for
- deactivating code. You should keep the delimiters used for
- commenting out code in the left margin where they are very visible.
-
- ##### DO:
-
- Deactivate the three WriteLn statements following the one which
- prints
-
- TURBO-LESSON 3.
-
- Since { } are in use at the end of these lines you should use the
- other set of comment symbols, (* and *) to deactivate the
- statements.
-
- Run the program to see the results.
-
- DON'T BE AFRAID TO EXPERIMENT WITH THE SAMPLE PROGRAMS. YOU CAN
- ALWAYS GET ANOTHER COPY, FROM YOUR BACKUP DISK (you did make one,
- didn't you), FROM THE .BAK FILE PROVIDED BY TURBO, OR FROM THE
- ORIGINAL FILE IF YOU HAVEN'T SAVED AN EDITED COPY.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 32
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 4. DECLARATIONS, INPUT
-
- OBJECTIVES - In Lesson 4 you will learn about:
-
- 1. The DECLARATIONS part of a program
- 2. VAR declaration
- 3. Input using the ReadLn statement
- 4. DEBUG: Tracing Statements
- 5. DEBUG: Watching Variables
- 6. Integer variables
-
-
- 1. The DECLARATIONS Part of a Program.
-
- You learned in the previous lesson that there are two main parts to
- any PASCAL program: DECLARATIONS, and MAIN BODY. The various
- entries in the DECLARATIONS section define the data items used in
- the processing in the MAIN BODY. Not all declaration entries will
- occur in every program, but only the ones needed to support the
- processing.
-
- Standard PASCAL requires that declarations occur in the order
- specified and each type of declaration may occur only once.
-
- TURBO is a little more forgiving in some respects than standard
- PASCAL (declarations don't have to occur in the specified order,
- and the same type of declaration may occur more than once).
-
- The various types of declaration entries will be introduced as
- needed in the sample programs. Only the VAR entry will be used in
- this program.
-
-
- 2. VAR Declaration.
-
- All variables, (spelled A-L-L, no exceptions), must be defined
- before they are referenced by processing statements. The VAR entry
- is used to define variables. The form of the entry is:
-
- variable-name : type;
-
- The variable-name may be one or several variable-names separated by
- commas. The type may be a predefined type, such as Integer, or a
- type you have constructed using the predefined types. The colon
- must occur between the variable-name(s) and the type.
-
- Extra spaces are acceptable to allow more readable format. Below
- are some VAR entries:
-
- VAR
- i,j,k : Integer;
- Inkey : Char;
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 33
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Rate : Real;
- Count : Integer;
-
- (The example above includes types not yet discussed, to illustrate
- the form of the VAR entry.)
-
- ##### DO:
-
- Look at PROG4.
-
- (You know from earlier lessons how to load the program and use the
- editor to look at the program.) A segment of PROG4 is:
-
- VAR
- Number : Integer;
-
- The VAR entry above defines a variable called "Number" to be of
- type "Integer". This means the computer must set up a memory
- location large enough to store an integer, which can be accessed by
- referring to the variable name "Number".
-
- Notice the variable, Number, is later referenced in the processing
- statements.
-
- ##### DO:
-
- Add an integer variable called "Age" to Prog4. This will be used
- later in this lesson. You can either add the new variable to the
- declaration of Number
-
- Number, Age : Integer;
-
- or add another line with the Age declaration
-
- Number : Integer;
- Age : Integer;
-
- Compile the program to be sure you haven't made an error.
-
-
- 3. Input Using the ReadLn Statement.
-
- ReadLn is the statement used to input variables. The form of the
- statement is:
-
- ReadLn(var_1, var_2, . . . ,var_n);
-
- When the statement is executed, the computer will wait for you to
- type values for the variables, separated by one or more spaces,
- followed by depressing the enter key.
-
- Part of PROG4 follows:
-
-
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 34
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- BEGIN
- Write('Enter a number (no decimals please): ');
- ReadLn(Number);
- WriteLn; { Display one blank line }
- WriteLn('Number: ',Number); { Display the number entered }
- END.
-
- The Write statement provides a prompt before the ReadLn accepts
- Number. Notice that ReadLn does not provide a "?" or any other
- prompt. The programmer must provide any prompting required.
-
- The use of the Write statement for prompting, instead of the
- WriteLn, keeps the cursor on the same line so the input will occur
- right after the prompt message.
-
- The ReadLn accepts the number you type and stores it in the memory
- location which the computer has set aside for the Integer variable,
- Number.
-
- ##### DO:
-
- Compile and run PROG4. When prompted, type 12 and enter. Run it
- again and enter -34.
-
- ##### DO:
-
- Using the statements of PROG4 as an example, expand the program to
- do the following:
-
- (1) Write a prompt to 'Enter your age'.
-
- (2) Read a value for age to be stored in the integer variable,
- Age, which you added to the VAR declarations in the previous
- section.
-
- (3) Write a message which prints the age entered, in a manner
- similar to the way Number was printed.
-
- Run the program.
-
-
- 4. DEBUG - Tracing Statements
-
- Turbo Pascal 5.0 has a new DEBUG feature to help you find problems
- in programs. It is also an excellent way to learn about Pascal
- programming. You can execute your program one statement at a time
- with TRACE to see what each statement does.
-
- ##### DO:
-
- Press F7 to begin TRACEing your program. Note the highlighted line
- in your program. The highlighted statement will be executed when
- you press F7 again.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 35
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- As you press F7 to trace each statement, first look at the
- statement and predict what you think will happen.
-
- ##### DO:
-
- Continue pressing F7, watching the output in the bottom window.
-
- ##### DO:
-
- Trace through the program again to be sure you understand what the
- TRACE feature is doing for you.
-
-
- 5. DEBUG - Watching Variables
-
- Another of the DEBUG features allows you to WATCH the values stored
- in the computer's memory. TRACE and WATCH used together form a
- very effective debugging tool.
-
- ##### DO:
-
- Press F6 to make the bottom window active. Press Alt-F6 to switch
- from Output window to Watch window. Press F6 again to make the top
- (Edit) window active again.
-
- Now let's add a variable to the Watch window.
-
- ##### DO:
-
- Press Alt-B to pull down the Break/Watch window. Notice the "Hot
- Key" for Adding a Watch is Ctrl-F7.
-
- ##### DO:
-
- With the option "Add Watch Ctrl-F7" highlighted, press Enter.
- The Add Watch box appears on the screen. This is where you enter
- the name of the variable you want to watch.
-
- ##### DO:
-
- Type Number in the box and press Enter.
-
- The variable, Number, should now appear in the Watch window at the
- bottom of the screen.
-
- ##### DO:
-
- Trace the program. Watch the variable Number as you Trace the
- statement, ReadLn(Number).
-
- There is another variable in the program. You added the variable,
- Age, in an earlier exercise. Let's add Age to the watch window.
- Notice there is a shortcut.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 36
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- ##### DO:
-
- Place the cursor on the word Age in your program. Press Ctrl-F7.
- The word with the cursor appears in the Watch box. You don't have
- to type it, just press enter to accept it.
-
- ##### DO:
-
- Trace the program again, watching both variables, Age and Number.
-
-
- 6. Integer Variables.
-
- Integers are counting numbers, with no decimal points. They may be
- positive or negative. In TURBO the range of Integers permitted is:
-
- -32768 to +32767
-
- Integers are used for subscripts, indexes, counting, input and
- output of such things as counts, limits, menu choices. Decimal
- numbers (Real type, discussed later), are needed for such things as
- dollar amounts and calculations. When decimal numbers are not
- actually needed, Integers should be used since they are easier to
- use and take less memory.
-
- ##### DO:
-
- Run or Trace PROG4 again, this time entering data which will cause
- errors.
-
- When prompted to enter a number, enter the letter A instead.
-
- What happened?
-
- A message appears to indicate the nature of the problem.
-
- Note that the cursor is positioned at the statement which caused
- the error. (In this case, it is not the statement which caused the
- problem, but the type of data entered.)
-
- ##### DO:
-
- Run or trace the program again, entering too large a number, 88888.
- Note that no error message appears but the result is wrong. What
- number appears in the watch window? Not 88888. Run it again with
- too small a number, -55555.
-
- When you use Integer variables, you have to be sure they will be
- within the acceptable range or errors will occur.
-
- NOTE: IN THE LESSONS THAT FOLLOW, BE SURE TO MAKE USE OF TRACE AND
- WATCH. WHEN INSTRUCTED TO "RUN" A PROGRAM, YOU COULD TRACE IT.
- TRACING TAKES A LITTLE LONGER, BUT YOU WILL PROBABLY LEARN MORE
- THAT WAY.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 37
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 5. INTEGER EXPRESSIONS
-
- OBJECTIVES - In lesson 5 you will learn about:
-
- 1. Assignment statements
- 2. DEBUG: Removing Watch Variables
- 3. Integer expressions
- 4. Problems with expressions
-
-
- 1. Assignment Statements.
-
- A large part of computer processing is accomplished by storing
- numbers, strings, and other data objects in memory locations in the
- computer.
-
- There are several ways to store a value in memory in Pascal:
-
- (1) use an Input statement (ReadLn),
- (2) use an Assignment statement,
- (3) use a Procedure or Function (in later lessons).
-
- The Assignment statement has the following form:
-
- Variable := Expression;
-
- An example: A := B + C;
-
- The way it works: The computer evaluates the expression on the
- right side of the replacement operator, :=, and stores the
- resulting value in the memory location named by the variable on the
- left.
-
- In the example above, the computer would obtain whatever value is
- currently stored in the memory location called B, add that value to
- the value it finds in the memory location called C, and store the
- sum in the memory location, A. If B holds the value 3, and C holds
- the value 4, then 7 would be stored in the memory location called
- A.
-
- ##### DO:
-
- Use the editor (Alt-F, New) to enter the following short program
- (omit the comments, if you like):
-
- PROGRAM ABC;
-
- VAR A, B, C : Integer;
-
- BEGIN
- A := 6; {Assign the value 6 to the memory location, A}
- B :=7; {Assign 7 to B }
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 38
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- C := A + B; {Add the values in A and B, Store in C }
- WriteLn ('A=',A, ' B=',B, ' C=',C);
- END.
-
-
- 2. DEBUG - Removing Watch Variables
-
- There are some "Leftover" variables in the watch window. You will
- need to get rid of them and add this program's variables.
-
- ##### DO:
-
- Press Alt-B to get the Break/Watch menu. Press R to "Remove all
- watches".
-
- ##### DO:
-
- Use Ctrl-F7 to add A, B, and C to the watch window.
-
- ##### DO:
-
- Press F7 once to start tracing. What values do the variables A, B,
- and C have?
-
- Pascal doesn't preset variables to zero as some languages do.
-
- ##### DO:
-
- Trace the program, watching the values of A, B, and C.
-
- ##### DO:
-
- Trace the program, watching the variables and expressions.
-
- ##### DO:
-
- Change the statement A:=6 to A:=3 and run the program again.
-
- Note that you have just written a complete PASCAL program - from
- here on, you will just be adding new features in each lesson to
- enable you to write more complicated programs. (It didn't run?
- Use the error message to help correct any errors. If the error
- message doesn't make sense, try looking for misplaced or omitted
- semicolons. Also note that the assignment statement uses the
- compound symbol, ":=" and not "=".).
-
-
- 3. Integer Expressions.
-
- Integer expressions are composed of integer variables, constants,
- and operators. The operators used with integer expressions are:
-
- +, -, *, div, mod.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 39
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- The + and - have their usual meaning, addition and subtraction.
- The * indicates multiplication. Division of integer numbers is
- done with div and mod.
-
- ##### DO:
-
- Examine PROG5.
-
- Two numbers, which you enter, are added, subtracted, and one is
- cubed using the multiply operator, "*". WriteLn is used to print
- out the results.
-
- Notice that expressions may be calculated in an assignment
- statement,
-
- I_Cubed := I * I * I;
-
- or calculated in a WriteLn statement,
-
- WriteLn('I - J ', I - J);
-
- ##### DO:
-
- Remove the variables from the watch window (Alt-B, R).
-
- ##### DO:
-
- Add the following to the Watch window. (Add them in reverse order
- if you prefer to see them as listed below.)
-
- I
- J
- I + J
- I - J
- I_Cubed
-
-
-
- ##### DO:
-
- Trace the program.
-
- Note that the ReadLn statement will attempt to read 2 values. The
- values should be typed with a space between them, not a comma.
-
- Enter the values 2 and 5.
-
- Check all the results. Are they all correct?
-
- ##### DO:
-
- Run the program again, this time entering -3 and 5.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 40
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Is everything correct again? Notice the negative cube of -3 is as
- expected, -27. (Some incorrect negative cubes will appear a little
- later in this lesson.)
-
- The division operators, div and mod are not used in PROG5. To see
- how they work,
-
- ##### DO:
-
- Just before the END of PROG5, edit in the following statements:
-
- WriteLn('I div J = ', I div J);
- WriteLn('I mod J = ', I mod J);
-
- Run the program, entering the values 5 and 3.
-
- Did you get the results expected? Is 5 divided by 3 really 1 and
- not 1.666?
-
- ##### DO:
-
- Add these two statements at the end of the program:
-
- WriteLn(I,' divided by ', J, ' = ', I div J);
- WriteLn(' with a remainder of ', I mod J);
-
- Run the program with the values 5 and 3.
-
- Often, when working with integers, it is useful to know one or both
- of these components of the division. If you really want the
- decimal result of the division, the slash, /, could be used with
- integers.
-
- ##### DO:
-
- Add I/J to the watch window.
-
- Run the program with the values 5 and 3.
-
- Note the result of division using the slash is the usual result.
-
- Observe that you can watch expressions that are not in the program.
- I and J have values in the program so you can look at I/J even
- though the expression I/J does not occur in the program.
-
- ##### DO:
-
- Before going on, try to improve the "readability" of the output by
- adding some blank lines between the different parts of the output.
- Use Alt-F5 to see the full Output screen. For example, you might
- add a blank line just before printing the cube of the number.
- Remember, whenever you want to print a blank line, use WriteLn
- without any parentheses.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 41
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- 4. Problems with Expressions.
-
- You should be aware of the possibilities for various types of
- errors involving expressions. If you find this section a little
- heavy going, come back to it later. It is not essential for
- understanding the lessons that follow. However, it will help you
- later when you are debugging your own programs if you understand
- the ideas presented here.
-
- First, an easy to detect error.
-
- ##### DO:
-
- Declare K to be an integer variable. (Add K to the list of
- integers in the VAR section.)
-
- Add this statement at the end of the program:
-
- K := I / J;
-
- Run the program. How did it go?
-
- This error, "Type mismatch", is easy to find, since the compiler
- finds it. The reason for the type mismatch is that the result of
- the division using the "/" is a real number (covered in a later
- lesson). The variable, K, is an integer. A real number can't be
- stored in an integer memory location. (Why not? One reason: a
- real number takes more memory space than an integer.)
-
- A second type of error, is illustrated in the following:
-
- ##### DO:
-
- First, remove the statement that caused the error (K := I/J), then
- run the program using the values 31 and 5.
-
- Check the results of the cube of I. Is it correct?
-
- O.K., but notice the cube of I, 29791, is approaching the upper
- limit of integer variables, 32767.
-
- What will happen if you enter 32 and 5? (The correct cube of 32 is
- 32768, just 1 too large to fit as an integer.)
-
- Try it!
-
- ##### DO:
-
- Run the program using the values 32 and 5.
-
- The computer, known for its reliability, informs you:
-
- The cube of I = -32768
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 42
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Now, you know the sign is wrong - positive numbers do not produce
- negative cubes. But look at the number, -32768. Correct number
- with the wrong sign? (The cube of 32 is 32768.)
-
- ##### DO:
-
- Run the program again with the values 33 and 5. (The correct cube
- of 33 is 35937.)
-
- The cube of I = -29599
-
- Wrong number! Wrong sign! So why does the computer go merrily on
- its way - giving you these wrong answers?
-
- The computer is very good at detecting errors in the format of
- program statements, missing declarations, wrong punctuation.
-
- There are other types of errors that are more difficult to detect.
-
- It is up to you, the programmer, to find ways to keep these errors
- from going unnoticed. For now, you need to be aware that these
- problems can occur.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 43
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
-
-
- TURBO-LESSON 6. CONDITIONAL PROCESSING
-
- OBJECTIVES - You will learn, in this lesson, about:
-
- 1. Selection structures used for conditional processing
- 2. DEBUG: Goto Cursor
- 3. IF statement (two-way selection)
- 4. IF statement (one-way selection)
-
-
- 1. Selection Structures for Conditional Processing.
-
-
-
- There are three types of statement sequencing used in PASCAL:
-
- 1. SIMPLE SEQUENCE. One statement follows another with no
- branching.
-
- 2. SELECTION STRUCTURES. Based on a condition, supplied by the
- programmer, the next statement to execute is chosen from two
- alternatives (IF statement) or chosen from many alternatives (CASE
- statement).
-
- 3. REPETITION STRUCTURES. A group of program statements may be
- repeated more than once, dependent on a condition supplied by the
- programmer. The repetition statements are WHILE, REPEAT, and FOR,
- included in later lessons.
-
- The Selection statement, IF .. THEN .. ELSE, is illustrated in this
- lesson, the CASE statement appears in a later lesson.
-
-
- 2. DEBUG - Goto Cursor
-
- In previous lessons you traced through each line of the programs.
- As programs grow larger you probably won't want to trace the entire
- program. You will often want to run a program without tracing up
- to a certain point (where you suspect a problem exists), then have
- the program pause to let you start tracing at that point. The Goto
- Cursor debug feature let's you do just that.
-
- First, let's see what the first five statements of PROG6A do.
-
- ##### DO:
-
- Set the bottom screen to Output and trace the first 5 statements.
- Then use Ctrl-F2 to reset the program so you can start the program
- from the beginning again.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 44
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- To see how the Goto Cursor works, we'll assume you want to run the
- first five statements without tracing, then stop at the IF
- statement to begin tracing.
-
- ##### DO:
-
- Move the cursor to the first line of the IF statement. (Anywhere
- in the line is OK.)
-
- This marks the place you want the program to pause so you can take
- control.
-
- ##### DO:
-
- Press F4 to run the program up to the statement with the cursor.
- Input values at the prompts.
-
- Notice the program stops at the statement where you positioned the
- cursor. This statement will be highlighted.
-
- Study the next section to find out what to expect of the IF
- statement. The DO activities will let you see the IF in action.
-
-
- 3. IF Statement (Two-way Selection).
-
- The two-way IF causes one of two alternative statements to be
- executed, based on whether the condition is TRUE or FALSE. The
- form of the statement is:
-
- IF condition THEN Statement_1 ELSE Statement_2;
-
- If the condition is TRUE, the statement following the THEN is
- executed. If the condition is FALSE, the statement following the
- ELSE is executed.
-
- The condition is an expression or comparison which the computer can
- evaluate as TRUE or FALSE. Examples of a condition:
-
- 7 < 10 TRUE
-
- I < 10 TRUE, if the memory location named I holds a
- value less than 10, otherwise FALSE.
-
- NOT(7 < 10) FALSE (7 < 10 is TRUE, but NOT reverses the
- value to FALSE).
-
- ##### DO:
-
- Look at PROG6A.
-
- The IF statement in PROG6A is a two-way IF. A congratulations
- message is printed if the condition is true, condolences if false.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 45
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- The condition is (Have >= Want). This condition is true if the
- number you enter for computers owned is greater than or equal to
- the number you enter for computers you would like to have.
-
- ##### DO:
-
- Run the program several times, experimenting with various input
- values.
-
- ##### DO:
-
- Trace the IF statement action several times. Try to predict which
- statement will be highlighted next based on your input values.
-
- ##### DO:
-
- Switch the bottom window to Watch and add the two variables, Have,
- and Want to the watch window.
-
- ##### DO:
-
- Watch the values of Have and Want as you trace the program again.
-
- You could also watch the CONDITION part of the IF statement. This
- should convince you that the only values a condition can have are
- TRUE and FALSE.
-
- ##### DO:
-
- Add (Have >= Want) to the watch window. Trace the program several
- times with input values chosen to make the condition sometimes
- TRUE, sometimes FALSE.
-
-
- 4. IF Statement (One-way Selection).
-
- The one-way IF is really a special case of the two-way IF, where
- the second alternative is to do nothing, just go on to the next
- statement. The form of the IF statement is:
-
- IF condition THEN statement;
-
- ##### DO:
-
- Observe PROG6B.
-
- The second IF statement is a one-way selection. If the condition
- is true, the WriteLn statement will be executed. If the condition
- is false, the WriteLn will be ignored.
-
- ##### DO:
-
- Run (or Trace) the program using 0 for the number of computers
- owned.
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 46
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- Run it again with 1 for the number of computers owned.
-
- The "No Computer!" message should print for 0 computers owned, but
- not print for 1 computer owned.
-
- Remember you can use Alt-F5 to see your output screen when the
- bottom window is the Watch window.
-
- NOTE: At this point you know what the TRACE feature and Goto Cursor
- feature can do for you. In the sections and lessons to follow I
- will usually tell you to "Run" a program. Remember you can always
- stop to Trace parts of the program. Tracing a step at a time while
- observing variables or expressions in the Watch window will usually
- provide extra learning opportunities for you.
-
- ##### DO:
-
- Examine the third IF statement, added in PROG6C.
-
- This is a one-way IF with a slightly more complicated condition.
- The condition contains an integer expression, (Want - Have). The
- computer first evaluates the integer expression to get a number to
- compare with the 2 on the right side of the ">". For example, if
- you want 3 more computers than you have, the "Greedy" message
- should print.
-
- ##### DO:
-
- Add the expression, (Want - Have) to the Watch window.
-
- ##### DO:
-
- Run the program several times with different input to see the
- effect of this IF.
-
- Also note the misspelled "Aren''t" in the message. The double
- apostrophe is used in the message to represent a single apostrophe.
- If a single apostrophe were used, it would appear to be the end of
- the message.
-
- ##### DO:
-
- Try your hand at writing an IF statement to do the following:
-
- If the number of computers owned is more than the number of
- computers wanted, print a message 'Send extra computers to (put
- your name here?) '.
-
- *********************** SHAREWARE REMINDER ***********************
-
- Congratulations! You have finished the first 6 of 16 lessons. You
- should be able to decide now whether these lessons are helping you
- get started with Pascal. If you decide to continue, remember that
- payment for the lessons is expected. This is SHAREWARE, not
-
- î TURBO-LESSONS - A Pascal Tutorial Version 5.0 Page 47
- Copyright 1989 Lyle M. Faurot Lessons 1-16: $10.00
-
-
-
- freeware. If you missed the "Newspaper" concept of primary value
- in "first use", please go back and read the SHAREWARE notice at the
- beginning of the lessons.
-
- Thank you for your support for TURBO-LESSONS and SHAREWARE.
-