6 IF STRUCTURES

The selection structure is used to make a comparison between two values and uses the results to determine the next action to be taken. In FORTRAN, the IF statement, containing a logical comparison is used in the selection structure. There are two main forms of the IF structure: a logical IF statement and a block IF.

Logical IF Statement

The logical IF statement is used when the action to be taken is a single FORTRAN statement. Its general form is
IF (logical expression) statement 1

next statement

Figure 1 - Logical IF

As shown in Figure 1, if logical exp. is true, statement 1 is executed and then next statement is executed. If logical exp. is false, statement 1 is ignored and next statement is executed. Example

IF (x .gt. large) large = x
PRINT *, 'The larger of the numbers is ', large

If x is greater than large, large is assigned the value x and the PRINT statement is executed.
If x is less than or equal to large, only the PRINT statement is executed.

Block IF

The block IF structure is used when more than one statement (referred to as a block of statements) is to be executed. Its general form is

IF (logical expression) THEN

statement block

END IF

next statement

Figure 2 - Block IF

As shown in Figure 2, if logical exp. is true, then statement block will be executed. If logical exp. is false, program control immediately passes to next statement and statement block is not executed.

Example

Problem Statement
This program reads the name of a student and the student's grade point average. If the
grade point average is greater than or equal to 3.5, the student's name and average
are printed. The number of honor students, students with gpa's greater than or equal to
3.5, is tallied and printed when the user has finished entering names.

Input/Output Description
The user inputs the student's name and grade point average.

If the grade point average is greater than or equal to 3.5, the students' name and
grade point average is printed. A total number of honor students is printed at the end of the
program.

Algorithm
  1. Read the student's name and grade point average.
  2. Compare the average to 3.5
  3. If the average is greater than or equal to 3.5, print the students name and average, add one to the total number of honor students.
  4. Give the user a chance to enter another student and grade point average.
  5. Print the total number of students on the honor roll.

Code
c  The following program reads the name of a student and the student's gpa.
c  If the gpa is greater than or equal to 3.5, the student's name and gpa are
c  printed.  The number of honor students, students with gpa's greater 
c  than or equal to 3.5, is also incremented and printed when the user has 
c  finished entering names.
 
        PROGRAM honor
 
        REAL gpa
        INTEGER honor
        CHARACTER name*20, answer*1
        
	honor = 0
5       PRINT *, 'Student"s name:'
        READ *,name
        PRINT *, 'GPA: '
        READ *, gpa
 
        IF (gpa .ge. 3.50) THEN
           PRINT *,name, ' is an honor roll student with GPA: 'gpa
           honor = honor + 1
        ENDIF
        
        PRINT *, 'Do you want to enter another name (y=yes, n=no)?'
        READ *, answer
 
        IF (answer .eq.'y') go to 5
 
        PRINT *, 'The number of honor roll students is ', honor
 
        END

If the value of the variable gpa is greater than or equal to 3.50, the program prints the student's name and gpa and adds one to the current value of the variable honor. If the value of the variable gpa is less than 3.50, the student's name and gpa is not printed and program control passes to the PRINT statement following the END IF. Notice that the logical IF statement is used to allow the user to enter an arbitrary number of student names and associated GPA's.

Example - Division Program 1

Problem Statement
This program accepts two numbers from the keyboard and divides the first number by the
second. If the second number is a 0, it prints an error message.

Input/Output Description
The user inputs two numbers.

Mathematical Equation
The first number entered is divided by the second number entered.

Algorithm
  1. Prompt for and get two numbers.
  2. If the second number entered is a 0, print an error message.
  3. Divide the first number by the second number.
  4. Print the quotient.

Code
Division Program 1

IF-THEN-ELSE Structure The IF-THEN-ELSE structure executes a single statement or a block of statements if the logical expression is true and a different statement or block of statements if it is false. Its general form is

IF (logical expression) THEN

statement block 1

ELSE

statement block 2

END IF

next statement

Figure 3 - IF-THEN-ELSE

Figure 3 illustrates that if the logical expression is true, statement block 1 is executed. If it is false, statement block 2 is executed. Example - Division Program 2

The division program could have been written with an IF-THEN-ELSE structure.

Division Program 2

IF-THEN-ELSE IF Structure

The IF-THEN-ELSE IF structure is an extension of the IF-THEN-ELSE structure. The added feature is the ability to handle multiple conditions. Its general form for three conditions is
IF (logical expression 1) THEN

statement block 1

ELSE IF (logical expression 2) THEN

statement block 2

ELSE IF (logical expression 3) THEN

statement block 3

END IF

next statement

Figure - 4 IF-THEN-ELSE-IF

As shown in Figure 4, if logical exp. 1 is true, statement block 1 is executed and program control passes to next statement. If logical exp. 1 is false and logical exp. 2 is true, then statement block 2 is executed and program control passes to next statement. Likewise, if the first two logical statements are false and logical exp. 3 is true, statement block 3 is executed and program control passes to next statement . The statement block following the first logical expression which is true is the only statement block that will be executed. If none of the logical expressions is true, program control passes to the next statement following the END IF.

Example - Number of Distinct Real Roots in a Quadratic Equation

Program Description
This program will determine the number of real roots of a quadratic equation.

Input/Output
The user will input three values - a,b, and c (where a*x**2+b*x+c=0).

Mathematical Equation
The roots, x1 and x2, of a quadratic equation of the form 0 = ax**2 + bx + c are

b**2 - 4ac is called the discriminant because its value determines whether there are two
distent real roots, a double real root, or two imaginary roots (no real roots). If the
discriminant is less than 0.0 the quadratic has no real roots. If it is equal to 0.0 the
quadratic has one real root (-b/(2*a)). If it is greater than 0.0 then the quadratic has two
real roots (-b+rad)/(2*1) and (-b-rad)/(2*a).

Algorithm
  1. Prompt for and get the values for a, b, and c.
  2. Calculate the value of the discriminant.
  3. Determine the number of real roots.

Code
Number of real roots in a quadratic equation.

Nested IF Statements

IF statements may also be nested within IF statements. They then take the general form

IF (logical expression 1) THEN

statement block 1

IF (logical expression 2) THEN

statement block 2

END IF

END IF

next statement

Figure 5 - Nested IF
If logical exp. 1 is true, statement block 1 is executed; if logical exp. 1 is false program control passes to next statement. If logical exp. 2 is true statement block 2 is executed and program control passes to next statement. Logical exp. 2 is evaluated only when logical exp. 1 is true.

EXERCISES

1. Number of Real Roots in a Quadratic Equation Program

Enter, compile, and execute the program. Give the output when

Notice the flow of the program based on the value of the variable rad and the nested IF-THEN-ELSE structure.

2. Pythagorean Triple Program

Program Description This program determines whether or not three integers (a,b,c) entered from the keyboard are a Pythagorean triple. When a Pythagorean triple is the length of the sides of a triangle, the triangle will always be a right triangle.

Input/Output Description
The user enters three integer values.

The output indicates whether or not the three values entered form a Pythagorean Triple.

Mathematical Equation
A Pythagorean Triple is defined as: (a**2+b**2=c**2) where c is the largest value.

Algorithm
  1. Prompt for and get three values.
  2. Determine whether or not the three values form a Pythagorean Triple.
  3. Display the appropriate message.

Write a program (on paper) implementing the algorithm.

Give the output for the program when

What determines which lines of the program are executed?

3. Shipping Category Program

Program Description

This program places an object in a shipping category when the weight of the object is entered. Weights and categories are listed in Table 1.

Table 1: Shipping Weight Categories
Weight Category
0.0 =< weight < 21.0 1
21.0 =< weight < 101.0 2
101.0 =< weight < 201.0 3
201.0=< weight < 301.0 4
301.0 =< weight 5

Input/Output Description
The user inputs the weight of the object.

The output is the shipping category of the object based on weight.

Algorithm
  1. Prompt for and get the weight of the object.
  2. Determine the shipping category based on the weight.
  3. Display the shipping category.

Code
Shipping Category Program

Enter, compile, and execute the program. Give the output for the program when

Why do the results change? What determines which lines of the program are executed?

4. Binary Star Magnitude Program
Problem Statement

The brightness of a binary star varies in a cycle with a 6.4 day period. Write a program that calculates the magnitude of a binary star given the time in the cycle.

Mathematical Equations

The mathematical model for this program has already been developed. It uses the intrinsic functions ln and cos. Note that pi is a variable and must be assigned a value. The times and magnitudes are listed in Table 2.

Table 2: Binary Star Brightness Magnitudes
Time (days) Magnitude
0.0 =< time < 0.9 2.5
0.9 =< time < 2.3 3.355 - ln(1.352 + cos(pi(t - 0.9)/0.7))
2.3 =< time < 4.4 2.5
4.4 =< time < 5.2 3.598-ln(1.998 + cos(pi(t - 4.4)/0.4))
5.2 =< time < 6.4 2.5

Algorithm
  1. Prompt for and get the time value.
  2. Determine the value of magnitude based on the value of time.
  3. Display the time and the magnitude.

Determine the values for the magnitude using a calculator or the data provided in the table when time = .6, 1.0, 3.0, 5.0, 6.0. Test the program using your data.