Decision-making is a fundamental aspect of programming. Often, a program must perform certain actions depending on the value of a variable or the result of a condition. In Fortran, the IF-ELSE statement provides a structured way to execute alternative code blocks based on logical conditions.
This post explores IF-ELSE statements in depth, including syntax, examples, nested conditions, logical expressions, practical applications, and best practices.
1. Introduction to IF-ELSE Statements
The IF-ELSE statement allows a program to evaluate a condition and execute different blocks of code depending on whether the condition is true or false. It is one of the most common control flow structures in programming and is essential for making decisions in scientific computations, simulations, and data processing.
2. Basic Syntax of IF-ELSE
The general syntax of an IF-ELSE statement in Fortran is:
if (condition) then
! code executed if the condition is true
else
! code executed if the condition is false
end if
- condition: A logical expression that evaluates to
.true.or.false. - then: Marks the beginning of the block executed when the condition is true
- else: (Optional) Block executed if the condition is false
- end if: Terminates the IF statement
3. Example: Basic IF-ELSE Statement
program if_else_example
integer :: num
num = -3
if (num > 0) then
print *, "Positive"
else
print *, "Non-positive"
end if
end program if_else_example
Output:
Non-positive
Explanation: The variable num is -3. The condition num > 0 is false, so the code in the else block executes.
4. IF-ELSE with Logical Expressions
Conditions in IF statements can include relational operators (>, <, >=, <=, ==, /=) and logical operators (.and., .or., .not.).
4.1 Example: Checking Multiple Conditions
program if_else_logical
integer :: x
x = 7
if (x > 0 .and. x < 10) then
print *, "x is between 1 and 9"
else
print *, "x is outside the range"
end if
end program if_else_logical
Output:
x is between 1 and 9
5. Nested IF-ELSE Statements
Sometimes, multiple conditions must be checked sequentially. Nested IF-ELSE statements allow this.
5.1 Example: Nested IF-ELSE
program nested_if_else
integer :: num
num = 0
if (num > 0) then
print *, "Positive"
else
if (num == 0) then
print *, "Zero"
else
print *, "Negative"
end if
end if
end program nested_if_else
Output:
Zero
Explanation: The outer IF checks if the number is positive. Since num is 0, it evaluates the inner IF.
6. Using IF-ELSE for Multiple Conditions
For multiple alternatives, you can chain conditions using ELSE IF.
6.1 Example: ELSE IF Ladder
program elseif_example
integer :: score
score = 85
if (score >= 90) then
print *, "Grade: A"
else if (score >= 75) then
print *, "Grade: B"
else if (score >= 50) then
print *, "Grade: C"
else
print *, "Grade: F"
end if
end program elseif_example
Output:
Grade: B
Explanation: The program evaluates each condition sequentially. score >= 75 is true, so “Grade: B” is printed.
7. IF-ELSE with Real and Double Precision Variables
IF-ELSE statements can be applied to real and double precision variables as well.
7.1 Example: Checking Temperature
program temperature_check
real :: temp
temp = 36.5
if (temp > 37.0) then
print *, "Fever"
else
print *, "Normal"
end if
end program temperature_check
Output:
Normal
8. IF-ELSE with Logical Variables
Logical variables can directly control IF statements.
8.1 Example: Using Logical Variables
program logical_if
logical :: is_raining
is_raining = .true.
if (is_raining) then
print *, "Take an umbrella"
else
print *, "No need for an umbrella"
end if
end program logical_if
Output:
Take an umbrella
9. Practical Applications of IF-ELSE Statements
9.1 Age Checker
program age_checker
integer :: age
print *, "Enter your age:"
read *, age
if (age >= 18) then
print *, "You are an adult."
else
print *, "You are a minor."
end if
end program age_checker
9.2 Speed Limit Checker
program speed_check
real :: speed
print *, "Enter speed (km/h):"
read *, speed
if (speed > 100.0) then
print *, "Speeding!"
else
print *, "Speed is within limit."
end if
end program speed_check
9.3 Even or Odd Number
program even_odd
integer :: n
print *, "Enter a number:"
read *, n
if (mod(n,2) == 0) then
print *, "Even number"
else
print *, "Odd number"
end if
end program even_odd
10. Best Practices for IF-ELSE Statements
- Use clear and descriptive conditions: Conditions like
x > 0are self-explanatory. - Use ELSE IF for multiple alternatives instead of deeply nested IF statements.
- Avoid redundant conditions: Check only necessary cases.
- Indent code blocks properly: Improves readability.
- Combine IF-ELSE with logical operators for complex decision-making.
- Use comments to explain complex decision logic.
11. IF-ELSE with Arrays
You can use IF-ELSE statements to check elements in arrays.
11.1 Example: Array Check
program array_check
integer :: numbers(5) = (/1, -2, 3, 0, -5/)
integer :: i
do i = 1, 5
if (numbers(i) > 0) then
print *, "Element", i, "is positive"
else if (numbers(i) == 0) then
print *, "Element", i, "is zero"
else
print *, "Element", i, "is negative"
end if
end do
end program array_check
Output:
Element 1 is positive
Element 2 is negative
Element 3 is positive
Element 4 is zero
Element 5 is negative
12. Combining IF-ELSE with User Input
IF-ELSE statements are commonly used to validate and respond to user input.
12.1 Example: Menu Selection
program menu_selection
integer :: choice
print *, "Select an option: 1) Start 2) Stop 3) Pause"
read *, choice
if (choice == 1) then
print *, "Starting..."
else if (choice == 2) then
print *, "Stopping..."
else if (choice == 3) then
print *, "Pausing..."
else
print *, "Invalid selection"
end if
end program menu_selection
13. IF-ELSE in Scientific Calculations
13.1 Example: Quadratic Equation Solver
program quadratic_solver
real :: a, b, c, discriminant, root1, root2
a = 1.0
b = -3.0
c = 2.0
discriminant = b**2 - 4*a*c
if (discriminant > 0) then
root1 = (-b + sqrt(discriminant)) / (2*a)
root2 = (-b - sqrt(discriminant)) / (2*a)
print *, "Two real roots:", root1, root2
else if (discriminant == 0) then
root1 = -b / (2*a)
print *, "One real root:", root1
else
print *, "No real roots"
end if
end program quadratic_solver
14. Summary
The IF-ELSE statement is a critical control structure in Fortran for making decisions. It allows a program to:
- Execute different code blocks depending on conditions
- Handle multiple alternatives using ELSE IF
- Work with integers, reals, logical values, and arrays
- Validate input and implement decision-making in scientific and engineering applications
Leave a Reply