Previous | Table of Contents | Next |
This section describes the various control structures that are available in Turbo Pascal. They include statements of conditional execution such as if statements and case statements, loop control structures such as for loops, while loops, and repeat loops, and a description of boolean logic in Turbo Pascal.
5.2.3.1. if Statements
These are the basic statements for use in conditional execution of a statement. The format of an IF statement is shown in Listing 5.5, where a writeln statement is executed depending on the number entered at the beginning. Multiple conditions may be placed on if statements, which are described at the end of this section.
Listing 5.5. A demonstration of an if statement.
program fig5; var entered: integer; begin write(Enter a number: ); readln(entered); if entered > 10 then writeln(Entered was greater than 10); end.
Any conditional statement (if statements, or while loops, and repeat loops) can use the following characters to represent different conditions (and more can be used, as described in section 5.2.3.6) in Turbo Pascal:
Conditional | Description |
---|---|
= | Equal |
<> | Not equal |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
In addition, an if statement might be extended to execute another statement if the condition is not met. This is referred to as an extension to the if statement, called an else statement. That extension is demonstrated along with the method used for placing multiple statements in any control structure (a begin-end; pair) in Turbo Pascal in Listing 5.6. It represents a rewrite of Listing 5.5 that adds a statement which appears if the number entered is less than 10, and adds statements on each part that tell how much greater or less the number entered was from 10:
Listing 5.6. A demonstration of if-else statements.
program fig6; var entered: integer; begin write(Enter a number: ); readln(entered); if entered > 10 then begin writeln(Entered was greater than 10); writeln(It is , entered - 10, greater than 10.); end else begin writeln(Entered was less than or equal to 10); writeln(It is , 10 - entered, less than 10.); end; end.
5.2.3.2. case Statements
If you chain if-else statements, a programs structure might become confusing rather quickly. One solution in some cases to cut down on clutter would be to use the case statement. It uses an ordinal value (integer or char, generally) as a condition and can use set definitions as part of the checks, as defined later in this section. In Listing 5.7, the conditional statements as if statements could grow to be quite cluttered quite quickly. Here, the case statement would accomplish the same thing and be easier understood upon review and maintenance. Also, at the end of the case statement, note the end;, which is required at the end of a case statement.
Listing 5.7. A demonstration of a case statement.
program fig7; { 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); case grade of 90..100: writeln(You earned an A); 80..89: writeln(You earned a B); 70..79: writeln(You earned a C); 60..69: writeln(You earned a D); 0..59: writeln(You earned an F); else { catch all other options that grade could be as an error } { condition } writeln(The number was not between 0 and 100. , Please re-enter your number.); end; end.
For the value of grade entered, each section of the case section is checked, as if an if statement in order from top to bottom. An equivalent listing of this program is presented in section 5.2.3.6.
5.2.3.3. for Loops
A loop that executes a statement or a set of statements a defined number of times is referred to as a for loop. It uses an index variable that it automatically increments by 1 or decrements by 1, given the way the loop is written between two set ranges. The index variable, which can be a number or an ASCII character, should not be changed in the loop. Valid for loops are defined here as well as in Listing 5.8:
for i := 1 to 10 do for c := a to z do for i := 10 downto 1 do for c := z downto a do
Listing 5.8. A demonstration of for loops.
program fig8; var i: integer; c: char; begin write(I can count down from 10: ); for i := 10 downto 1 do write(i, ); writeln; writeln(Now heres the ABCs); for c := A to Z do write(c, ); writeln; end.
The logic in execution of a for loop would be as follows, using the first example of a for loop:
Previous | Table of Contents | Next |