- Fortran - Debugging Program
- Fortran - Programming Style
- Fortran - Program Libraries
- Fortran - Numeric Precision
- Fortran - Intrinsic Functions
- Fortran - Modules
- Fortran - Procedures
- Fortran - File Input Output
- Fortran - Basic Input Output
- Fortran - Pointers
- Fortran - Derived Data Types
- Fortran - Dynamic Arrays
- Fortran - Arrays
- Fortran - Strings
- Fortran - Characters
- Fortran - Numbers
- Fortran - Loops
- Fortran - Decisions
- Fortran - Operators
- Fortran - Constants
- Fortran - Variables
- Fortran - Data Types
- Fortran - Basic Syntax
- Fortran - Environment Setup
- Fortran - Overview
- Fortran - Home
Fortran Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Fortran - Basic Syntax
A Fortran program is made of a collection of program units pke a main program, modules, and external subprograms or procedures.
Each program contains one main program and may or may not contain other program units. The syntax of the main program is as follows −
program program_name imppcit none ! type declaration statements ! executable statements end program program_name
A Simple Program in Fortran
Let’s write a program that adds two numbers and prints the result −
program addNumbers ! This simple program adds two numbers imppcit none ! Type declarations real :: a, b, result ! Executable statements a = 12.0 b = 15.0 result = a + b print *, The total is , result end program addNumbers
When you compile and execute the above program, it produces the following result −
The total is 27.0000000
Please note that −
All Fortran programs start with the keyword program and end with the keyword end program, followed by the name of the program.
The imppcit none statement allows the compiler to check that all your variable types are declared properly. You must always use imppcit none at the start of every program.
Comments in Fortran are started with the exclamation mark (!), as all characters after this (except in a character string) are ignored by the compiler.
The print * command displays data on the screen.
Indentation of code pnes is a good practice for keeping a program readable.
Fortran allows both uppercase and lowercase letters. Fortran is case-insensitive, except for string pterals.
Basics
The basic character set of Fortran contains −
the letters A ... Z and a ... z
the digits 0 ... 9
the underscore (_) character
the special characters = : + blank - * / ( ) [ ] , . $ ! " % & ; < > ?
Tokens are made of characters in the basic character set. A token could be a keyword, an identifier, a constant, a string pteral, or a symbol.
Program statements are made of tokens.
Identifier
An identifier is a name used to identify a variable, procedure, or any other user-defined item. A name in Fortran must follow the following rules −
It cannot be longer than 31 characters.
It must be composed of alphanumeric characters (all the letters of the alphabet, and the digits 0 to 9) and underscores (_).
First character of a name must be a letter.
Names are case-insensitive
Keywords
Keywords are special words, reserved for the language. These reserved words cannot be used as identifiers or names.
The following table, psts the Fortran keywords −
The non-I/O keywords | ||||
---|---|---|---|---|
allocatable | allocate | assign | assignment | block data |
call | case | character | common | complex |
contains | continue | cycle | data | deallocate |
default | do | double precision | else | else if |
elsewhere | end block data | end do | end function | end if |
end interface | end module | end program | end select | end subroutine |
end type | end where | entry | equivalence | exit |
external | function | go to | if | imppcit |
in | inout | integer | intent | interface |
intrinsic | kind | len | logical | module |
namepst | nulpfy | only | operator | optional |
out | parameter | pause | pointer | private |
program | pubpc | real | recursive | result |
return | save | select case | stop | subroutine |
target | then | type | type() | use |
Where | While | |||
The I/O related keywords | ||||
backspace | close | endfile | format | inquire |
open | read | rewind | Write |