Program Development: Bringing Your Ideas to Life!
Hey there! Welcome to the exciting world of Program Development and Computational Thinking. Ever wondered how your favourite mobile games, apps, or websites are made? It all starts here! In this chapter, we're going to learn the fundamental building blocks that programmers use to write instructions for a computer. Think of it as learning the grammar of a new language – the language of computers.
Don't worry if you've never coded before. We'll break everything down into simple, easy-to-understand steps. By the end, you'll understand how to think like a programmer and solve problems by creating your own programs. Let's get started!
1. Computational Thinking and Storing Information
Before writing code, programmers apply computational thinking: breaking down complex tasks (decomposition), spotting trends (pattern recognition), focusing on essential details (abstraction), and developing step-by-step solutions (algorithm design).
To implement algorithms, programs need containers to store and categorise information using specific data types (such as Integer for whole numbers, Real / Float for decimals, Boolean for True/False, Character for single symbols, and String for text).
Variables: The Labelled Boxes
A variable is like a box with a label on it where you can store one piece of information. You can change what's inside the box whenever you want. The label (the variable's identifier) helps you find the right box later.
Example: We can create an Integer variable called score and put the number 100 inside it.
score = 100
Later, the player might lose points, so we can change the value:
score = 90
Constants: The "Do Not Change" Boxes
A constant is just like a variable, but once you put something inside, you cannot change it. It's like a box that's been sealed shut! This is useful for values that should never change during program execution.
Example: The value of Pi is always the same. We can store it in a constant.
PI = 3.14159
Using a constant like PI makes your code easier to read and prevents you from accidentally changing an important value.
Lists (One-Dimensional Arrays): The Organised Collection
What if you need to store a whole list of items, like the scores of all students in a class? Using one variable for each would be impractical! Instead, we use a list (also known as a one-dimensional array).
Think of a list as a single container with many numbered compartments. Each compartment holds an element, and you access it using its position number, called an index.
Example: A list of high scores.
highScores = [550, 521, 498, 450]
To get the very first score, we use its index. (In standard 0-indexed arrays, the first index is 0!)
OUTPUT highScores[0] (This displays 550)
Key Takeaway
Variables store data that can change during execution.
Constants store fixed values that remain unchanged.
Arrays (Lists) store a collection of related elements under a single name using numerical indices.
2. Making Things Happen: Statements and Operators
Now that we know how to store information, let's learn how to work with it using statements and operators.
Assignment Statements: Giving a Variable its Value
An assignment statement uses the assignment operator (= or ←) to store a value inside a variable.
age = 17
The most important thing to remember is that = here means "assign the value on the right to the variable on the left". It does not mean mathematical equality.
Common Mistake Alert! Do not confuse the assignment operator (=) with the relational comparison operator for equality (often written as == or = depending on language syntax).
Input and Output Statements
Programs communicate with the user through standard input and output.
Input: An input statement receives data from an external source or user and assigns it to a variable.
Example:INPUT userName(The program waits for user input).Output: An output statement displays information on the screen or prints a message.
Example:OUTPUT "Hello, " + userName(If the user entered "Mary", this prints "Hello, Mary").
Operators: The Tools for Doing Work
Operators are special symbols that perform calculations or comparisons.
1. Arithmetic Operators: Used for mathematical computations.
+(Addition)-(Subtraction)*(Multiplication)/(Division)MOD(Modulus): Returns the remainder of integer division. For example,10 MOD 3evaluates to 1 (since \(10 \div 3 = 3\) with a remainder of 1).
2. Relational Operators: Compare two values and produce a Boolean outcome (True or False).
==or=(Equal to)!=or<>(Not equal to)>(Greater than)<(Less than)>=(Greater than or equal to)<=(Less than or equal to)
age >= 18 evaluates to True if age is 18, and False if age is 16.
3. Boolean (Logical) Operators: Combine or invert Boolean expressions.
AND: Evaluates to True only when both operands are True.OR: Evaluates to True when at least one operand is True.NOT: Inverts the Boolean value (e.g.,NOT Trueevaluates to False).
Expressions
An expression is any combination of values, variables, and operators that evaluates to a single value.
Examples: 5 + 10 (evaluates to 15), price * 1.1, or score >= 50 AND level == 3.
3. The Three Fundamental Control Structures
To solve real-world problems, we need to govern the execution flow of instructions. The three fundamental control structures are Sequence, Selection, and Iteration.
Sequence: Step-by-Step Execution
Sequence means statements execute strictly one after another in order from top to bottom.
Selection: Making Decisions (IF...THEN...ELSE)
Selection executes different code branches depending on whether a conditional expression evaluates to True or False.
Example: Checking if a student passed.
IF score >= 50 THEN
OUTPUT "Congratulations, you passed!"
ELSE
OUTPUT "Better luck next time."
END IF
Iteration: Repeating Code (Loops)
Iteration repeats a block of instructions multiple times while or until a condition is met.
Definite Loop (FOR Loop): Used when the exact number of repetitions is known in advance.
Example: Repeat 3 times.
FOR count FROM 1 TO 3
OUTPUT "Hello!"
END FORIndefinite Loop (WHILE Loop): Used when repetition continues as long as a condition remains True.
Example: Validation loop.
INPUT password
WHILE password != "secret123"
OUTPUT "Wrong password. Try again."
INPUT password
END WHILE
OUTPUT "Access granted!"Nested Loops: A loop placed inside another loop. These are commonly used when traversing two-dimensional data grids or implementing multi-pass algorithms such as sorting.
4. Putting It All Together: Finding the Average Score
Let's apply our computational thinking steps to solve a standard programming task: calculating the average value in a list of numbers.
Problem Statement: Given an array of student scores [80, 95, 72, 88, 91], calculate and output the arithmetic mean.
Step 1: Algorithm Design
- Initialise an accumulator variable
totalto 0. - Iterate through each element in the array using a loop.
- In each step, add the current element's value to
total. - Divide
totalby the total number of scores to calculateaverage. - Display
averageto the user.
Step 2: Pseudocode Implementation
// 1. Initialise variables and array
scores = [80, 95, 72, 88, 91]
total = 0
numberOfScores = 5
average = 0.0
// 2. Accumulate sum using iteration
FOR EACH score IN scores
total = total + score
END FOR
// 3. Compute mean
average = total / numberOfScores
// 4. Output result
OUTPUT "The average score is: " + average
Did you know? The term "debugging" gained widespread popularity in 1947 when computer pioneer Grace Hopper and her team found a real moth trapped in a relay of the Harvard Mark II computer. They taped the insect into their logbook with the note: "First actual case of bug being found."