Site icon StudyGyaan

Fortran Programming Language Cheatsheet

Fortran, short for Formula Translation, is one of the oldest programming languages still in use today. Developed in the 1950s for scientific and engineering calculations, Fortran has evolved over the years and remains a crucial language in fields like numerical simulation, scientific computing, and high-performance computing. This cheatsheet serves as a quick reference guide for Fortran programming, covering key syntax and concepts.

1. Basic Structure:

PROGRAM ProgramName
  ! Declarations
  IMPLICIT NONE

  ! Main program statements

END PROGRAM ProgramName

2. Data Types:

INTEGER :: i, j
REAL :: x, y
CHARACTER(LEN=20) :: name
LOGICAL :: flag

3. Arrays:

REAL, DIMENSION(3) :: vector
INTEGER, DIMENSION(3, 4) :: matrix

4. Control Structures:

a. Conditional Statements:

IF (condition) THEN
  ! Code block if condition is true
ELSEIF (another_condition) THEN
  ! Code block if another condition is true
ELSE
  ! Code block if no conditions are true
END IF

b. Loops:

DO Loop:

DO i = 1, 10, 2
  ! Loop body
END DO

DO WHILE Loop:

DO WHILE (condition)
  ! Loop body
END DO

DO UNTIL Loop:

DO UNTIL (condition)
  ! Loop body
END DO

5. Subroutines and Functions:

SUBROUTINE MySubroutine(arg1, arg2)
  ! Subroutine body
END SUBROUTINE

FUNCTION MyFunction(arg1, arg2) RESULT(result)
  ! Function body
END FUNCTION

6. File Handling:

OPEN(UNIT=1, FILE='data.txt', STATUS='OLD', ACTION='READ')
READ(1, *) variable1, variable2
WRITE(1, *) variable3, variable4
CLOSE(UNIT=1)

7. Modules:

MODULE MyModule
  ! Module content
CONTAINS
  ! Subroutines and functions
END MODULE MyModule

8. Advanced Concepts:

a. Pointers:

REAL, POINTER :: ptr
ALLOCATE(ptr)

b. Derived Types:

TYPE :: MyType
  INTEGER :: value1
  REAL :: value2
END TYPE MyType

9. Compiler Directives:

!$OMP PARALLEL DO
DO i = 1, N
  ! Parallelized loop body
END DO

10. Error Handling:

ERROR STOP 'An error occurred.'

Fortran may seem archaic to some, but its efficiency and legacy in scientific computing make it indispensable. This cheatsheet provides a concise overview of Fortran essentials, offering a quick reference for both beginners and experienced programmers. For more in-depth learning, refer to the official Fortran documentation and explore advanced features for specialized applications.

1. What is Fortran primarily used for?

Fortran is mainly used for scientific and engineering calculations, numerical simulation, and high-performance computing due to its efficiency in handling complex mathematical operations.

2. How do I declare an array in Fortran?

To declare an array in Fortran, use syntax like REAL, DIMENSION(3) :: vector for one-dimensional arrays or INTEGER, DIMENSION(3, 4) :: matrix for two-dimensional arrays.

3. What is the purpose of a Fortran module?

A Fortran module encapsulates procedures, data, and other constructs, enhancing code organization and reusability. It helps in managing and structuring large-scale Fortran programs.

4. Can Fortran be used for parallel programming?

Yes, Fortran supports parallel programming through directives like OpenMP. These directives facilitate the creation of parallel regions in the code, enhancing performance on multi-core processors.

5. How do I handle errors in Fortran?

Fortran uses the ERROR STOP statement to halt program execution and display an error message. Programmers can customize error messages to aid in debugging and troubleshooting.

Exit mobile version