0% found this document useful (0 votes)
62 views30 pages

Introduction To FORTRAN

Fortran was originally developed in 1957 by John Backus as a way to make programming easier. It includes basic features like comments, identifiers, data types, and control structures. Programming in Fortran involves writing code in a text editor, then compiling it using a compiler like GFortran. The code contains elements like program entry points marked with the "program" keyword, and comments denoted with an exclamation point.

Uploaded by

Wisdom Ojimah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views30 pages

Introduction To FORTRAN

Fortran was originally developed in 1957 by John Backus as a way to make programming easier. It includes basic features like comments, identifiers, data types, and control structures. Programming in Fortran involves writing code in a text editor, then compiling it using a compiler like GFortran. The code contains elements like program entry points marked with the "program" keyword, and comments denoted with an exclamation point.

Uploaded by

Wisdom Ojimah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Introduction to FORTRAN

Fortran, which in full means Formula Translation was originally


developed by IBM employee John Backus in 1957. The motivation
for this was because of the arduous nature of programming at that
time.

There are basic features possessed by every programming language,


and FORTRAN is no exception. These are:

1. Programming Environment
Programming environment refer to the compiler and other
associated development tools. Since FORTRAN is a compiled
language, one would need a text editor to write the code, and a
compiler to compile the code written to a format that can be
understood by the computer. Simple text editors like Notepad to
advanced ones like Visual Studio Code can be used. For program
compilation, Open Source compilers like GFortran can be used.
Fortran files can have a variety of files extensions such as .f90, .f,
although .f90 is the most accepted standard.

2. Program Entry Point


A program entry is the point in the program where execution starts.
In FORTRAN the entry point is the “program” keyword followed
by whatever the user decided to call that program. A program that
add two numbers for example would have this format:
program addNumber
implicit none
! Variable declaration
! Execution statement
end program addNumbers

Where “program addNumbers” is the program entry point and any


code between it and the “end program addNumbers” statement is
executed by the computer.

3. Comments
Comments are used to add context to a program. They help explain
what the program does and they are not compiled by the compiler.
In FORTRAN, the exclamation mark signifies a comment and
anything after it is ignored by the compiler. Example:
program addNumbers
implicit none
! This shows how to use comments in FORTRAN
INTEGER :: num1, num2, result
num1 = 2
num2 = 2
result = num1 + num2
! The answer is 4, but the compiler won’t show
this write up
write(*,*) “The answer is :”, num1 + num2
end program addNumbers
The output would be:
4. Delimiters
Delimiters are special and unique character or a series of characters
that indicate the beginning or end of a specific statement, string or
function body set. In FORTRAN, the delimiter is a blank space and
multiple blank spaces are ignored. In the example above, the blank
space makes the compiler skip compilation to the next line.

5. Whitespaces
Whitespaces is any character or series of characters which creates
space on a page, but does not display a visible mark. An example
of this is tabs. For example:
program showWhiteSpace
implicit none
write(*,*) 'This contains whitespaces'
end program showWhiteSpace
The output would be:
6. Identifier
Identifiers are names given to identify different entities in a
program. In FORTRAN an identifier must satisfy the following
conditions:
 It must not exceed 31 characters
 The first character must be a letter
 The remaining character(s) can be a letter, a number, or an
underscore. Any other value is not allowed.
 Identifiers are case-insensitive. Therefore, num1 and NUM1
would refer to the same thing, although good practice
advocates for consistency.
An example of an identifier is:
program identifierExample
implicit none
INTEGER :: number !number here is an identifier
write(*,*) 'This contains whitespaces'
end program identifierExample

7. Keywords
Keywords are predefined, reserved words that have special
meaning to the computer. They cannot be used as identifier names.
FORTRAN has the following keywords:
8. Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulation. There are 3 operators in
FORTRAN, and they are:
 Arithmetic: The available operators are:

An example of the this operator is:


program arithematicOperator
implicit none
INTEGER :: num1, num2, result
num1 = 20
num2 = 10
result = num1 + num2
write(*,*) result
end program arithematicOperator
 Relational: The available operators are:

An example of this operator in practice is:


program relationalOperator
implicit none
INTEGER :: num1, num2
LOGICAL :: result
num1 = 20
num2 = 10
result = num1 .ne. num2
write(*,*) result
end program relationalOperator

 Logical: This operator only works on logical .TRUE.


and .FALSE. values.

An example of this operator is:


program logicalOperator
implicit none
LOGICAL :: condition1, condition2, result
result = condition1 .or. condition2
write(*,*) result
end program logicalOperator

9. Data type
A data type specifies the type of value of a variable and the type of
mathematical, relational or logical operation that can be applied on
the variable. FORTRAN has 5 intrinsic data types, and they are:
 Integer type: This data type can hold integer values (whole
numbers that are either positive or negative).
program integerDataType
implicit none
INTEGER :: a = 2
write(*,*) a
end program integerDataType
 Real type: This data type holds floating point numbers.
program realDataType
implicit none
REAL :: a = 2.9
write(*,*) a
end program realDataType

 Logical type: There are only two logical


values: .true. and .false.
program logicalDataType
implicit none
LOGICAL :: isTrue = .true.
write(*,*) isTrue
end program logicalDataType

 Character type: This data type stores character and


numbers. The length of the characters can be specified
using the len specifier.
program characterDataType
implicit none
CHARACTER (len = 20) :: name
name = “Wisdom Ojimah”
write(*,*) name
end program characterDataType
 Complex type: This is used for storing complex
numbers. A complex number has two parts, the real
part and the imaginary part.
program complexDataType
implicit none
CHARACTER (len = 20) :: name
Name = “Wisdom Ojimah”
write(*,*) name
end program complexDataType

Older versions of Fortran allowed a feature called implicit


typing, i.e., you do not have to declare the variables before
use. If a variable is not declared, then the first letter of its
name will determine its type.
Variable names starting with i, j, k, l, m, or n, are
considered to be for integer variable and others are real
variables. However, you must declare all the variables as it
is good programming practice. For that you start your
program with the implicit none statement.

 Functions
A FORTRAN function is a procedure whose result is a single
number, logical value, character string or array. A function should
not modify its arguments.
The returned quantity is known as function value, and it is
denoted by the function name. The general syntax for
defining a function is:
function name(arg1, arg2, ....)
[declarations, including those for the
arguments]
[executable statements]
end function [name]
Let’s take an example to get the square root of a number
using functions:
program numberRoot
implicit none
REAL :: number, getNumberRoot
number = getNumberRoot(10.00)
write(*,*) "The root of 10 is ", number
end program numberRoot

function getNumberRoot(num)
implicit none
REAL :: getNumberRoot
REAL :: num
getNumberRoot = sqrt(num)
end function getNumberRoot

 Control structure
Control Structures are the blocks that analyze
variables and choose directions in which to go based
on given parameters. There are different basic control
structures in programming and they are:
o Loops: A loop statement allows us to execute a
statement or group of statements multiple
times. The general structure of a loop is:

FORTRAN has different loop constructs and they


are:
 Do loop: The do loop construct enables a
statement, or a series of statements, to be
carried out iteratively, while a given
condition is true. The flow diagram of a do
loop is:
The general syntax is:
do var = start, stop [,step]
! statement(s)

end do
Where,
 the loop variable var should be an integer
 start is initial value
 stop is the final value
 step is the increment, if this is omitted, then the
variable var is increased by unity (+1)

A program that computes the factorial of a number 1


to 10 is
program factorial
implicit none
INTEGER :: fact, number
fact = 10
do number = 1, 10
fact = fact * number
write(*,*) fact
end do
end program factorial:

 Do While loop
It repeats a statement or a group of statements while a
given condition is true. It tests the condition before
executing the loop body. The general syntax is:
do while (logical expr)
statements
end do

Rewriting the previous example using d while


loop we would have:
program factorial
implicit none
INTEGER :: fact, number
number = 1
fact = 10
do while(number <= 10)
fact = fact * number
write(*,*) fact
number = number + 1
end do
end program factorial

It is also possible to nest loops inside


another loop:
program nestedLoop
implicit none
INTEGER :: i, j, k
do i = 1, 3
do j = 1, 3
do k = 1, 3
write(*,*) i, j, k
end do
end do
end do
end program nestedLoop
 Selection control structure
In selection control structures, conditional
statements are features of a programming
language which perform different computations
or actions depending on whether a programmer-
specified Boolean condition evaluates to true
or false. There are different selection
constructs in FORTRAN, and they are:
o If… then construct: A if… then… end
if statement consists of a logical expression
followed by one or more statements.

A program that checks if a user’s grade is


A is:
program markGradeA
implicit none
REAL :: marks
marks = 90.4
if (marks > 90.0) then
write(*,*) "Grade A"
end if
end program markGradeA
 If… then else: This is a normal if… else
statement followed by an else statement that
executes if the if condition fails.
Building on the previous example, we can have an
else condition to show a message if then mark isn’t
an A.
program markGradeA
implicit none
REAL :: marks
marks = 80.4
if (marks > 90.0) then
write(*,*) "Grade A"
else
write(*,*) “Not an A”
end if
end program markGradeA
 Nested if: We can nest if statements to
check for complex conditions. We can modify
the existing example to display either A B
or C depending on the score.
program markGradeA
implicit none
REAL :: marks
marks = 80.4
if (marks < 90.0) then
if (marks < 80.0) then
write(*,*) "Grade C"
else
write(*,*) "Grade B"
end if
else
write(*,*) "Grade A"
end if
end program markGradeA
 Select Case
A select case statement allows a variable to be tested for
equality against a list of values. Each value is called a
case, and the variable being selected on is checked for
each select case. The syntax for the select case construct
is as follows:
select case (expression)
case (selector1)
! some statements
... case (selector2)
! other statements
...
case default
! more statements
...
end select

Its program flow is as follows:


An example of a program that gives remarks
based on a student’s grade:
program selectCaseProg
implicit none
CHARACTER :: grade = 'B'

select case (grade)

case ('A')
write(*,*) "Excellent!"

case ('B')

case ('C')
write(*,*) "Well done"
case ('D')
write(*,*) "You passed"

case ('F')
write(*,*) "Better try again"

case default
write(*,*) "Invalid grade"

end select

write(*,*) "Your grade is ", grade

end program selectCaseProg

 I/O Operations
o Input: This allows the user to enter
information. The read statement is used
to collect user data.
program input
implicit none

INTEGER :: number

read(*,*) number
write(*,*) number
end program input

o Output: This is used to display the


result of a variable or computation. The
write statement is used.
program input
implicit none

write(*,*) "Hello World"


end program input
Comparison with PHP
PHP is a recursive acronym for PHP: Hypertext
Preprocessor. It was originally created by
Danish-Canadian programmer, Rasmus Lerdorf in
1994. It is mostly used in web development and
can be embedded in HTML pages.

As far as programming environment goes it is


quite easy to set up a PHP environment. While
one can download the binary files, it is most
times easier to download tools than come
bundled with PHP by default. Such tools
include XAMP, Laragon, Mamp, etc. Unlike
FORTRAN that expects the programmer to tell
the compiler that type of data a variable
holds, PHP by default allows a programmer to
declare a variable without specifying its
type. It uses what is known as inferred types.
This means that the PHP interpreter decides
the data type of a variable at runtime,
although recent versions of PHP has made it
possible for one to use static typing in our
code.

 Program entry point


In PHP execution starts with the opening and
closing PHP tags. These tags are <?php
which is the opening tag and ?> which is the
closing tag. The opening tag is mandatory
while the closing tag is optional.
<?php
echo “Hello World”;

 Comments
To specify comments in PHP, we // for single line
comments or #. For multi-line comments, we make use
of /* */.

 Delimiters
PHP’s delimiter is the semicolon (;)
<?php
$number = 10; //The semicolon is the
delimeter
$anotherVariable = 20;

 Identifiers
In PHP, identifiers begin with the $ sign followed
by the name of the identifier.

 I/O Operations
The echo statement is used for printing out
variables and computational values.

 Selection control structures


For the if statement, PHP uses the if
keyword like FORTRAN, but uses curly braces
to denote a program block ({ and }) instead
of the then keyword in FORTRAN.
PHP uses the switch statement instead of the
select case in FORTRAN for testing equality
against a list of values.
<?php
$number = 10;
switch ($number) {
case number >= 5:
echo "Grade A";
break;
case number > 5 && number <= 3:
echo "Grade B";
break;
default:
echo "Grade C";
break;
}

 Function
PHP also uses the function keyword to followed
by the name of the function. It however uses
curly braces to signify program block.

References
1.https://www.php.net/manual/en/intro-
whatis.php
2.https://www.tutorialspoint.com/fortran/
fortran_do_while_loop.htm
3.https://www.tutorialspoint.com/fortran/
nested_if_construct.htm
4.https://www.tutorialspoint.com/fortran/
select_case_construct.htm
5.https://www.tutorialspoint.com/fortran/
fortran_procedures.htm
6.https://www.tutorialspoint.com/fortran/
fortran_basic_input_output.htm

You might also like