Logical expression and control
| Operators | Meaning |
|---|---|
| .lt. | less than |
| .gt. | greater than |
| .le. | less than or equal to |
| .ge. | greater than or equal to |
| .eq. | equals |
| .ne. | not equal to |
| .and. | and |
| .or. | or |
| .not. | not |
| .xor. | "exclusive" or |
| .eqv. | equivalent |
| .neqv. | not equivalent |
F90 introduced following simplified notation for the relational operators:
| Operators | Meaning |
|---|---|
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| == | equal to |
| /= | not equal to |
IF condition
Calculate square root of real positive numbers:
src/06_sq_root.f90
! Program : Calculate square root
PROGRAM sq_root
IMPLICIT NONE
REAL :: input, output
PRINT *, "Input = "
READ *, input
IF (input > 0) THEN
output = SQRT(input)
PRINT *, "Square root of ", input, "= ", output
ELSE
PRINT *, "Input must be positive real."
ENDIF
END PROGRAM sq_root
Multi-way IFs
src/06_multi_way_if.f90
PROGRAM age_group
IMPLICIT NONE
INTEGER :: age
PRINT "(a,$)", "Enter your age: "
READ *, age
IF (age <= 0) THEN
PRINT *, "Error: please enter a positive integer age."
STOP
ELSE IF (age < 18) THEN
PRINT *, "You are not adult."
ELSE IF (age >= 18 .AND. age < 65) THEN
PRINT *, "You are adult."
ELSE
PRINT *, "You are senior."
END IF
END PROGRAM age_group
Notice also the STOP keyword in above example.
Select case
src/06_select_case.f90
PROGRAM age_select
IMPLICIT NONE
INTEGER :: age
PRINT "(a,$)", "Enter your age: "
READ *, age
SELECT CASE (age)
CASE DEFAULT
PRINT *, "You are not adult."
CASE(18:64)
PRINT *, "You are adult."
CASE(65:)
PRINT *, "You are senior."
END SELECT
END PROGRAM age_select