Previous | Table of Contents | Next |
5.2.3.4. while Loops
A loop that executes while a condition is true is referred to as a while loop. A while loop is a pretest condition loop, which means it tests the condition before it executes the code. This means that if the condition is false approaching the while loop, the code in the while loop will never be executed. Also, if the condition in this loop reflects an index variable, you must be sure to have it initialized before entering the loop and increment it or decrement it before the loop re-cycles.
Demonstrated in Listing 5.9 is the method you would have to use to go from ranges in an index (the popular question How do I do a for loop while incrementing by a number other than one?) by a while loop, noting that in this construct, the index variable can be changed in the loop. This listing is the equivalent program of Listing 5.8, minus the abc section.
Listing 5.9. A demonstration of a while loop.
program fig9; var i: integer; begin write(I can count down from 10: ); i := 10; while i > 0 do begin write(i, ); i := i - 1; end; writeln; end.
Here, what is performed in the while loop is exactly what is done in the for-downto loop presented in Listing 5.8. The generic function of a while loop is to continue the statements encompassed by the while loop while the condition specified is true. If the assignment to i is changed to i := 0, the statements in the while loop will never be executed.
5.2.3.5. repeat Loops
A loop that executes statements until a condition is true is referred to as a repeat loop in Turbo Pascal. This loop always executes once no matter what condition is placed on the repeat loop; therefore, it could be referred to as a posttest condition loop. As with a while loop, any index variables used for the condition must be initialized before the loop is entered.
Listing 5.10 shows a program that performs the same actions as Listing 5.9, using a repeat loop. Note the difference in the control statement as well as the location of the control statement. As Wirth intended Pascal to be a teaching language with logic behind how the control structures are expressed, the way the constructs are written indicates the actions that happen. Because a repeat loop checks the condition after the statements are executed, the condition appears afterwards, whereas a while loop shows the control statement beforehand.
Listing 5.10. A demonstration of a repeat loop.
program fig10; var i: integer; begin write(I can count down from 10: ); i := 10; repeat write(i, ); i := i - 1; until i < 1; writeln; end.
5.2.3.6. Boolean Logic and Control Structures
This section presents the use of boolean operators with the control structures listed previously. They include AND, OR, and NOT keywords. Generally, AND and OR would be used to indicate multiple conditions, whereas NOT negates a condition. The following is a list of standard boolean algebra:
AND | OR | NOT |
---|---|---|
True and true = true | True or true = true | Not (true) = false |
True and false = false | True or false = true | Not (false) = true |
False and true = false | False or true = true | |
False and false = false | False or false = false | |
In the order of operations, these operators are placed above all arithmetic operations and comparison operations. If a true comparison between two conditional statements is desired, parentheses must be used, as the AND or OR will be evaluated between the items nearest the word as boolean types and not the results of the two conditions as might be desired. In Listing 5.11, multiple conditions are demonstrated in the rewrite of Listing 5.7, which was the case statement grades example, to use if statements. Here, you begin to see the easily sprawling code that if statements can begin to generate.
Listing 5.11. A demonstration of control statements including boolean logic.
program fig11; { this is a grade parser, which uses the general college grading rules: 90-100 : A, 80-89 : B, 70-79 : C, 60 - 69 : D, 0-59 : F } var grade: integer; begin write(What is your grade? ); readln(grade); if (grade > 100) or (grade < 0) then writeln(The number was not between 0 and 100. , Please re-enter your number.) else if grade >= 90 then writeln(You earned an A) else if grade >= 80 then writeln(You earned a B) else if grade >= 70 then writeln(You earned a C) else if grade >= 60 then writeln(You earned a D) else writeln(You earned an F); end.
In any event, any control structure can be nested within another control structure numerous times deep in a proper manner with Turbo Pascal.
5.2.3.7. Boolean Logic Evaluation
Note that the manner Turbo Pascal defaults to is what is called short-circuit evaluation. When the final result of a multiple-condition boolean statement is apparent in short-circuit evaluation, it ceases to evaluate the particular condition statement. This behavior may or may not be desirable, if a control statement with multiple conditions does not work. The compiler directive ({$B+}) at the top of a source program sets the compiler so it fully evaluates all condition statements.
Previous | Table of Contents | Next |