TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 28 TURBO-LESSON 7: REPEAT STATEMENT OBJECTIVES - In lesson 7 you should learn about: 1. CHARacter variables 2. BOOLEAN variables 3. REPEAT statement 1. CHARacter variables. The reserved word, CHAR, is used to declare a variable of character type. A variable of type CHAR can be used to refer to or store a single character. VAR Alpha : CHAR; Alpha is declared to be a variable which can be used to store any of the characters in the character set. This includes the upper and lower case alphabet, the digits, 0 to 9, special characters such as #, $, %, *, and the rest of the 256 characters in the PC's character set. ##### DO: Examine PROG7. A variable named Response, of type CHAR, is used to store the character entered in response to a multiple choice question. ##### DO: Run the program several times, entering wrong responses, and the correct response, D. Also try lower case d. ##### DO: Study the first IF statement in PROG7. IF (Response = 'D') OR (Response = 'd') THEN . . . The character 'D' must be enclosed in single quotes in the program. Note that you enter the character as input data without quotes when running the program. Notice the compound condition using OR to combine two simple conditions. This condition will be true if either or both of the simple conditions are true. The correct response, D, is checked in both upper and lower case to make responding easier. ##### DO: Modify the IF statement to accept A, B, or C as the right response. Assume that the correct answer is A or B or C. (Ignore lower case responses to keep the statement short.) Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 29 2. BOOLEAN variables. BOOLEAN variables can have only two values, TRUE or FALSE. The value of a condition, TRUE or FALSE, may be stored in a BOOLEAN variable for later use. VAR Correct_Response : Boolean; In PROG7, the Boolean variable, Correct_Response, is used to store the truth value (TRUE or FALSE) of the condition in the first IF statement. If the correct character, 'D' or 'd', is entered, TRUE is stored in Correct_Response. If anything else is entered, FALSE is stored. Actually, TRUE and FALSE are stored as 1 and 0 to take up less space, but you can always view a BOOLEAN variable as having a value of TRUE or FALSE. ##### DO: Identify the condition in the second IF statement in PROG7. Since BOOLEAN variables can only have two values, TRUE or FALSE, and conditions always evaluate to the same two values, a BOOLEAN variable may be substituted for a condition. If would be permissible, but unnecessary to write the IF statement: IF Correct_Response = TRUE THEN . . . ##### DO: Modify the IF statement as indicated above and run the program to verify that the IF still works exactly as before. There is another way to assign the correct value to the variable, Correct_Response. ##### DO: Replace the FIRST IF statement in PROG7 with the following statement: Correct_Response := (Response = 'D') OR (Response = 'd'); Run the program. How does the program change when you run it? (If it doesn't do exactly as before, maybe you typed it wrong, or replaced the second IF instead of the first?) Since the condition on the right of the := must be evaluated by the computer and assigned a value of TRUE or FALSE, this value can be stored directly in a BOOLEAN variable without using the IF statement. Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 30 3. REPEAT Statement. In a previous lesson, you learned that there are three ways to sequence the execution of statements in PASCAL: SIMPLE SEQUENCE, SELECTION STRUCTURES, and REPETITION STRUCTURES. One of the statements used for REPETITION is REPEAT . . UNTIL. The form of the REPEAT statement is: REPEAT Statement 1; Statement 2; . . . Statement n UNTIL condition; Statements 1, 2, . . . , n will be executed repeatedly until the condition becomes true. This implies that the condition is checking something that can be changed by the statements 1 to n. If this is not so, the statements will be repeated forever! In PROG7, you are prompted to respond to the multiple choice question. A REPEAT statement controls the block of statements which prompt for a response, and then check the response. The block of statements will be repeated until the UNTIL condition is true. ##### DO: Change the condition in the UNTIL in PROG7 to: UNTIL 'A' = 'B'; Run the program. Does the program correctly identify a correct response? What happens then. (Use ctrl-c or ctrl-Scroll-Lock to stop the program.) ##### DO: Change the condition again to: UNTIL 'A' = 'A'; How many times are you prompted for a response? Notice that the statements in a REPEAT structure are ALWAYS executed at least once. Even if the UNTIL condition is TRUE before entering the REPEAT, the condition is not checked until the end of the statements in the REPEAT block. Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 31 ##### DO: Try to find another way to terminate the REPEAT without using the BOOLEAN variable, Correct_Response, in the UNTIL condition. Hint: Check the statement which assigns a value of TRUE or FALSE to Correct_Response. Œ