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:


s0→c1s1→c2s2→c3⋯→cnsns_0 \xrightarrow{c_1} s_1 \xrightarrow{c_2} s_2 \xrightarrow{c_3} \cdots \xrightarrow{c_n} s_n


Here s0s_0 is the state before anything runs, ckc_k is command number kk, and sns_n 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.


fortran
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_five


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


fortran
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_steps


a) 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.

A Fortran program computes a payroll total. It sets a variable total to zero, loops over every employee adding that employee's pay to total, and finally prints total. Which description best explains why this program is an example of imperative programming?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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.

Match each term
A.

Any change a command makes beyond producing a value, such as overwriting a variable or printing output

B.

Running commands one after another in the order they are written

C.

Repeating a block of commands for a counted range or while a condition holds

D.

Evaluating an expression and storing the result in a variable, replacing its old value

E.

The values held by all of a program's variables at one moment during execution

F.

Choosing which commands run according to whether a condition is true

Program state

Sequence

Selection

Assignment

Side effect

Iteration

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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.


fortran
a = 2
b = 5
do i = 1, 3
   a = a + b
   b = a - b
end do
End of passab
i = 1
i = 2
i = 3

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors