Program Execution Control Mechanisms

Introduction

There are three primary ways to control program execution:

  1. Expression Evaluation: Control is based on operator precedence and associativity.
  2. Instruction Execution: Control is achieved by selecting the next instruction. Selection, iteration, and logical expressions or counters govern this process.
  3. Subprogram Control: A call and return system manages the execution of subprograms.

Execution Control at the Expression Level

Understanding expression evaluation requires understanding the order of operator and operand evaluation. Operator evaluation order is governed by precedence and associativity rules.

Evaluation of Arithmetic Expressions

Most programming languages inherit arithmetic expression characteristics from mathematical conventions. Arithmetic expressions consist of operators, operands, parentheses, and function calls. An operator’s arity refers to the number of operands it requires (unary, binary, or ternary). Most imperative languages use binary infix operators (placed between operands). However, prefix (before operands) or postfix (after operands) notations exist. Postfix notation is also known as Reverse Polish Notation.

Example: The expression (8 * ((5 + 4) – 10)) / 20 can be represented as:

  • Infix: (8 * ((5 + 4) – 10)) / 20
  • Prefix: / * 8 – + 5 4 10 20
  • Postfix: 8 5 4 + 10 – * 20 /

Operator Precedence

An expression’s value depends on the order of operator evaluation. For example, if a = 3, b = 4, and c = 5, the expression a + b * c evaluates to 23 because multiplication has precedence over addition.

Precedence rules vary across languages. Consult language documentation for specific rules.

Associativity of Operators

Associativity determines evaluation order for operators with the same precedence. For example, in a – b + c – d, subtraction and addition have the same precedence. Left associativity means a – b is evaluated first, then the result is added to c, and finally, d is subtracted.

Exponentiation is often right-associative. Parentheses override precedence and associativity rules.

Order of Operand Evaluation

If an operation has multiple operands and side effects occur during operand evaluation, the order of evaluation can affect the final result.

Type Conversion

Two types of conversion exist: narrowing (loss of precision) and widening (no loss of precision). Conversions can be explicit (user-specified) or implicit (compiler-performed).

Mixed-Mode Operations

Mixed-mode operations involve operands of different types. Coercion converts one operand to the other’s type. For example, in 4.0 + 3, the integer 3 might be converted to a real number before addition.

Explicit Type Conversion

Explicit conversions (casts) allow specifying the desired type. C-style languages use parentheses (e.g., (int)count). Ada uses function-call syntax (e.g., float(sum)).

Short-Circuit Evaluation

Short-circuit evaluation avoids unnecessary operand evaluation. For example, in (19 * x) * (y / 123 – 32), if x = 0, the second operand is not evaluated. This is more common with boolean expressions.

Control Flow at the Instruction Level

Instructions execute sequentially unless altered by control flow statements.

Explicit Deviations (GOTO)

The goto statement transfers control to a labeled instruction. Fortran’s arithmetic IF provides conditional goto functionality.

Selection Commands (IF, CASE)

If statements provide conditional execution. Case (or switch) statements select execution based on an expression’s value.

Iterative Statements (Loops)

Iterative statements (loops) repeat a block of code. Loop execution can be controlled by boolean expressions (while, repeat-until) or counters (for).

LOOP and EXIT Commands

Some languages offer loop and exit commands for more flexible loop control.

Control Flow at the Subprogram Level

Subprograms abstract processes and can be called from various program points.

Rationale

Subprograms have a single entry point and return control to the caller after execution.

Parameters

Subprograms communicate through parameters. Formal parameters are placeholders in the subprogram definition. Actual parameters are values passed during the call.

Parameter Passing Mechanisms

Various mechanisms exist for passing parameters, including by reference, by value, by result, by value-result, and by name.

Overloaded Subprograms

Overloaded subprograms have multiple implementations with the same name but different parameter types.

Generic Subprograms

Generic (polymorphic) subprograms can operate on various data types with the same implementation.

Coroutines

Coroutines offer a symmetric control model with multiple entry points and the ability to maintain state between activations.

Summary

This document explored program control flow at the expression, instruction, and subprogram levels. Understanding these mechanisms is crucial for writing efficient and maintainable programs.