CHAPTER 4

CONDITIONAL STATEMENTS

 

Conditional statements, in particular the block IF, are the workhorses of the Fortran language. They are executable statements, used to conditionally execute:

  • a block, defined below,

  • a statement, or

  • a transfer of control to another statement.

The conditional execution of a block is accomplished with the block IF. The conditional execution of a statement is accomplished with the logical IF. The conditional transfer of control is accomplished with the arithmetic IF.

The block IF is by far the most commonly used IF statement. The logical IF has limited application, although it remains in use. The arithmetic IF is a remnant of older versions of the language, and it has been categorized as obsolescent by the Fortran 90 standard (see Section 1.4). Accordingly, its use is discouraged in the development of new programs.

A block is a set of zero or more executable statements that is treated as a unit. A null block has zero statements. Execution of a null block has no effect.

A block IF construct consists of one or more blocks separated by the partial statements IF (exp) THEN, ELSE IF (exp) THEN, ELSE, and END IF. The word exp enclosed in parentheses stands for a logical expression.

A block IF construct always begins with the partial statement IF (exp) THEN and ends with the partial statement END IF. A block forming part of a block IF construct is conditionally executed based on the satisfaction of the logical expression exp contained in the partial statement statement IF (exp) THEN or ELSEIF (exp) THEN immediately preceding it.

Example of Block IF Construct Sequence

IF (exp1) THEN

... ! Block 1

ELSE IF (exp2) THEN

... ! Block 2

ELSE IF (exp3) THEN

... ! Block 3

ELSE IF (exp4) THEN

Block 4

ELSE ... ! Block 5

ENDIF


4.1  LOGICAL EXPRESSIONS

A logical expression is one that can take a value of true (.TRUE.) or false (.FALSE.). Logical expressions link two variables with relational and/or logical operators. Table 4.1 shows a list of relational operators. Table 4.2 shows a list of logical operators.

TABLE 4.1  RELATIONAL OPERATORS

Operator Meaning
x.GT.y

x.GE.y

x.LT.y 

x.LE.y

m.EQ.n

m.NE.n  

x greater than y

x greater than or equal to y

x less than y

x less than or equal to y

m equal to n

m not equal to n

Note: Relational operators are shown in upper case, enclosed by delimiting periods.

TABLE  4.2 LOGICAL OPERATORS

Operator   Meaning
a.AND.b  

 

 a.OR.b  

 

a.NEQV.b 

 

a.EQV.b

 

NOT.b

 

The expression is true if both a and b are true;

the expression is false otherwise.

The expression is true if either a or b are true, or both are true;

the expression is false otherwise.

The expression is true if a and b are not equivalent;

the expression is false if a and b are the same.

The expression is true if a and b are the same;

the expression is false if a and b are not equivalent. 

The expression is true if b is false;

the expression is false if b is true.

Note: Logical operators are shown in upper case, enclosed by delimiting periods.

A relational expression is a special type of logical expression consisting of two arithmetic expressions connected by a relational operator, for example:

X.GT.Y

where X and Y are arithmetic expressions and GT is a relational operator. The delimiting periods are required. During execution, the relational expression is evaluated. If X is greater than Y, the relational expression is assigned the value .TRUE.; otherwise, it is .FALSE.

Complex variables can be related only with the .EQ. and .NE. relational operators (see Table 4.1). Furthermore, two complex values are equal only if their corresponding real and imaginary parts are equal.

The following are additional examples of relational expressions:

Y.GE.Z

A+B.LT.C*D X+Y.LE.P/Q

JTIME.EQ.KTIME

APPLE.NE.ORANGE

All variables included in these examples are of numeric data type (integer, real, complex, or double precision).

A logical expression can contain any combination of relational and/or logical operators (including the case of no operator), as shown in the following examples:

A

A.AND.B

C.GT.D.AND.X.LE.Y

A and B are logical expressions; C, D, X, and Y are arithmetic expressions. The delimiting periods are required. During execution, the logical expression is evaluated. In the first example, if A is true, the logical expression is true; otherwise, it is false. In the second example, if both A and B are true, the logical expression is true; otherwise, it is false. In the third example, if C is greater than D, and X is less than or equal to Y, the logical expression is true; otherwise, it is false.

Logical operations (.AND., .OR., and so on) can only be performed on logical data types (accepting only .TRUE. or .FALSE.).

The following are examples of logical expressions:

E.OR.F

E.NEQV.F

G.EQV.H

.NOT.R

U.AND..NOT.V

During execution, these logical expressions are evaluated as follows:

  • The expression E.OR.F is true if either E or F are true.

  • The expression E.NEQV.F is true if E is true and F is false, or if E is false and F is true.

  • The expression G.EQV.H is true if both G and H are true or false.

  • The expression .NOT.R is true if R is false.

  • The expression U.AND..NOT.V is true if U is true and V is false.

In the last example, note that each one of the logical operators AND and NOT is delimited by periods. Also, note that only the operator .NOT. can immediately follow any of the other logical operators.

An example of a logical expression that includes a relational expression is :

(X.GE.45..AND.A.NEQV.B)

The combined expression is true if the relational expression X.GE.45. and the logical expression A.NEQV.B are both true.

The logical and relational expressions introduced in this section are placed within parentheses in the block IF statement (exp).


4.2  BLOCK IF CONSTRUCT

A block IF construct consists of one or more blocks separated by the partial statements

  • IF (exp) THEN

  • ELSE IF (exp) THEN

  • ELSE, and

  • END IF,

in that specific order. Therefore, a block IF construct always begins with the partial statement IF (exp) THEN, and ends with the partial statement END IF.

The partial statement ELSE IF (exp) THEN can be repeated zero or more times. The partial statement ELSE is optional; if included, it can only appear once in a given block IF construct (excluding nesting--see Nested Block IF Constructs later in this section).

In general, at most one of the blocks contained within the block IF construct is executed. However, if the construct includes the optional ELSE statement, exactly one of the blocks is executed. The logical expressions (exp) are evaluated in sequential order until either

  • a true value is found (in one of the exp), or

  • an ELSE statement is encountered.

In any of these two cases, the block immediately following is executed and this completes the execution of the entire block IF construct. In the first case, remaining ELSE IF (exp) THEN statements, if any, are not evaluated. If no true value is found and no ELSE statement is encountered, the execution of the block IF construct is completed with out the execution of any block.

The following are examples of block IF constructs:

Example 1

IF (exp1) THEN

... !Block 1

ELSE IF (exp2) THEN

... ! Block 2

END IF

Example 2

IF (exp1) THEN

... ! Block 1

ELSE IF (exp2) THEN

... ! Block 2 ELSE

... ! Block 3

END IF

In these examples, the words exp1 and exp2 enclosed in parentheses stand for logical expressions. The symbol ... followed by an appropriate trailing comment stands for a given block, i.e., a set of executable statements that is treated as a unit.

The following is an example of a block IF construct containing a block:

IF (A.GT.0.) THEN

B= SQRT(A) ! These two statements

C= A + B ! form a block

END IF

The following rules apply for block IF contructs:

  • The partial statements ELSEIF (exp) THEN and ELSE can have statement labels but these cannot be referenced, because the partial statements are in the middle of the block construct.

  • The partial statement END IF can have a statement label; however, this can only be referenced from the block immediately preceding the END IF.

  • Control can be transferred out of a block by means of the GO TO statement (see Section 4.4).

  • Control cannot be transferred into a block, i..e, without passing first through the appropriate IF (exp) THEN partial statement.

The following examples further illustrate the block IF construct.

Example 1

IF(K.EQ.L) THEN

... ! Block 10

END IF If

If the relational expression (K.EQ.L) is true, block 10 is executed; otherwise, block 10 is bypassed.

Example 2

IF (X.LE.150.) THEN

... ! Block 20

ELSE IF (X.LE.250.) THEN

... ! Block 21

END IF

If the relational expression (X.LE.150.) is true, block 20 is executed; otherwise, if the expression (X.LE.250.) is true, block 21 is executed; otherwise, all blocks are by passed. At most one (1) block is executed per block IF construct.

Example 3

IF (X.LE.150.) THEN

... ! Block 30

ELSE IF (X.LE.250.) THEN

... ! Block 31

ELSE IF (X.LE.350.) THEN

... ! Block 32

END IF

If the relational expression (X.LE.150.) is true, block 30 is executed; otherwise, if the expression (X.LE.250.) is true, block 31 is executed; otherwise, if the expression (X.LE.350.) is true, block 32 is executed; otherwise, all blocks are bypassed. At most one (1) block is executed per block IF construct. Note that once an expression is found to be true, the rest are not evaluated.

Example 4

IF (X.LE.XLIMIT) THEN

... ! Block 40 ELSE ... ! Block 41

END IF

If the relational expression (X.LE.XLIMIT) is true, block 40 is executed; otherwise, block 41 is unconditionally executed due to the presence of the ELSE statement.

Example 5

IF (X.LE.375.) THEN

... ! Block 50

ELSE IF(X.LE.625.) THEN

... ! Block 51 ELSE ... ! Block 52

END IF

If the expression (X.LE.375.) is true, block 50 is exe cuted; otherwise, if the expression (X.LE.625.) is true, block 51 is executed. If neither of the above are found to be true, block 52 is unconditionally executed.

Example 6

IF (X.LE.375.) THEN

... ! Block 60

ELSE IF (X.LE.625.) THEN

... ! Block 61

ELSE IF (X.LE.900.) THEN

... ! Block 62

ELSE ... ! Block 63

END IF

If the expression (X.LE.375.) is true, block 60 is executed; otherwise, if the expression (X.LE.625.) is true, block 61 is executed; otherwise, if the expression (X.LE.900.) is true, block 62 is executed. If none of the above are found to be true, block 63 is unconditionally executed.

Things to watch for with regard to block IF constructs:

  • Make sure that the expressions in parentheses, individually and collectively, follow a clear logical pattern that leads to a logical answer. Otherwise, the block IF construct may fail to accomplish its intended purpose.
  • Remember to always end a block IF construct with an END IF statement. This is a common source of error, which may be difficult to detect.

Nested Block IF Constructs

It is possible to nest two or more block IF constructs, i.e., to insert one block IF construct inside another block IF construct. However, the nested block IF should be completely contained within one block of the nesting block IF. That is, the nested block IF should not overlap blocks of the nesting block IF. There is no limit to the number of nestings allowed in block IF constructs, provided the no-overlap rule is strictly adhered to. The following examples illustrate the use of nested block IF constructs. 

Example 1 

IF (A.LE.3.5) THEN

... ! Block 10

ELSE IF (A.LE.5.5) THEN

... ! Block 20

      IF (K.EQ.1) THEN !Nested block

      ... ! Block 21 

      END IF ! Nested block

ELSE 

... ! Block 30 

END IF 

If the relational expression (A.LE.3.5) is true, block 10 is executed; otherwise, if the expression (A.LE.5.5) is true, block 20 and the nested block IF that follows it (indented here only for clarity), including block 21, are executed. If neither (A.LE.3.5) nor (A.LE.5.5) is true, block 30 is un conditionally executed.

It is good programming practice to label nested block IF constructs with trailing comments to help identify nested from nesting block IF constructs. Following this practice, the preceding example would look like this: 

Example 2 

IF (A.LE.3.5) THEN   ! 100 

... ! Block 10 

ELSE IF (A.LE.5.5) THEN ! 101

... ! Block 20 

    IF (K.EQ.1) THEN  ! 200

    ... ! Block 21

    END IF ! 201

ELSE     ! 102

... ! Block 30

END IF  ! 103 

It is seen that the 100 series numeric trailing comments mark the nesting (outer) block IF. Likewise, the 200 series mark the nested (inner) block IF construct.

 
4.3  LOGICAL IF STATEMENT 

Unlike the block IF construct, which conditionally executes a block, the logical IF statement conditionally executes a single statement. It takes the form 

IF (exp) execstat

where exp is a logical expression and execstat is any executable statement, excluding IF and DO (see Chapter 7). During execution, if the logical expression exp within the parentheses is true, the statement execstat is executed; otherwise, the statement execstat is bypassed.

The following are examples of logical IF statements: 

IF (B) X= Y + Z

IF (C.GT.4.) STOP

IF (D.AND.E) IFLAG= 1

IF (F.EQV.G) KOUNT= KOUNT + 1

Note the following:

  • In the first example, if the logical expression B is true, the statement X= Y + Z is executed. 

  • In the second example, if the relational expression C.GT.4. is true, the statement STOP is executed. 
  • In the third example, if the logical expression D.AND.E is true, the statement IFLAG= 1 is executed. 

  • In the fourth example, if the logical expression F.EQV.G is true, the statement KOUNT= KOUNT + 1 is executed. Note that the value of KOUNT is being incremented by 1 (see Section 5.2).

A logical IF can be readily converted to a block IF. For instance, the statement 

IF (C.GT.4.) STOP

can alternatively be written in the following form: 

IF (C.GT.4.) THEN 

STOP

ENDIF 

While the block IF construct form is longer, it is usually preferred because it leads to a more readable program. Too many logical IF statements result in a disruptive and difficult-to-read program. Therefore, it is good programming practice to avoid the use of the logical IF statement.

 
4.4  GO TO AND CONTINUE STATEMENTS

GO TO Statement 

The GO TO statement is used to transfer control to another statement located elsewhere in the same program unit. It has the form 

GO TO statnum 

where statnum is a valid statement label in the same program unit as the GO TO statement.

Execution of a GO TO statement causes the statement identified by the label to be executed next. For instance, execution of the statement

GO TO 20

causes statement 20 to be executed next.

The GO TO statement can appear by itself, or forming an intrinsic part of a logical IF, as follows: 

IF(N.EQ.L) GO TO 40 

In this example, if the relational expression N.EQ.L is true, control branches to statement 40.

It is good programming practice to use the GO TO statement sparingly, only when absolutely necessary. In addition, the logical IF/GO TO combination is highly disruptive, and its use should be minimized. Often it is better to use a block IF construct instead of a series of logical IF/GO TO statements. 

 
CONTINUE Statement 

Execution of a CONTINUE statement has no effect. The CONTINUE statement is usually placed in between two executable statements or groups of executable statements. It has the form:

CONTINUE

Upon execution of this statement, control transfers to the statement immediately following. 

When preceded by an appropriate statement label, the CONTINUE statement can serve as the destination of a logical IF/GO TO statement or as the last statement of a DO loop (see Chapter 7). For example:

      IF (N.EQ.L) GO TO 50 

     WRITE (6,*) X,Y,Z 

50 CONTINUE 

If the relational expression N.EQ.L is true, control branches to statement 50, and the statement WRITE(6,*) X,Y,Z is effectively bypassed. 

GO TO-CONTINUE Loop 

The GO TO and CONTINUE statements can be used to set up a loop construct to execute a block more than once. The loop works by transfering control from the bottom of the block (using a GO TO statement) to the top of the block (usually a CONTINUE statement). Thus, the execution of the block is repeated as many times as needed (See examples below). Other more advanced types of loop constructs are accomplished with the DO and DO WHILE statements (Chapter 7).

An example of the GO TO-CONTINUE loop is: 

     DATA N /0/ 

10 CONTINUE ! Top of loop

     N= N +1 

     READ(5,*) A, B X= A*B 

     WRITE(6,*) X 

     IF (N.EQ.8) STOP 

     GO TO 10 !    Bottom of loop

This example initializes a counter N with the value 0, and sets up a GO TO-CONTINUE loop to read two variables A and B, calculate X= A*B, and write X. Each time control goes through the loop, the counter N is incremented by 1. Execution stops when N = 8, i.e., after completion of 8 passes through the loop. 

The logical IF statement is used to terminate execution (with the STOP statement, as in the previous example) or to exit the loop when appropriate (with a GO TO statement, as in the next example). Without such loop stop or exit, the loop would continue to be executed indefinitely, which is not desirable. A loop that executes indefinitely is referred to as an infinite loop. Identifying and fixing an infinite loop is part of normal debugging in source program development.

An example of a loop exit via a GO TO is: 

     DATA N /0/ 

10 CONTINUE ! Top of loop 

     N= N +1

    READ(5,*) A, B 

     X= A*B

     WRITE(6,*) X 

     IF (N.EQ.8) GO TO 20 ! Loop exit 

     GO TO 10    ! Bottom of loop 

20 CONTINUE 

An example of an infinite loop is:

     DATA N /0/

10 CONTINUE ! Top of loop 

     N= N +1 

     READ(5,*) A, B 

     X= A*B 

     WRITE(6,*) X 

     GO TO 10    ! Bottom of loop 

Note that this loop would continue to execute indefinitely, with nothing to stop it. This infinite loop would tie up the processor, effectively distracting it from completing other useful tasks.

Things to keep in mind with respect to the GO TO-CONTINUE loop:

  • The GO TO-CONTINUE loop is used here as a way to introduce programming logic rather than as a preferred way to program an application. 
  • In practice, most GO TO-CONTINUE loops can be readily replaced by the more compact and efficient DO and DO WHILE loops treated in Chapter 7. 
 

4.5  ARITHMETIC IF STATEMENT  

Unlike the logical IF/GO TO combination, which conditionally transfers control to a statement located elsewhere in the program unit, the arithmetic IF statement transfers control to at least one and at most three statements. 

The arithmetic IF statement has the form 

IF (numexp) label1, label2, label3

where numexp is a numeric expression, and label1, label2, and label3 are the first, second, and third labels, respectively. Each of these must be a valid statement label in the same program unit as the given arithmetic IF statement.

Execution of an arithmetic IF statement causes the numeric expression numexp to be evaluated, followed by a transfer of control:

  • If numexp is less than zero, control branches to statement label1.
  • If numexp is equal to zero, control branches to statement label2. 
  • If numexp is greater than zero, control branches to statement label3.

The same label may appear more than once in one arithmetic IF statement.

The following are two examples of arithmetic IF statements: 

IF (A) 10,20,30

IF (L-M) 40,40,50

In the first example, if A is less than 0., control branches to statement 10. If A is equal to 0., control branches to statement 20. If A is greater than 0., control branches to statement 30.

In the second example, if L-M is less than or equal to 0, control branches to statement 40. If L-M is greater than 0, control branches to statement.

Note that the arithmetic IF statement has been declared as obsolescent by the Fortran 90 standard. Therefore, its use should be avoided in new program development, and is described here only for completeness.

 

4.6  EXAMPLE PROGRAMS

 Example Program 1: Logical Expressions 

C234567890

      PROGRAM LOGEXP 

      LOGICAL A,B

      DATA A,B,X,Y,Z /.TRUE.,.FALSE.,15.,24.,30./

      IF(A.EQV.B.AND.X.GE.Y) THEN 

      WRITE(6,*) 'FIRST TEST PASSED' 

      ELSE

      WRITE(6,*) 'FIRST TEST DID NOT PASS'

      END IF

      IF (X.LT.Y.OR.Y.LT.Z) THEN 

      WRITE(6,*) 'SECOND TEST PASSED'

      END IF 

      END

This example accomplishes the following tasks: 

  • Declares two logical variables A and B.
  • Initializes A as .TRUE. and B as .FALSE.
  • Initializes three implicitly declared real variables X = 15., Y = 24., and Z = 30, in a DATA statement. 
  • Tests to see if both of the following conditions are true: (a) A and B have the same value; and (b) X is greater than or equal to Y. If both conditions are true, it writes FIRST TEST PASSED; otherwise, it writes FIRST TEST DID NOT PASS. 
  • Tests to see if X is less than Y, or Y is less than Z. If this is true, it writes SECOND TEST PASSED. 

Note that this is only an example. In a real-world situation, some or all of the variables would be read as input rather than be initialized with a DATA statement. Also, note that some processors may implicitly initialize all logical variables as .FALSE., making such initialization redundant.

 Example Program 2: Nested Block IF Construct 

C234567890 

      PROGRAM ABCDEF 

      DATA A,B,C,D,E,F /3.,5.,7.-10.,-20.,-40./

      IF (A.LT.B) THEN ! 100 

      WRITE(6,*) 'A IS LESS THAN B' 

      ELSEIF (B.LE.C) THEN   ! 101

      WRITE(6,*) 'B IS LESS THAN OR EQUAL TO C'

      ELSE    ! 102

        IF (D+E.GT.F) THEN   ! 200

        WRITE(6,*) '(D + E) IS GREATER THAN F' 

        END IF ! 201 

      END IF ! 103 

      END

This example accomplishes the following tasks:

  • Initializes six implicitly declared real variables A, B, C, D, E, and F. 

  • Tests to see if A is less than B. If so, it writes the message A IS LESS THAN B. 

  • Otherwise, it tests to see if B is less than or equal to C. If so, it writes the message B IS LESS THAN OR EQUAL TO C. 

  • Otherwise, it unconditionally executes the nested block IF that follows the ELSE ! 102 statement. It tests to see if D + E is greater than F. If so, it writes the message (D + E) IS GREATER THAN F.

Again, this is only an example. In a real-world situation, some or all of the variables would be read as input rather than be initialized with a DATA statement. Also, the presence of numeric trailing comments helps identify the nested block (200 series) from the nesting block IF construct (100 series). Note that every block IF construct finishes with its own END IF (partial) statement, and every program finishes with it own END statement.

 
4.7  SUMMARY

 The conditional IF statements are used to conditionally execute  

  • a block 

--by using the block IF construct, which consists of the partial statements 

  IF (exp) THEN

  ELSEIF (exp) THEN 

  ELSE 

  END IF 

--block IF constructs may be nested within other block IF constructs 

--block IF constructs are the most commonly used conditional statements

  •  a single statement 

--by using the logical IF statement 

--the logical IF has limited application, but remains in use 

  • a transfer of control 

--by using a GO TO statement as part of a logical IF statement, as in IF(N.EQ.L) GO TO 40

--by using a logical IF as a means to exit a GO TO- CONTINUE loop, as in IF(N.EQ.8) STOP

--by using the arithmetic IF statement

--the arithmetic IF statement has been declared as obsolescent by the Fortran 90 standard; therefore, its use in new applications is discouraged.

 
CHAPTER 4 -- PROBLEMS

  1. The three dimensions of a box are: (1) length, (2) width, and (3) height. Write an interactive program that uses logical variables to accomplish the following tasks:

    • Using DATA statements, initialize the specified minimum width/length ratio as 0.5, maximum width/length ratio as 1.0, minimum volume as 950 cubic inches, and maximum volume as 1050 cubic inches. 

    • Read the width, length, and height of a tested box (in inches), and calculate its width/length ratio and volume.

    • Determine if the box width/length ratio is between the specified limits. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: WIDTH/LENGTH CONSTRAINT IS SATISFIED?: F 

    • Determine if the box volume is between the specified limits. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: VOLUME CONSTRAINT IS SATISFIED?: T 

    • Determine if both width/length ratio and volume constraints are satisfied. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: BOTH WIDTH/LENGTH AND VOLUME CONSTRAINTS ARE SATISFIED?: F

    • Test the program with the following data sets: 

      LENGTH    WIDTH    HEIGHT 
          12.               8.           11. 
          10.5            11.          9.0 
          10.               6.         17.5 

  2. The density of a sample cylinder is being tested for compliance with a set of specifications. Write an interactive program that uses logical vari ables to accomplish the following tasks:

    • Using DATA statements, initialize the specified minimum and maximum densities of the cylinder as 30 and 50 lbs/cu ft, respectively, and the base diameter, height, and weight of a specification cone as 2 ft, 4 ft, and 120 lbs, respectively.

    • Read the diameter and height (in ft) and weight (lbs), of a sample cylinder, and calculate its density.

    • Determine if the cylinder density is between the specified limits. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: FIRST DENSITY CONSTRAINT IS SATISFIED?: T

    • Determine if the cylinder density is less than or equal that of the specification cone. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: SECOND DENSITY CONSTRAINT IS SATISFIED?: F

    • Determine if both density constraints are satisfied. Write the answer, either true (T) or false (F), preceded by an appropriate message such as: BOTH CONSTRAINTS ARE SATISFIED?: F

    • Test the program with the following data sets: 

      CYL. DIAMETER    CYL. HEIGHT    CYL.WEIGHT 
      0.5                           1.                          30. 
        1.                           2.                          60. 
        2.                           4.                         180. 

  3. An instructor wants to convert numeric grades to letter grades, and has chosen for this purpose the following grading scale: A= 100-90; B= 89- 80; C= 79-70; D= 69-60; F= 59-0. Write a program to accomplish the following tasks. Use a nested block IF for this purpose. 

    • Read, from an input file, one at a time, 15 numeric grades; assign a corresponding letter grade; and write the letter grade to an output file. [Hint: Set up a GO TO-CONTINUE loop to execute a block 15 times].

    • If the input numeric grade is out of range, write an appropriate message such as: ERROR IN NUMERIC GRADE.

    • Test the program with the following data set: 97, 78, 56, 100, 902, 45, 67, 72, 89, 71, 69, 85, 92, 80, and 79.

  4. An international traveler is allowed by an airline carrier two check-in bags and one carry-on. The check-in bags cannot weigh more than 70 lbs each. The sum of the dimensions of the check-in bags (length plus width plus height) cannot exceed, for the first bag, 62 inches, and for the second, 45 inches. The third bag (the carry-on) cannot weigh more then 30 lbs, the sum of its dimensions cannot exceed 45 inches, and its longer dimension cannot exceed 22 inches. Write a program to read, from an input file, one-at-a-time, 5 complete baggage data sets, and to determine whether all baggage restrictions have been satisfied. Write the answer to the screen, either YES or NO, preceded by an appropriate message such as: ALL BAGGAGE OK?: YES, or, ALL BAGGAGE OK?: NO. [Hint: Set up a GO TO-CONTINUE loop to execute a block 5 times]. Test your program with the following data set:

    L1    W1    H1      lbs      L2     W2    H2     lbs     L3     W3     H3    lbs 

    36.     24.     6.     66.     24.     16.     5.     45.     17.     12.     8.     29.     

    35.     18.     6.     70.     22.     14.     6.     40.     16.     12.     6.     25.    

    30.     16.     6.     55.     24.     14.     5.     67.     14.     10.     5.     21. 

    34.     16.     6.     72.     23.     15.     6.     39.     14.     10.     5.     43.    

    35.     15.     8.     80.     20.     10.    10.    61.     14.     10.     5.     23.    

  5. The water department of a major city has decided to implement a new rate structure that reflects the perceived need for water conservation. The new rates are: $ 0.95/HCF (100 cubic feet) for the first 20 HCF, $ 1.15/HCF for any additional amounts over 20 HCF, up to 40 HCF, and $ 1.85/HCF for any additional amounts over 40 HCF. The sewer rate is a flat rate of $36.00 per billing period. Write a program to read from an input file, one-at-a-time, six unformatted data sets, each consisting of a consumer's name, address, and water consumption (in HCF) per billing period; calculate the total billing amount, including water and sewage; and write to an output file (using an unformatted WRITE) the con sumer's name, address, water consumption, and amount billed. For any consumer whose consumption is 20 HCF or less, the following message should accompany the bill: THANK YOU FOR YOUR EFFORTS IN WATER CONSERVATION. [Hint: Initialize all constants in DATA statements. Set up a block IF construct and a GO TO-CONTINUE loop to execute six times]. Test your program with the following data set: 

    NAME                ADDRESS              CONSUMPTION (HCF)

    Smith, J. W.      1345 Samantha Ave.          35

    Jones, P. T.       675 Thunderhead Ave.      56 

    Davis, H. K.       3355 Ruffin Ave.                49

    Peters, L. D.      1262 Dax Ave.                   19 

    Garcia, B. R.      6734 Carmel Ave.              27 

    Thomas, J. K.    894 Springfield Dr.             67 

  6. Repeat Problem 5, but with the provision that any consumer showing a consumption of more than 60 HCF per billing period pay an additional sewer charge of $ 12.00. 

  7. Do Problem 3 of Chapter 3 (E banner) using a block IF construct and a GO TO-CONTINUE loop. 

  8. Do Problem 14 of Chapter 3 (tic-tac-toe board) using a block IF con struct and a GO TO-CONTINUE loop. 

  9. Do Problem 15 of Chapter 3 (numeric design) using a block If construct and a GO TO-CONTINUE loop. 

  10. The electric utility of a major city is changing its rates to reflect increasing energy costs. The new rates are: $ 0.095/kwh (kilowatt-hour) for the first 250 kwh (baseline usage), and $ 0.125/kwh for any additional amounts over 250 kwh. Write a program to read from an input file, one at a time, six unformatted data sets, each consisting of a consumer's name, address, and energy consumption (in kwh) per billing period; calculate the amount to be billed; and write to an output file (using an unformatted WRITE) the consumer's name, address, energy consumption, and amount billed. For any consumer whose consumption is 250 kwh or less, the following message should accompany the bill (to the right): THANK YOU FOR YOUR EFFORTS IN ENERGY CONSERVATION. [Hint: Initialize all constants in DATA statements. Set up a block IF construct and a GO TO-CONTINUE loop to execute six times]. Test your program with the following data set: 

    NAME                ADDRESS              CONSUMPTION (kwh)

    Smith, J. W.       1345 Samantha Ave.        535

    Jones, P. T.       675 Thunderhead Ave.     856 

    Davis, H. K.       3355 Ruffin Ave.             1249

    Peters, L. D.      1262 Dax Ave.                  219 

    Garcia, B. R.      6734 Carmel Ave.            627 

    Thomas, J. K.    894 Springfield Dr.           167

  11. Repeat Problem 10, but with the provision that any consumer showing a consumption of more than 750 kwh pay a surcharge of $ 0.025/kwh. 

  12. Write an interactive program that reads an integer less than or equal to 30, calculates its factorial, and writes the result to the screen. Use the following output form: THE FACTORIAL OF ___ IS _______. If the entered number is greater than 30, write the following message to the screen: ENTERED NUMBER IS OUT OF RANGE.


081214