Fortran, a powerful programming language extensively used in scientific, engineering, and numerical applications, provides a variety of operators to compare values. Among these, relational operators are crucial for making decisions, controlling loops, validating data, and driving the logic of programs.
This post focuses on the equality (==) and inequality (/=) operators, which are used to compare values and return logical results—either .true. or .false.. These operators are foundational for writing conditional statements, loops, and functions in Fortran.
1. Introduction to Relational Operators
Relational operators are used to compare two values. In Fortran, the primary relational operators are:
==: Equal to/=: Not equal to<: Less than>: Greater than<=: Less than or equal to>=: Greater than or equal to
Equality (==) and inequality (/=) operators are essential for checking if values are identical or different. They always return logical values of type .true. or .false..
2. Equality Operator (==)
The equality operator checks whether two values are the same. If the values are equal, it returns .true.; otherwise, it returns .false..
Syntax
result = value1 == value2
value1andvalue2can be integers, real numbers, or characters.resultis a logical variable.
Example 1: Integer Comparison
program equality_example
integer :: x, y
logical :: result
x = 10
y = 5
result = x == y
print *, "x == y:", result
end program equality_example
Output:
x == y: F
Explanation:
xis 10 andyis 5.- Since 10 is not equal to 5, the equality operator returns
.false.(F).
Example 2: Real Number Comparison
program real_equality
real :: a, b
logical :: result
a = 3.5
b = 3.5
result = a == b
print *, "a == b:", result
end program real_equality
Output:
a == b: T
Explanation:
aandbare both 3.5.- The equality operator returns
.true.(T).
Note: When comparing real numbers, be careful with floating-point precision. Minor differences due to computation may lead to .false. even if numbers appear equal.
3. Inequality Operator (/=)
The inequality operator checks whether two values are not equal. If the values are different, it returns .true.; otherwise, it returns .false..
Syntax
result = value1 /= value2
Example 3: Integer Inequality
program inequality_example
integer :: x, y
logical :: result
x = 10
y = 5
result = x /= y
print *, "x /= y:", result
end program inequality_example
Output:
x /= y: T
Explanation:
- Since 10 is not equal to 5, the operator returns
.true..
Example 4: Real Number Inequality
program real_inequality
real :: a, b
logical :: result
a = 3.5
b = 4.2
result = a /= b
print *, "a /= b:", result
end program real_inequality
Output:
a /= b: T
Explanation:
- 3.5 is not equal to 4.2, so the inequality operator returns
.true..
4. Logical Variables and Relational Operators
Relational operators always produce logical values. These values can be stored in a logical variable or used directly in conditional statements.
Example 5: Using Relational Operators in IF Statements
program if_relational
integer :: x, y
x = 7
y = 7
if (x == y) then
print *, "x and y are equal"
else
print *, "x and y are not equal"
end if
if (x /= y) then
print *, "x and y are different"
else
print *, "x and y are the same"
end if
end program if_relational
Output:
x and y are equal
x and y are the same
Explanation:
- Logical expressions control program flow.
==and/=can be directly used in conditional statements.
5. Combining Relational Operators with Logical Operators
Relational operators can be combined using logical operators like .and., .or., and .not. to form complex conditions.
Example 6: Multiple Conditions
program combined_conditions
integer :: x, y
logical :: result
x = 10
y = 20
result = (x == 10) .and. (y /= 10)
print *, "Is x 10 AND y not 10?", result
result = (x == 5) .or. (y == 20)
print *, "Is x 5 OR y 20?", result
end program combined_conditions
Output:
Is x 10 AND y not 10? T
Is x 5 OR y 20? T
Explanation:
- Logical operators allow multiple relational conditions to be checked simultaneously.
6. Character Comparisons
Relational operators can also be used with character variables. Comparisons are lexicographical, meaning based on ASCII order.
Example 7: Character Comparison
program char_comparison
character(len=5) :: a, b
a = "apple"
b = "banana"
print *, "a == b:", a == b
print *, "a /= b:", a /= b
end program char_comparison
Output:
a == b: F
a /= b: T
Explanation:
"apple"is not equal to"banana", so equality returns.false.and inequality returns.true.
7. Arrays and Relational Operators
Relational operators can be applied to arrays element-wise when using Fortran 90+ array syntax.
Example 8: Array Comparison
program array_comparison
integer, dimension(5) :: a, b
logical, dimension(5) :: result
a = [1, 2, 3, 4, 5]
b = [1, 0, 3, 4, 0]
result = a == b
print *, "Element-wise equality:", result
result = a /= b
print *, "Element-wise inequality:", result
end program array_comparison
Output:
Element-wise equality: T F T T F
Element-wise inequality: F T F F T
Explanation:
- Each element is compared individually.
- Useful for data validation, masks, or conditional array operations.
8. Relational Operators in Loops
Relational operators are frequently used in loops for termination or validation conditions.
Example 9: Loop with Relational Condition
program loop_condition
integer :: i
i = 1
do while (i /= 6)
print *, "i =", i
i = i + 1
end do
end program loop_condition
Output:
i = 1
i = 2
i = 3
i = 4
i = 5
Explanation:
- Loop continues while
iis not equal to 6. - Relational operator controls loop termination.
9. Practical Applications of Equality and Inequality
Relational operators are used in:
- Input validation – Check if user input matches expected values.
- Loop control – Terminate loops based on specific conditions.
- Conditional computations – Execute code only when values satisfy criteria.
- Data filtering – Compare arrays to select elements.
- Flags and status checks – Determine equality between variables for logic decisions.
Example 10: Input Validation
program input_check
integer :: age
print *, "Enter your age:"
read *, age
if (age == 18) then
print *, "You are exactly 18"
else if (age /= 18) then
print *, "You are not 18"
end if
end program input_check
Explanation:
- Equality and inequality operators validate input and control program output.
10. Best Practices
- Use logical variables to store relational results when reused multiple times.
- Be cautious with real numbers; consider rounding before equality comparison.
- Combine relational operators with logical operators for complex conditions.
- Use element-wise array operations for efficient comparisons in large datasets.
- Prefer descriptive variable names for clarity in conditions.
- Use relational operators in loops and conditionals to clearly express program logic.
11. Summary
- Equality (
==) checks if two values are identical. - Inequality (
/=) checks if two values are different. - Both operators return logical values (
.true.or.false.). - They are used extensively in if statements, loops, arrays, and functions.
- Can be combined with logical operators to form complex conditions.
- Proper use of relational operators ensures accurate, readable, and maintainable code.
12. Comprehensive Example
program relational_example
integer, dimension(5) :: scores
logical, dimension(5) :: passed
integer :: pass_mark
integer :: i
pass_mark = 50
scores = [45, 67, 50, 39, 82]
! Check which scores are passing
do i = 1, 5
passed(i) = scores(i) >= pass_mark
end do
print *, "Scores:", scores
print *, "Passed:", passed
! Check if any score is exactly 50
do i = 1, 5
if (scores(i) == 50) then
print *, "Score of 50 found at index", i
end if
end do
end program relational_example
Explanation:
- Demonstrates equality and inequality operators with arrays.
- Uses logical arrays to track results.
- Combines relational operators with loops for practical data handling.
Leave a Reply