home *** CD-ROM | disk | FTP | other *** search
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 25
-
-
- TURBO-LESSON 6: CONDITIONAL PROCESSING
-
- OBJECTIVES - You will learn, in this lesson, about:
-
- 1. Selection structures used for conditional processing
- 2. IF statement (one-way selection)
- 3. IF statement (two-way selection)
-
-
- 1. Selection structures used 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.
- î
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 26
-
-
- 2. 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;
-
- 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 PROG6.
-
- The first 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.
-
- Run the program using 0 for the no of computers owned.
-
- Run it again with 1 for the no of computers owned.
-
- The "No Computer!" message should print for 0 computers owned,
- but not print for 1 computer owned.
-
- ##### DO:
-
- Examine the last IF in the program.
-
- 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 ">".
-
- ##### 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.
- î
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 27
-
-
- 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.
-
- ##### DO:
-
- Look at the second IF in PROG6.
-
- This is a two-way IF. A congratulations message is printed if
- the condition is true, condolences if false.
-
- 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:
-
- 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?) '.
- î