Fortran Ramp-Up For HTML Programmers
Fortran Ramp-Up For HTML Programmers
A PRACTICAL GUIDE
INTRODUCTION TO FORTRAN FOR HTML
PROGRAMMERS
Fortran, short for Formula Translation, is one of the oldest high-level
programming languages, originally developed in the 1950s. Unlike HTML,
which is a markup language designed to structure and present content on the
web, Fortran is a general-purpose programming language primarily used in
scientific and numerical computing. Its main focus is on performing complex
mathematical calculations and data processing efficiently.
While HTML describes what web pages look like using tags and attributes,
Fortran defines how computations should be carried out through a
procedural programming approach. This fundamental difference highlights
the distinct roles these languages play: HTML creates interactive visual
layouts and interfaces, whereas Fortran handles intensive numerical tasks
often required in fields such as physics, engineering, and climate modeling.
Despite being over six decades old, Fortran remains relevant and widely used
in high-performance computing (HPC) environments. Its design emphasizes
speed and efficiency, making it ideal for simulations, scientific research, and
large-scale numerical analysis. Modern versions of Fortran have evolved to
support advanced programming features, ensuring that it stays current with
contemporary computing needs.
program hello
print *, "Hello, Fortran!"
end program hello
Fortran uses the ! symbol to mark comments, which can appear anywhere
on a line. This is somewhat similar to HTML comments, but HTML requires the
full <!-- comment --> syntax. For example:
A crucial difference lies in how the two languages are processed: HTML is
interpreted by web browsers directly, reading the markup and rendering the
web page dynamically. Fortran, on the other hand, is a compiled language.
This means the Fortran source code must be converted into machine code by
a compiler before it can be executed. The compiled program then runs
independently, performing calculations with high efficiency.
• INTEGER: Used for whole numbers without decimal points. Typical use
cases include counters, indices, and discrete values.
• REAL: Represents single-precision floating-point numbers, suitable for
decimal values where moderate precision is acceptable.
• DOUBLE PRECISION: Provides higher precision floating-point numbers,
useful for scientific calculations that require greater accuracy.
• COMPLEX: Stores complex numbers with real and imaginary parts,
commonly used in engineering and physics applications.
• LOGICAL: Holds Boolean values .TRUE. or .FALSE. , similar to true/
false in JavaScript.
• CHARACTER: Represents text strings, analogous to JavaScript strings,
but requires specification of the string length.
DECLARING VARIABLES
integer :: count = 10
real :: temperature
logical :: isActive = .true.
character(len=20) :: name = "Fortran User"
Unlike JavaScript where a variable can hold any type at any point (dynamically
typed), Fortran variables have fixed types defined at compile time. This means
you cannot assign a string to an integer variable later on, preventing some
common runtime errors but requiring more upfront planning.
if (x > 0) then
print *, "Positive number"
print *, "Value of x:", x
end if
if (x > 0) then
print *, "Positive"
else if (x == 0) then
print *, "Zero"
else
print *, "Negative"
end if
do i = 1, 5
print *, "Iteration:", i
end do
While Fortran does not have a built-in DO UNTIL , equivalent behavior can
be implemented with a DO loop and an exit condition using EXIT
statements.
A function returns a single value and can be used in expressions, much like
JavaScript functions that return results. In contrast, a subroutine performs
actions but does not return a value directly; it resembles a void function in
other languages and typically modifies variables passed to it or performs
input/output operations.
PARAMETER PASSING
program demo
integer :: num
real :: sq
num = 5
call increment(num) ! num becomes 6
sq = square(3.0) ! sq becomes 9.0
Notice that square is called like a function and returns a value, while
increment is invoked with the call statement and modifies its argument
directly. The intent attribute clarifies how parameters are used:
In Fortran, input comes from the keyboard or a file, but console input is
typical for simple programs. The READ statement retrieves data entered by
the user, while WRITE displays text or variables to the screen.
SIMPLE EXAMPLES
program io_example
integer :: age
write(*,*) "Enter your age:"
read(*,*) age
write(*,*) "You entered age:", age
end program io_example
./program
If there are syntax errors, the compiler will display messages to help you fix
the code. Make sure to check error lines carefully. Unlike instant HTML
preview in browsers, Fortran requires this compile-run cycle to see changes.
Learning basic compiler commands and error interpretation is key for a
smooth Fortran workflow.