Relational operators are essential tools in Fortran programming that allow comparison of values. They return logical results (.true. or .false.) based on the relationship between operands. Among the relational operators, greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=) are fundamental for evaluating numerical relationships. These operators are widely used in decision-making, loops, and conditional expressions in scientific and engineering applications.
Mastering relational operators is crucial for building complex logic, validating data, controlling program flow, and implementing algorithms that depend on comparisons.
What Are Relational Operators?
A relational operator is a symbol that compares two operands and evaluates to a logical value:
.true.if the condition is satisfied..false.if the condition is not satisfied.
The main relational operators in Fortran are:
- Equality (
==) - Inequality (
/=) - Greater Than (
>) - Less Than (
<) - Greater Than or Equal To (
>=) - Less Than or Equal To (
<=)
In this post, we focus on the greater-than and less-than operators and their variations.
Greater Than (>)
The greater-than operator (>) checks if the left-hand operand is larger than the right-hand operand. It is commonly used in conditions to trigger certain actions when a variable exceeds a threshold.
Syntax
result = operand1 > operand2
operand1andoperand2can be integers, real numbers, or double precision.resultis a logical variable that stores.true.ifoperand1is greater thanoperand2, otherwise.false..
Example
real :: a, b
logical :: result
a = 7.5
b = 3.2
result = a > b
print *, "Is a greater than b?", result
Output:
Is a greater than b? T
- In this example,
a > bevaluates to.true.because 7.5 is greater than 3.2.
Greater-than comparisons are frequently used in loops and conditional statements to check upper limits, validate inputs, or implement decision-making logic.
Less Than (<)
The less-than operator (<) checks if the left-hand operand is smaller than the right-hand operand. It is used to perform comparisons when lower limits or thresholds need to be verified.
Syntax
result = operand1 < operand2
- Works with integer, real, and double precision values.
- Returns
.true.if the left-hand operand is smaller than the right-hand operand, otherwise.false..
Example
real :: a, b
logical :: result
a = 7.5
b = 3.2
result = a < b
print *, "Is a less than b?", result
Output:
Is a less than b? F
- Since 7.5 is not less than 3.2, the result is
.false..
Greater Than or Equal To (>=) and Less Than or Equal To (<=)
For more inclusive comparisons, Fortran provides greater-than-or-equal-to (>=) and less-than-or-equal-to (<=) operators. These operators are useful when equality is part of the comparison criteria.
Example
real :: a, b
print *, "a >= b:", a >= b
print *, "a <= b:", a <= b
Output:
a >= b: T
a <= b: F
a >= breturns.true.becauseais greater thanb.a <= breturns.false.becauseais not smaller than or equal tob.
Using Relational Operators in Conditional Statements
Relational operators are most commonly used in IF statements to control program flow. They allow the program to make decisions based on comparisons.
Example: Checking Temperature
real :: temperature
temperature = 37.5
if (temperature > 37.0) then
print *, "Temperature is above normal."
else if (temperature < 36.0) then
print *, "Temperature is below normal."
else
print *, "Temperature is normal."
end if
Output:
Temperature is above normal.
- Here,
>and<operators determine which branch of the conditional statement is executed.
Using Relational Operators in Loops
Relational operators are also used to control loops, ensuring they iterate only when certain conditions are met.
Example: Counting Down
integer :: i
do i = 10, 1, -1
if (i < 5) exit
print *, "i =", i
end do
Output:
i = 10
i = 9
i = 8
i = 7
i = 6
i = 5
- The loop exits when
i < 5. - The relational operator
<controls when to terminate the loop.
Combining Relational Operators
Relational operators can be combined with logical operators (.and., .or., .not.) to create complex conditions.
Example: Range Check
real :: x
logical :: flag
x = 7.0
flag = (x > 5.0) .and. (x < 10.0)
print *, "Is x between 5 and 10?", flag
Output:
Is x between 5 and 10? T
- The expression checks if
xlies within a specified range. - Both conditions must be true for
.and.to return.true..
Practical Applications of Greater Than and Less Than Operators
- Validating Input Data: Ensure user inputs are within acceptable ranges.
integer :: age
print *, "Enter your age:"
read *, age
if (age >= 0 .and. age <= 120) then
print *, "Valid age"
else
print *, "Invalid age"
end if
- Scientific Calculations: Identify thresholds in simulations, e.g., pressure, velocity, or temperature limits.
real :: pressure
pressure = 150.0
if (pressure > 100.0) then
print *, "Warning: Pressure is too high!"
end if
- Sorting Algorithms: Compare elements to arrange arrays in ascending or descending order.
integer, dimension(5) :: arr
integer :: i, j, temp
arr = (/ 5, 2, 9, 1, 7 /)
do i = 1, 4
do j = i+1, 5
if (arr(i) > arr(j)) then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
end if
end do
end do
print *, "Sorted array:", arr
Output:
Sorted array: 1 2 5 7 9
- Greater-than operator is used to compare elements for sorting.
- Algorithm Control: Implement conditions for iterative methods, convergence checks, or stopping criteria.
real :: error
error = 0.005
if (error < 0.01) then
print *, "Convergence achieved."
else
print *, "Continue iterations."
end if
Best Practices for Using Relational Operators
- Use Logical Variables: Store comparison results in logical variables for clarity.
- Combine with Parentheses: When combining multiple comparisons, use parentheses to control evaluation order.
- Check Data Types: Ensure operands are compatible (integer with integer, real with real) to avoid unintended results.
- Avoid Magic Numbers: Use constants or parameters for thresholds instead of hardcoding values.
Example Using Parameter
real, parameter :: MAX_TEMP = 100.0
real :: temp
temp = 105.0
if (temp > MAX_TEMP) then
print *, "Warning: Temperature exceeds maximum limit."
end if
- Improves readability and maintainability of code.
Leave a Reply