0% found this document useful (0 votes)
9 views9 pages

Fortran Ramp-Up For HTML Programmers

This document serves as a practical guide for HTML programmers transitioning to Fortran, highlighting the differences between the two languages. It covers Fortran's syntax, structure, data types, control structures, subprograms, and the compilation process, emphasizing its role in scientific and numerical computing. The guide aims to provide HTML programmers with a foundational understanding of Fortran's capabilities and programming paradigms.

Uploaded by

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

Fortran Ramp-Up For HTML Programmers

This document serves as a practical guide for HTML programmers transitioning to Fortran, highlighting the differences between the two languages. It covers Fortran's syntax, structure, data types, control structures, subprograms, and the compilation process, emphasizing its role in scientific and numerical computing. The guide aims to provide HTML programmers with a foundational understanding of Fortran's capabilities and programming paradigms.

Uploaded by

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

FORTRAN 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.

For HTML programmers, understanding Fortran opens the door to a very


different kind of programming paradigm where precise control over
computations and performance optimization are critical. This guide will help
you bridge that gap by explaining Fortran’s syntax, structure, and typical use
cases in an approachable way.
BASIC SYNTAX AND STRUCTURE OF FORTRAN
COMPARED TO HTML
Understanding the basic syntax and structure of Fortran programs is
essential for HTML programmers transitioning to this powerful language.
While HTML organizes content with nested tags and attributes, Fortran uses a
set of well-defined program units and statements to describe procedural logic
and computation.

PROGRAM UNITS AND OVERALL STRUCTURE

A typical Fortran program is composed of program units, such as the main


program , modules , and subroutines . These units encapsulate
different parts of the code, much like HTML elements group related content,
but in Fortran they define executable blocks rather than visual structures.

For example, a minimal Fortran program looks like this:

program hello
print *, "Hello, Fortran!"
end program hello

Here, the program starts with the program keyword followed by an


identifier, contains statements such as print for output, and ends with
end program . This structure contrasts with HTML's tree of nested tags but
serves to define the flow of computation.

STATEMENTS AND KEYWORDS

Fortran programs consist of statements that perform actions or declare data.


Statements typically start with a keyword, like print , if , do , or
integer . Unlike HTML tags, which are case-insensitive, Fortran keywords
are also case-insensitive, meaning PRINT , print , and Print are
equivalent.

Statements in Fortran are usually ended by the end of a line (line


termination), so no specific character is needed to mark the end, unlike
languages that require semicolons. Older Fortran versions restricted lines to
72 characters, but modern compilers support free-form formatting.
COMMENTS AND CODE READABILITY

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:

! This is a comment in Fortran


print *, "Output message" ! This prints text

MODULES VS. HTML COMPONENTS

Modern Fortran supports modules to group related procedures, variables,


and definitions to promote code reuse and organization. This is conceptually
akin to HTML components or reusable sections but applied to executable
code rather than content structure.

COMPILATION VS. INTERPRETATION

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.

DATA TYPES AND VARIABLES IN FORTRAN


In Fortran, variables store data that your program manipulates, similar to
variables in JavaScript or other scripting languages familiar to HTML
programmers. However, Fortran is a strongly typed language, meaning you
must explicitly declare the type of each variable before using it. This helps the
compiler optimize performance and catch errors early.

FUNDAMENTAL DATA TYPES

• 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

Variables are declared by specifying their type followed by the variable


name(s). Initialization can be done in the declaration or later in the code. For
example:

integer :: count = 10
real :: temperature
logical :: isActive = .true.
character(len=20) :: name = "Fortran User"

Here, count is an integer set to 10, temperature is a real number


(uninitialized initially), isActive is a logical set to true, and name is a
character string capable of holding up to 20 characters.

COMPARISONS TO JAVASCRIPT VARIABLES

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.

Similar to JavaScript's let or const , Fortran's type declaration controls the


data stored and how it is treated during operations, but Fortran adds rigor by
enforcing type consistency and offering special numeric types designed for
high-performance scientific computing.
CONTROL STRUCTURES: CONDITIONAL
STATEMENTS AND LOOPS
Control structures in Fortran guide the flow of a program, deciding which
instructions to execute based on conditions or repetitions. Unlike HTML,
which uses scripting languages like JavaScript or templating engines to
implement conditional logic and loops, Fortran provides built-in, structured
control flow statements that are fundamental for procedural programming.

CONDITIONAL STATEMENTS: IF AND SELECT CASE

Fortran's IF statement comes in several forms:

• Simple IF tests a single condition and executes one statement if true:

if (x > 0) print *, "Positive number"

• Block IF allows multiple statements when the condition is true, ended


explicitly by END IF :

if (x > 0) then
print *, "Positive number"
print *, "Value of x:", x
end if

• IF-ELSE IF-ELSE handles multiple branches:

if (x > 0) then
print *, "Positive"
else if (x == 0) then
print *, "Zero"
else
print *, "Negative"
end if

Another useful multi-way branching structure is SELECT CASE , which


simplifies checking a variable against many values:
select case (day)
case (1)
print *, "Monday"
case (2)
print *, "Tuesday"
case default
print *, "Other day"
end select

LOOPS: DO, DO WHILE, AND DO UNTIL

Fortran provides the DO loop as the main repeating structure:

• Counted DO loop repeats a block a fixed number of times:

do i = 1, 5
print *, "Iteration:", i
end do

• DO WHILE loop repeats while a condition is true:

do while (x < 10)


x = x + 1
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.

In all loops and conditional blocks, explicit END DO and END IF


statements mark their end, which improves readability and structure clarity.
Indentation, though not enforced by the compiler, is strongly recommended
to visually organize code, especially for programmers transitioning from
HTML where indentation helps readability but is not syntactically significant.

Unlike HTML, where dynamic behavior often relies on embedded JavaScript or


server-side templating languages, Fortran’s control structures are native and
part of the compiled code's logic, which provides both performance benefits
and clarity for numerical computation workflows.
SUBPROGRAMS: FUNCTIONS AND SUBROUTINES
In Fortran, subprograms enable code reuse and modularity through two
main types: functions and subroutines. Both are similar to JavaScript
functions, but with differences important to understand for HTML
programmers venturing into Fortran.

FUNCTIONS VS. SUBROUTINES

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

Fortran passes arguments by reference by default. This means changes inside


a subprogram affect the original variables outside it. This differs from
JavaScript’s often “pass by value” (for primitives), so in Fortran, be mindful that
input variables can be modified unless explicitly protected.

EXAMPLE FORTRAN CODE

! Function example that returns the square of a number


function square(x) result(res)
real, intent(in) :: x
real :: res
res = x * x
end function square

! Subroutine example that increments a number


subroutine increment(val)
integer, intent(inout) :: val
val = val + 1
end subroutine increment

program demo
integer :: num
real :: sq
num = 5
call increment(num) ! num becomes 6
sq = square(3.0) ! sq becomes 9.0

print *, "Incremented num:", num


print *, "Square of 3:", sq
end program demo

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 — input only (read-only)


• out — output only (written inside subprogram)
• inout — input and modified (read/write)

Compared to JavaScript, Fortran's explicit parameter passing modes and


compiled structure encourage safer, clearer code when managing inputs and
outputs in subprograms.

BASIC INPUT AND OUTPUT IN FORTRAN


Fortran handles input and output primarily through READ and WRITE
statements, which interact directly with the console. This is quite different
from HTML, where user input is collected via forms and output is displayed
within a browser using markup and scripting.

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

Here, write(*,*) outputs a prompt, and read(*,*) reads an integer


from the user. The * specifies the default input/output device (console),
making the code straightforward. Unlike HTML’s event-driven input, Fortran
input/output is sequential and console-based.

COMPILING AND RUNNING FORTRAN CODE


Unlike HTML files, which are directly rendered by web browsers, Fortran
source code requires compilation before execution. Compilation transforms
the human-readable Fortran code into machine code that a computer can
run. A commonly used compiler is gfortran , available on many platforms.

To compile a Fortran program called program.f90 , you would typically run


the following command in a terminal or command prompt:

gfortran program.f90 -o program

This creates an executable file named program (or program.exe on


Windows). You can then run it by typing:

./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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy