Imperative Programming, programs describe how computation changes the program state step
by step. It focuses on commands (assignments, loops, conditionals) that modify variables and
control execution flow.
Formally, in the theory of programming languages, imperative languages are modeled by:
State: A mapping from variable names to values.
Operational Semantics: Rules describing how each statement changes the state.
Example:
x = 5;
y = x + 1;
Each statement mutates the state that’s the essence of imperative semantics.
i. Variables and Assignment
➤ Theoretical (Operational Semantics) View:
A variable is a symbolic name for a memory location that holds a value.
The assignment statement changes the program’s state.
Formally:
Bash
⟨x := e, σ⟩ → σ[x ↦ eval(e, σ)]
This means:
If the current state is σ, then executing x := e results in a new state where x is updated to the
evaluated result of e.
Example:
x=5
x=x+2
After execution, the state changes from {x: 5} to {x: 7}.
➤ Pragmatic View:
Variables store temporary data.
Assignment provides control and efficiency — but excessive use can make reasoning
difficult (side effects).
i. Control Structures
➤ Theoretical View:
Control structures (like if, while, for) define the flow of execution.
Example — Operational semantics for if:
Bash
⟨if b then S1 else S2, σ⟩ → ⟨S1, σ⟩ if eval(b, σ) = true
⟨if b then S1 else S2, σ⟩ → ⟨S2, σ⟩ if eval(b, σ) = false
For while:
Arduino
While b do S ≡ if b then {S; while b do S} else skip
Pragmatic View:
Control structures allow expressing logic clearly:
if → decision making
while/for → repetition
break/continue → fine control
They make imperative programs expressive and readable.
iii. Exceptions
➤ Theoretical View:
Exceptions model abrupt changes in control flow.
Operational semantics extend normal execution with an exception state.
Example rule:
⟨throw E, σ⟩ → exception(E)
⟨try S1 catch S2, σ⟩ → ⟨S2, σ⟩ if S1 raises exception
Pragmatic View:
Exceptions improve error handling by separating normal and exceptional paths.
Prevents code clutter with checks after every operation.
Example:
try:
result = x / y
except ZeroDivisionError:
result = 0
v. Aliasing
➤ Theoretical View:
Aliasing occurs when two or more variables reference the same memory location.
Changes through one alias affect all others — complicating semantics and reasoning.
Formally:
If x and y are aliases, then:
σ(x) = σ(y)
and any update to x changes y too.
Example:
a = [1,2]
b=a
b[0] = 10
# a now becomes [10,2]
Pragmatic View:
Makes code less predictable.
Can cause side effects, bugs, or security issues.
Must be carefully controlled (e.g., using copies or immutability).
v. Sequential Expressions
➤ Theoretical View:
Sequential execution means one statement executes after another.
This defines the core semantic rule of imperative computation.
Rule:
⟨S1; S2, σ⟩ → ⟨S2, σ'⟩
if executing S1 on state σ produces state σ'.
Example:
x = 5;
y = x + 2;
z = y * 3;
Each statement updates the state, and the next uses the updated one.
➤ Pragmatic View:
Sequence ensures deterministic, step-by-step behavior.
Makes programs easy to reason about in terms of order of operations.
vi. Structured Programming
➤ Theoretical View:
Structured programming constrains control flow to sequence, selection (if/else), and iteration
(while/for) — avoiding unstructured jumps (goto).
Semantically, it ensures:
Clear, hierarchical program structure.
Predictable control transitions.
➤ Pragmatic View:
Increases readability and reliability.
Reduces logical errors.
Foundation of modern imperative languages like C, Java, and Python.
Example (structured):
for i in range(5):
if i % 2 == 0:
print(i)
vs. (unstructured, using goto in C):
start:
if (i >= 5) goto end;
if (i % 2 == 0) printf("%d", i);
i++;
goto start;
end:
v ii. Expression-Oriented Languages
➤ Theoretical View:
In some imperative languages, everything is an expression that returns a value (even conditionals
and loops).
Operational semantics treats statements as expressions with defined values.
Example:
python
x = (a if a > b else b)
The conditional produces a value — not just control flow.
Formally:
Bash
eval(if b then e1 else e2, σ) = eval(e1, σ) if eval(b, σ)=true
Pragmatic View:
Encourages compact, functional-style coding inside imperative languages.
Makes constructs composable — expressions can nest within others.
Languages like Python, Scala, and Rust blend imperative and expression-oriented paradigms.
Concept Theoretical (Operational Pragmatic (Practical
Semantics) Importance)
Variables & Assignment State changes via value Core mechanism for storing
updates data
Control Structures Define branching and looping Enable logical decision &
repetition
Exceptions Model abnormal control flow Handle errors cleanly
Aliasing Multiple names for one Affects correctness and
location predictability
Sequential Expressions Ordered state transitions Step-by-step deterministic
behavior
Structured Programming Hierarchical control flow Improves clarity &
rules maintainability
Expression-oriented Everything evaluates to a Allows compact and
Languages value composable syntax