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

  1. Basic if Statement: Executes a block of code if a condition is true.
  2. if-else Statement: Provides an alternative block of code if the condition is false.
  3. if-else if-else Ladder: Allows multiple conditions to be checked in sequence.
  4. Nested if Statements: An if statement inside another if statement.

Basic if Statement

The basic if statement in Fortran has the following syntax:

if (condition) then
    ! Code to execute if condition is true
end if

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 your if statements with end 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.

© Copyright 2024. All rights reserved