Programming Fundamentals
1Introduction
This lecture presents a program as a procedure that receives input and produces output. It describes execution in terms of state and control flow and explains the roles of variables, conditional branches, iteration, and functions.
2Basic Terms
An
A
A
3Describing Execution as State Transitions
If is a state and is one instruction, execution can be represented as a state transition
For example, if in state , the assignment x := x + 1 produces a new state in which . Here := denotes an update and is distinct from mathematical equality.
4Branching and Iteration
if p then A else B executes when condition is true and when it is false. while p do A executes while is true and then reevaluates . Designing an iteration requires distinguishing the state updated on each iteration, the continuation condition, and the property that must hold at termination.
total := 0
for each score in scores:
total := total + score
return total
In this example, scores is the input, total is part of the updated state, and return total determines the output. Processing each of scores once performs additions. A subsequent lecture on computational complexity formalizes this analysis.
5Decomposition into Functions
A function can have a specification that states its purpose, accepted inputs, return value, and effects on state or I/O. Decomposing a large procedure into functions with clear responsibilities makes each component easier to verify independently.
However, not every function determines its return value solely from its input values. Functions may update shared state or perform I/O. The next lecture on functional programming examines this distinction in detail.
6Summary
- A program's behavior becomes clearer when its input, output, state, and control flow are distinguished.
- A conditional branch selects an operation, whereas iteration repeats an operation.
- Functions decompose a computation into units with explicit specifications.