In this section, we will cover the fundamental syntax and structure of Fortran programs. Understanding these basics is crucial for writing and reading Fortran code effectively.

Key Concepts

  1. Program Structure
  2. Comments
  3. Identifiers
  4. Data Types
  5. Constants and Variables
  6. Basic Input/Output

  1. Program Structure

A Fortran program typically consists of the following parts:

  • Program name
  • Declarations
  • Executable statements
  • End statement

Example:

program HelloWorld
    implicit none
    print *, "Hello, World!"
end program HelloWorld

Explanation:

  • program HelloWorld: Declares the start of the program named HelloWorld.
  • implicit none: Ensures all variables must be explicitly declared.
  • print *, "Hello, World!": Prints the string "Hello, World!" to the console.
  • end program HelloWorld: Marks the end of the program.

  1. Comments

Comments are used to explain code and are ignored by the compiler. In Fortran, comments start with an exclamation mark (!).

Example:

program CommentExample
    implicit none
    ! This is a comment
    print *, "Comments are ignored by the compiler"
end program CommentExample

  1. Identifiers

Identifiers are names given to various program elements such as variables, constants, and functions. They must start with a letter and can be followed by letters, digits, and underscores.

Example:

program IdentifiersExample
    implicit none
    integer :: myVariable
    myVariable = 10
    print *, myVariable
end program IdentifiersExample

  1. Data Types

Fortran supports several data types, including:

  • Integer
  • Real
  • Complex
  • Logical
  • Character

Example:

program DataTypesExample
    implicit none
    integer :: i
    real :: x
    complex :: z
    logical :: flag
    character(len=20) :: name

    i = 10
    x = 3.14
    z = (1.0, 2.0)
    flag = .true.
    name = "Fortran"

    print *, i, x, z, flag, name
end program DataTypesExample

  1. Constants and Variables

Constants are fixed values, while variables can change during program execution. Constants are declared using the parameter attribute.

Example:

program ConstantsVariablesExample
    implicit none
    integer, parameter :: max_value = 100
    real :: radius, area

    radius = 5.0
    area = 3.14 * radius**2

    print *, "Max Value:", max_value
    print *, "Area:", area
end program ConstantsVariablesExample

  1. Basic Input/Output

Fortran uses print for output and read for input.

Example:

program BasicIOExample
    implicit none
    integer :: num

    print *, "Enter a number:"
    read *, num
    print *, "You entered:", num
end program BasicIOExample

Practical Exercises

Exercise 1: Simple Arithmetic

Write a Fortran program that reads two integers from the user, adds them, and prints the result.

Solution:

program AddTwoNumbers
    implicit none
    integer :: num1, num2, sum

    print *, "Enter first number:"
    read *, num1
    print *, "Enter second number:"
    read *, num2

    sum = num1 + num2
    print *, "The sum is:", sum
end program AddTwoNumbers

Exercise 2: Circle Area Calculation

Write a Fortran program that reads the radius of a circle from the user and calculates the area.

Solution:

program CircleArea
    implicit none
    real :: radius, area
    real, parameter :: pi = 3.14159

    print *, "Enter the radius of the circle:"
    read *, radius

    area = pi * radius**2
    print *, "The area of the circle is:", area
end program CircleArea

Common Mistakes and Tips

  • Forgetting implicit none: Always include implicit none to avoid undeclared variables.
  • Incorrect variable names: Ensure variable names start with a letter and contain no spaces or special characters.
  • Mismatched data types: Be careful with data types, especially when performing operations.

Conclusion

In this section, we covered the basic syntax and structure of Fortran programs, including program structure, comments, identifiers, data types, constants, variables, and basic input/output. These fundamentals are essential for writing and understanding Fortran code. In the next section, we will delve deeper into variables and data types.

© Copyright 2024. All rights reserved