FORTRAN

UNIT 4 
SELECTION STATEMENT

Selection statement or selection construct or selection structures is also called conditional or decision statement. The purpose is to make your program select a result from an option or from a list of options, when a specified condition is met.

4.1       PROGRAMS LAB EXAMPLES

4.1.1   Simple If Statement

In simple if statement, only a single condition is evaluated, the if-block is executed, if the condition is true.

Syntax:

if condtion then

    !statements

end if

 

4.1.2   Lab 1 – program to test the age of a student, if he eligible to vote

program lab1

 

!declare variable

integer :: age

 

!display user action and accept information

print *, 'Enter your age'

read*, age

 

!test and display

!test if age <18, show 'teenager…’

if (age <18) then

    print*, 'You are a teenager, you cannot vote'

end if

end program lab1

 

4.1.3   If…else Statement

In if…else statement, when the condition of the if-block is evaluated to false, the statements in the else block, will get executed.

Syntax:

if condtion then

    !statements

else

    !statements

end if

 

4.1.4   Lab 2 – program to test the age of a student, if he eligible to vote

program lab2

 

!declare variable

integer :: age

 

!user action and accept information

print *, 'Enter your age'

read*, age

 

!test and display

!test if age <18, show 'teenager…’

if (age <18) then

    print*, 'You are a teenager, you cannot vote'

else

    print*, 'You are an adult, you can vote'

end if

end program lab2

 

 

4.1.5   If…else…if…else Statement

In if…else…if….else statement, multiple conditions are evaluated, it is only the block that satisfies the specified condition that will get executed. You can add as many else-if-blocks as you want.

Syntax:

if condition_1 then

   ! statements  

else if condition_2 then      

   ! statements  

else if condition_3 then      

   ! statements

else      

   ! statements

end if

 

4.1.6   Lab 3 – program to test the classification of patient’s BMI (Body Mass Index), using the table below.

BMI

CLASSIFICATION

< 18.5

Under weight

18.5 to < 25

Normal

25 to < 30

Over weight

> =30

Obesity

!code

program lab3

 

!declare variable

 

real :: weight, height, resultBmi

 

 !user action (what user should do) and accept input

print *, 'Enter weight value in KG'

read*, weight

 

print *, 'Enter height value in meters from (0-2)'

read*, height

 

 !processing

resultBmi = weight / (height * height)

 

 !display output

print *,'The BMI is:: '

print '(f6.2)', resultBmi

 

 

if (resultBmi .lt. 18.5) then

    print *,'Your BMI is Under weight'

else if ((resultBmi .gt. 18.5) .and. (resultBmi .lt. 25))  then

    print *,'Your BMI is Normal'

 

else if ((resultBmi .gt. 25) .and. (resultBmi .lt. 30))  then

    print *,'Your BMI is Over weight'

 

else if (resultBmi .gt. 30) then

    print *,'Your BMI is Obesity'

 

else

    print *,'Something went wrong'

end if

end program lab3

TIP: Take note of how we wrap the expressions for the condition inside parentheses. Then, fortran logical and is ‘.and.’ (period and keyword and period), also, Fortran relational operator for greater than is ‘.gt.’ while for less than is ‘.lt.’. That is to say, you have to use the correct two-letter abbreviation enclosed by dots!


4.1.7   Lab 4 – program for a user to select his/her gender (take/read/accept character input from the user), using the table below.

Value

CLASSIFICATION

M

Male

F

Female

When the user enters something else

Something went wrong

TIP: We will use the equality operator ‘.EQ.’ and the OR operator ‘.OR.’.

Fortran

program lab4 


!declare variable 

character :: gender 


!user action (what user should do) and accept input

print *, 'Enter m/f to select your gender'

read*, gender

 

!test and display output 

if ((gender .eq. 'm') .or. (gender .eq. 'M')) then

    print *,'Male'

 

else if ((gender .eq. 'f')  .or. (gender .eq. 'F')) then

    print *,'Female'

 

else

    print *,'Something went wrong'

end if

end program lab4

 

4.1.8   Select Case Statement

Select case statement is a simplified version of if…else…if…else statement, it helps to make the program to be more readable. Here, only a single condition is evaluated among several alternatives, the block the meets the condition is executed. That is to say, a select case statement allows a variable to be tested for equality against a list of values.

Syntax:

   select case (expression)  

      case label_1

        !statement

      case label_2

        !statement

      case label_3

        !statement

      case default

         !statement     

   end select 


4.1.9   Lab 5 – program to test a user's gender. Modification of Lab4 program example

Fortran

program lab5

 

!declare variable

character :: gender

 

!user action (what user should do) and accept input

print *, 'Enter m/f to select your gender'

read*, gender

 

!test and display output

   select case (gender)

      case ('m')

        print*, 'Male'

      case ('M')

        print*, 'Male'

      case ('f')

        print*, 'Female'

      case ('F')

        print*, 'Female'

      case default

         print*, 'Something went wrong'

   end select

     print*, "Your gender is:: ", gender

end program lab5

 

 


Comments

Popular posts from this blog