Fortran and Other Imperative Languages
Imperative Programming
When you write a program as a list of instructions that the computer carries out one after another, storing and updating values as it goes, you are programming imperatively. It is the oldest programming style and still the most common. FORTRAN, C and Pascal work this way, and so does the statement-by-statement core of Python and Java. This lesson explains what makes code imperative and how to read it by tracing how its values change.
Commands that change state
Imperative programming describes a computation as a sequence of commands, each of which changes what the program has stored. The values held by all of a program's variables at one moment form its program state. Running an imperative program moves it from an initial state through a chain of intermediate states, one command at a time:
Here s0 is the state before anything runs, ck is command number k, and sn is the final state, from which the program reads its output.
The central command is assignment, which evaluates an expression and stores the result in a variable, replacing the old value. In Fortran, total = total + i reads the current value of total, adds i, and overwrites total with the sum. Any change a command makes beyond producing a value, such as overwriting a variable or printing output, is a side effect. Imperative code does almost all of its work through side effects.
Each command acts on the state the previous one left behind, so the order of the statements is part of the program's meaning. Swapping two lines usually changes the result.
Four building blocks
Every imperative program is assembled from a few kinds of construct.
Construct | Fortran form | Effect on state |
|---|---|---|
Assignment | x = expr | Replaces the value of one variable |
Sequence | One statement per line | Applies commands in the written order |
Selection | if (cond) then ... else ... end if | Chooses which commands run |
Iteration | do i = 1, n ... end do or do while (cond) | Repeats commands, changing state on each pass |
The program below adds the whole numbers from 1 to 5. The line implicit none makes Fortran insist that every variable is declared, which catches misspelled names.
program sum_to_five
implicit none
integer :: i, total
total = 0
do i = 1, 5
total = total + i
end do
print *, total
end program sum_to_fiveA trace table records the state after each pass of the loop. It is the standard way to predict what imperative code will do.
After pass | i | total |
|---|---|---|
Before loop | unset | 0 |
1 | 1 | 1 |
2 | 2 | 3 |
3 | 3 | 6 |
4 | 4 | 10 |
5 | 5 | 15 |
The program prints 15. The line total = 0 has to come before the loop. Placed inside the loop, it would reset the running total on every pass, and the program would print 5.
Fortran and the imperative family
Fortran was developed at IBM by a team led by John Backus, and its first compiler was delivered in 1957. It let scientists write formulas such as y = a*x**2 + b*x + c instead of machine code. The language is still revised (the current standard is Fortran 2023) and is widely used for weather, physics and engineering simulations. In that work, its array operations and mature compilers produce fast numerical code. COBOL, designed in 1959, brought the same style to business data processing, and C, from 1972, became the language of operating systems. Later languages added procedures, objects and other ways to organize code. Inside each procedure or method, though, the code is still a sequence of assignments, branches and loops.
What to remember
- An imperative program is an ordered list of commands that change program state.
- Assignment overwrites a variable, so = means store, not equality.
- Sequence, selection and iteration control which assignments run and when.
- Statement order matters. A trace table shows the state after each change and predicts the output.
- Fortran (1957) was the first widely used high-level imperative language and remains central to scientific computing.
Example: Tracing a halve-or-triple loop
A student writes this Fortran program to count how many steps a number takes to reach 1. On each step, an even number is halved and an odd number is tripled and increased by one. The intrinsic function mod(x, 2) gives the remainder when x is divided by 2. The operator /= means not equal.
program count_steps
implicit none
integer :: x, steps
x = 6
steps = 0
do while (x /= 1)
if (mod(x, 2) == 0) then
x = x / 2
else
x = 3*x + 1
end if
steps = steps + 1
end do
print *, steps
end program count_stepsa) What value does the program print?
The starting state is x = 6, steps = 0. The condition x /= 1 is checked before every pass. Each pass applies exactly one branch of the if and then adds 1 to steps.
- Pass 1: 6 is even, so x becomes ________.
- Pass 2: 3 is odd, so x becomes 3 × 3 + 1 = ________.
- Pass 3: 10 is even, so x becomes 5.
- Pass 4: 5 is odd, so x becomes 3 × 5 + 1 = ________.
- Passes 5 to 8: halving gives 8, then 4, then 2, then 1.
After pass 8, x is 1, so x /= 1 is false and the loop ends. Each of the 8 passes added 1 to steps, so the program prints 8.
b) Suppose the line steps = steps + 1 is moved from inside the loop to just after end do. What does the program print now?
The loop still takes x through the same eight values, but nothing inside the loop changes steps any more. When the loop ends, steps still holds ________, its initial value. The single increment after end do then runs once, so the program prints 1. The commands are identical to the original program. Only their position changed, and that alone changed the output.
c) Go back to the original program and change the first assignment to x = 12. What does the program print?
Pass 1: 12 is even, so x becomes ________ and steps becomes 1. The state is now x = 6 with one step already counted. Part a showed that starting from x = 6 the loop needs ________ more passes to reach 1, and each of those passes adds 1 to steps. The total is 1 + 8 = 9, so the program prints 9.
Match each term used to describe imperative programs with the description that fits it. Each description is used exactly once, and each describes how a program written in a language such as Fortran or C stores and changes values.
Any change a command makes beyond producing a value, such as overwriting a variable or printing output
Running commands one after another in the order they are written
Repeating a block of commands for a counted range or while a condition holds
Evaluating an expression and storing the result in a variable, replacing its old value
The values held by all of a program's variables at one moment during execution
Choosing which commands run according to whether a condition is true
Program state
Sequence
Selection
Assignment
Side effect
Iteration
Trace the Fortran fragment below, in which a, b and i are integers. Fill in the values of a and b at the end of each pass of the loop, entering whole numbers.
a = 2
b = 5
do i = 1, 3
a = a + b
b = a - b
end do| End of pass | a | b |
| i = 1 | ||
| i = 2 | ||
| i = 3 |