Chapter 8 Notes: Programming Concepts
Five Basic Programming Constructs
DATA USE: use of variables, constants and arrays to store data while a program runs.
SEQUENCE: the order of steps executed to complete a task.
SELECTION: choosing a path or branch in a program based on conditions.
ITERATION: repetition of a sequence of steps until a condition is met.
OPERATOR USE: arithmetic for calculations; logical and Boolean operators for making decisions.
Variables and Constants
A VARIABLE is a data item whose value can change during program execution.
Examples: name, position, age
Example representations: name = "Mr Bulmer", position = "Teacher", age = 35
A CONSTANT provides a value that does not change during program execution.
Example: pi (π) as a constant used in calculations.
Example representation: pi = 3.142 (will not change)
Rationale: variables hold data the program manipulates; constants hold fixed values used in calculations or decisions.
Declaring Variables and Constants (Pseudocode conventions)
Good practice is to declare variables and constants used in a program.
Declarations can be explicit (specifying data type) or implicit (type inferred from value).
Declarations can occur at the start of a program or just before the data is used.
Pseudocode example (explicit declarations for variables; implicit for constants):
DECLARE FirstVar : INTEGER
DECLARE SecondVar : INTEGER
CONSTANT FirstConst = 500
CONSTANT SecondConst <- 100
Declaring Variables and Constants (Python conventions)
In Python, there are no separate declarations and no distinction between variables and constants (by language syntax).
You simply assign values to names as needed.
Examples:
FirstVar = 50
SecondVar = 100
FIRSTCONST = 500
SECONDCONST = 1000
Alternatively, multiple assignments:
FirstVar, SecondVar = 50, 100
FirstConst, SecondConst = 500, 1000
Basic Data Types
Data types determine what kind of value a variable holds.
INTEGER: whole numbers
Example: 42
Python-like label: FirstInteger = 42
REAL (Floating-Point): decimal numbers
Example: 42.5 or 45.0
Python-like label: FirstReal = 42.5 or 45.0
CHARACTER: a single alphanumeric character
Example: 'A' or 'M'
Python-like label: MyChar = 'A' (single character)
STRING: one or more alphanumeric characters
Example: "Hello" or "Smith"
BOOLEAN: TRUE/FALSE (True/False in Python)
Example: Flag = True
Notes:
Data type determines what operations are valid and how much memory is used.
Strings are sequences of characters; booleans represent truth values.
Input and Output
Programs need to receive data from users (INPUT) and display results (OUTPUT).
For each input, provide a prompt that indicates what is required from the user.
The input data type must match the variable’s required type.
All inputs are strings by default; to convert types, explicit conversions are used (e.g., Python: int(), float()).
Example (Python-like):
yourname = input("Please enter your name: ")
print("Hello", yourname)
Example run:
Please enter your name: Robert
Hello Robert
Cylinder Volume Challenge (Concept)
Goal: declare variables/constants to compute the volume of a cylinder.
Formula (real-world):
Where:
$V$ is the volume, $r$ is the radius, $h$ (or sometimes length) is the height/length of the cylinder.
Approximate value:
Pseudocode declaration (from slides):
DECLARE radius, length : REAL
CONSTANT PI = 3.142
V = PI * r^{2} * h
OUTPUT Enter values of "1" and "r"
INPUT "1" and "r"
Volume = (3.142) * r * r * 1
Print Volume
Cylinder Volume Challenge (Python example)
A clearer Python approach from the slides:
The Constant for pi (constantPi)
= 3.142
radius = float(input("Please enter the radius of the cylinder "))
length = float(input("Please enter the length of the cylinder "))
volume = radius * radius * length * constantPi
print("Volume of the cylinder is ", volume)
The same idea expressed with a more conventional naming:
constantPi = 3.142
radius = float(input("Please enter the radius of the cylinder "))
length = float(input("Please enter the length of the cylinder "))
volume = radius * radius * length * constantPi
print("Volume of the cylinder is ", volume)
Example Run (Validation)
Input:
Radius = 4
Length = 7
Calculation:
Volume = 4 × 4 × 7 × 3.142 = 351.904
Output:
Volume of the cylinder is 351.904
Additional Notes and Practical Implications
Practical implications:
Explicit declarations (where used) aid readability and error checking in strongly typed languages.
Languages like Python allow implicit typing, which increases flexibility but can lead to runtime errors if types are mishandled.
When using constants, prefer meaningful names (e.g., PI) and document their fixed nature.
For real-world code, consider using a math library constant (e.g., math.pi in Python) for accuracy and clarity.
Real-world relevance:
Understanding data types helps prevent type errors in calculations and user input handling.
Input validation is essential to handle invalid or unexpected user input gracefully.
Clear prompts and descriptive variable names improve maintainability and user experience.
Connections to foundational principles:
Data types, variable scope, and the distinction between mutable (variables) and immutable (constants) values underpin programming across languages.
The cylinder volume example illustrates how formulas connect to program logic: variables hold inputs, constants define fixed values, and expressions perform calculations.