In this section, we will explore one of the fundamental control structures in Fortran: the if
statement. Control structures allow you to dictate the flow of your program based on certain conditions. The if
statement is used to execute a block of code only if a specified condition is true.
Key Concepts
- Basic
if
Statement: Executes a block of code if a condition is true. if-else
Statement: Provides an alternative block of code if the condition is false.if-else if-else
Ladder: Allows multiple conditions to be checked in sequence.- Nested
if
Statements: Anif
statement inside anotherif
statement.
Basic if
Statement
The basic if
statement in Fortran has the following syntax:
Example
program basic_if_example implicit none integer :: a a = 10 if (a > 5) then print *, "a is greater than 5" end if end program basic_if_example
In this example, the program checks if the variable a
is greater than 5. Since a
is 10, the condition is true, and the message "a is greater than 5" is printed.
if-else
Statement
The if-else
statement provides an alternative block of code to execute if the condition is false.
Syntax
if (condition) then ! Code to execute if condition is true else ! Code to execute if condition is false end if
Example
program if_else_example implicit none integer :: a a = 3 if (a > 5) then print *, "a is greater than 5" else print *, "a is not greater than 5" end if end program if_else_example
In this example, since a
is 3, the condition a > 5
is false, and the message "a is not greater than 5" is printed.
if-else if-else
Ladder
The if-else if-else
ladder allows you to check multiple conditions in sequence.
Syntax
if (condition1) then ! Code to execute if condition1 is true else if (condition2) then ! Code to execute if condition2 is true else ! Code to execute if none of the above conditions are true end if
Example
program if_elseif_else_example implicit none integer :: a a = 7 if (a > 10) then print *, "a is greater than 10" else if (a > 5) then print *, "a is greater than 5 but less than or equal to 10" else print *, "a is 5 or less" end if end program if_elseif_else_example
In this example, since a
is 7, the condition a > 5
is true, and the message "a is greater than 5 but less than or equal to 10" is printed.
Nested if
Statements
You can nest if
statements within each other to check multiple conditions.
Example
program nested_if_example implicit none integer :: a, b a = 8 b = 12 if (a > 5) then if (b > 10) then print *, "a is greater than 5 and b is greater than 10" else print *, "a is greater than 5 but b is not greater than 10" end if else print *, "a is not greater than 5" end if end program nested_if_example
In this example, since a
is 8 and b
is 12, both conditions are true, and the message "a is greater than 5 and b is greater than 10" is printed.
Practical Exercises
Exercise 1
Write a program that checks if a number is positive, negative, or zero.
Solution
program check_number implicit none integer :: num print *, "Enter a number:" read *, num if (num > 0) then print *, "The number is positive." else if (num < 0) then print *, "The number is negative." else print *, "The number is zero." end if end program check_number
Exercise 2
Write a program that checks if a year is a leap year. A year is a leap year if it is divisible by 4 but not by 100, except if it is also divisible by 400.
Solution
program check_leap_year implicit none integer :: year print *, "Enter a year:" read *, year if (mod(year, 400) == 0) then print *, "The year is a leap year." else if (mod(year, 100) == 0) then print *, "The year is not a leap year." else if (mod(year, 4) == 0) then print *, "The year is a leap year." else print *, "The year is not a leap year." end if end program check_leap_year
Common Mistakes and Tips
- Forgetting
end if
: Always remember to close yourif
statements withend if
. - Incorrect Condition Syntax: Ensure your conditions are correctly written and logical.
- Nested
if
Statements: Be careful with indentation and structure to avoid confusion.
Conclusion
In this section, we covered the if
statement in Fortran, including its basic form, if-else
statements, if-else if-else
ladders, and nested if
statements. Understanding these control structures is crucial for controlling the flow of your programs based on conditions. Practice the exercises provided to reinforce your understanding and prepare for more complex control structures in the next sections.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability