Fortran, a powerful programming language widely used in scientific and engineering applications, provides a variety of data types to represent different kinds of information. Among these, logical variables play a crucial role in controlling program flow. Logical variables store true or false values and are often used in conditional statements, loops, and logical expressions. Understanding logical variables is essential for writing robust, readable, and efficient Fortran programs.
1. Introduction to Logical Variables
A logical variable in Fortran can take only one of two values:
.true..false.
Logical variables are commonly used to represent conditions, flags, or binary states within a program. Unlike integers or real numbers, logical variables are specifically designed for decision-making processes in programs.
Declaration of Logical Variables
The syntax for declaring logical variables is straightforward:
logical :: flag
logicalspecifies the type.flagis the variable name.
Example 1: Basic Logical Variable
program logical_basic
logical :: flag
flag = .true.
if (flag) then
print *, "The flag is true"
else
print *, "The flag is false"
end if
end program logical_basic
Explanation:
flagis assigned the value.true.- The
ifstatement checks the condition. - Depending on the value of
flag, the corresponding message is printed.
2. Using Logical Variables in Conditional Statements
Logical variables are commonly used in if statements to control program flow.
Syntax of IF Statements
if (condition) then
! Statements executed if condition is true
else
! Statements executed if condition is false
end if
Example 2: Logical Variable in IF Statement
program check_flag
logical :: is_valid
is_valid = .false.
if (is_valid) then
print *, "Input is valid"
else
print *, "Input is not valid"
end if
end program check_flag
Explanation:
is_validis assigned.false.- The
elsebranch executes because the condition is false.
3. Logical Operators
Logical variables can be combined using logical operators to form complex conditions.
Common Logical Operators
.and.: Logical AND.or.: Logical OR.not.: Logical NOT.eqv.: Logical equivalence.neqv.: Logical non-equivalence
Example 3: Logical Operators
program logical_operators
logical :: a, b, c
a = .true.
b = .false.
c = a .and. b
print *, "a .and. b =", c
c = a .or. b
print *, "a .or. b =", c
c = .not. a
print *, ".not. a =", c
end program logical_operators
Output:
a .and. b = F
a .or. b = T
.not. a = F
Explanation:
.and.evaluates to true only if both operands are true..or.evaluates to true if at least one operand is true..not.reverses the logical value.
4. Logical Expressions
Logical expressions combine relational operators with logical variables to produce true or false results.
Relational Operators
==: equal/=: not equal>: greater than<: less than>=: greater than or equal<=: less than or equal
Example 4: Logical Expressions
program logical_expression
integer :: x
logical :: result
x = 10
result = (x > 5) .and. (x < 20)
if (result) then
print *, "x is between 5 and 20"
else
print *, "x is outside the range"
end if
end program logical_expression
Explanation:
(x > 5) .and. (x < 20)evaluates to.true.- The
ifstatement uses the result to control program flow.
5. Logical Variables in Loops
Logical variables are often used in loops to control iteration, especially in do while loops or conditional exits.
Example 5: Logical Variable in a DO WHILE Loop
program logical_loop
integer :: i
logical :: keep_running
i = 1
keep_running = .true.
do while (keep_running)
print *, "Iteration", i
if (i == 5) then
keep_running = .false.
end if
i = i + 1
end do
end program logical_loop
Explanation:
keep_runningcontrols the loop execution.- The loop exits when
keep_runningbecomes.false.
6. Combining Multiple Logical Conditions
Complex logical conditions can be created by combining multiple logical variables and expressions.
Example 6: Combined Conditions
program complex_logical
integer :: age
logical :: has_permission, is_adult
age = 20
has_permission = .true.
is_adult = (age >= 18)
if (is_adult .and. has_permission) then
print *, "Access granted"
else
print *, "Access denied"
end if
end program complex_logical
Explanation:
- Both
is_adultandhas_permissionmust be true to grant access. - Logical operators allow flexible decision-making.
7. Logical Arrays
Fortran allows arrays of logical variables, which are useful in scientific computing, simulations, and data validation.
Example 7: Logical Array
program logical_array
logical, dimension(5) :: flags
integer :: i
flags = [.true., .false., .true., .true., .false.]
do i = 1, 5
if (flags(i)) then
print *, "Element", i, "is true"
else
print *, "Element", i, "is false"
end if
end do
end program logical_array
Explanation:
flagsis an array of logical variables.- Each element is tested in a loop to print its status.
8. Intrinsic Functions for Logical Variables
Fortran provides intrinsic functions for logical operations and array reductions.
Common Intrinsic Functions
all(array): Returns.true.if all elements are trueany(array): Returns.true.if any element is truelbound(array)andubound(array): Work with array indices
Example 8: Using Intrinsic Logical Functions
program logical_intrinsic
logical, dimension(5) :: flags
flags = [.true., .true., .false., .true., .true.]
if (all(flags)) then
print *, "All flags are true"
else
print *, "Not all flags are true"
end if
if (any(flags)) then
print *, "At least one flag is true"
end if
end program logical_intrinsic
Explanation:
all(flags)checks if every element is true.any(flags)checks if at least one element is true.
9. Logical Variables in Functions and Subroutines
Logical variables can be used as function return types or as parameters in subroutines.
Example 9: Logical Function
program logical_function
logical :: is_even
is_even = check_even(4)
if (is_even) then
print *, "Number is even"
else
print *, "Number is odd"
end if
contains
function check_even(n) result(res)
integer, intent(in) :: n
logical :: res
res = mod(n, 2) == 0
end function check_even
end program logical_function
Explanation:
check_evenreturns a logical value based on a condition.- Logical functions simplify conditional checks in programs.
Example 10: Logical Variable as Subroutine Argument
program logical_subroutine
logical :: flag
flag = .false.
call toggle(flag)
if (flag) then
print *, "Flag is now true"
else
print *, "Flag is still false"
end if
contains
subroutine toggle(x)
logical, intent(inout) :: x
x = .not. x
end subroutine toggle
end program logical_subroutine
Explanation:
intent(inout)allows the subroutine to modify the logical variable.- The
togglesubroutine reverses the flag value.
10. Best Practices for Logical Variables
- Always use
.true.or.false.explicitly: Avoid using integers for logical values. - Use descriptive names: Variables like
is_valid,flag_active, orhas_permissionimprove readability. - Combine logical operators carefully: Parentheses improve clarity in complex conditions.
- Avoid unnecessary negations: Simplify conditions to reduce cognitive load.
- Leverage intrinsic functions: Functions like
allandanysimplify array-based logical operations. - Use logical variables in loops and functions to control flow efficiently.
11. Applications of Logical Variables
Logical variables are essential in numerous applications, including:
- Input validation: Checking user inputs for correctness.
- Control flags: Controlling loops, program modes, and feature toggles.
- Array condition checks: Performing operations based on logical arrays.
- Conditional computations: Selecting algorithms or operations based on conditions.
- Simulation and modeling: Representing states, switches, or binary conditions.
Example 11: Practical Use in Simulation
program simulation
logical :: simulation_running
integer :: timestep
timestep = 0
simulation_running = .true.
do while (simulation_running)
timestep = timestep + 1
print *, "Simulation timestep:", timestep
if (timestep >= 10) simulation_running = .false.
end do
print *, "Simulation completed"
end program simulation
Explanation:
simulation_runningcontrols the loop for timesteps.- The simulation stops when the timestep reaches 10.
12. Summary
- Logical variables store
.true.or.false.values. - They are essential for conditional statements, loops, and functions.
- Logical operators allow complex expressions to control program flow.
- Arrays of logical variables are useful in data processing and scientific computing.
- Intrinsic functions like
allandanysimplify logical array operations. - Best practices include descriptive variable names, explicit values, and careful combination of conditions.
- Logical variables are widely used in input validation, control flags, simulations, and decision-making in Fortran programs.
Leave a Reply