Welcome to the World of Programming!
In this chapter, we are going to explore the "building blocks" of computer programs. Whether you are building a simple calculator or the next viral video game, you will use these same core concepts. Think of this as learning the grammar of a new language—once you know these rules, you can start writing your own digital stories! Don't worry if this seems tricky at first; everyone starts at the beginning, and we’ll break it down step-by-step.
1. The Big Three: Sequence, Selection, and Iteration
Every program in the world is built using three main principles. You can imagine these as the "directions" you give a robot:
Sequence
This is the simplest one! It just means that the computer follows your instructions one after another, from top to bottom. If you change the order, the program might break.
Example: When making tea, you must boil the water before you pour it!
Selection (Choice)
This allows the program to make decisions. The computer checks a condition (is it true or false?) and then decides which path to take. We usually use IF statements for this.
Analogy: IF it is raining, THEN take an umbrella. ELSE, leave it at home.
Iteration (Repetition)
Computers are great at doing things over and over again without getting bored. This is called a loop.
Example: Whisk the eggs until they are fluffy.
Quick Review:
• Sequence: The order of steps.
• Selection: Making a choice.
• Iteration: Repeating a task.
2. Storing Data: Variables and Constants
To do anything useful, a program needs to remember information. We use "containers" for this.
Variables
A variable is a named memory location that stores data that can change while the program is running.
Think of it like: Your "Score" in a game. It starts at 0 and goes up as you play.
Constants
A constant is a named memory location for data that cannot change once the program starts.
Think of it like: The value of \( \pi \) (3.14) or the maximum number of players in a match.
Why use names instead of just numbers?
We use meaningful identifier names (like player_health instead of just h) because:
1. It makes the code easier for humans to read and understand.
2. It makes it easier to update the code later without making mistakes.
Did you know? Using a constant for something like a "Tax Rate" is safer than typing the number 0.2 everywhere. If the tax rate changes, you only have to change it in one place at the top of your code!
3. Putting Data in the Box: Assignment
Assignment is the process of giving a value to a variable or constant. In most languages and pseudo-code, we use the \( = \) or \( \leftarrow \) symbol.
Example: \( Score \leftarrow 10 \) means we are putting the number 10 into the box named Score.
4. Getting Loopy: Types of Iteration
AQA asks you to know two main ways to repeat code:
Definite Iteration (Count-Controlled)
We use this when we know exactly how many times the loop should run before it starts. We usually use a FOR loop.
Example: Run 10 laps around the field. (You know you'll stop after 10).
Indefinite Iteration (Condition-Controlled)
We use this when we don't know how many times the loop will run. It keeps going until a certain condition is met.
• Condition at the START: A WHILE loop. It checks the rule first. If the rule isn't met, it might not run at all!
• Condition at the END: A REPEAT...UNTIL or DO...WHILE loop. These always run at least once because they do the task first and check the rule after.
Memory Aid:
FOR is fixed (you know the count).
WHILE is waiting (for a condition to change).
5. Nesting: Structures Inside Structures
Sometimes you need to put one structure inside another. This is called nesting.
Nested Selection
An IF statement inside another IF statement.
Example: IF it is the weekend... THEN (IF it is sunny... go outside).
Nested Iteration
A loop inside another loop.
Analogy: Think of a clock. The "hour hand" moves once, but inside that hour, the "minute hand" has to loop 60 times!
6. Subroutines (Procedures and Functions)
A subroutine is a named "mini-program" inside your main program. Instead of writing the same 20 lines of code five times, you write it once, give it a name, and "call" it whenever you need it.
• Procedures: They perform a task (like print_header).
• Functions: They perform a calculation and return a value back to you (like calculate_tax).
Common Mistakes to Avoid
• Variable vs Constant: Don't try to change a constant's value in your code! The computer will give you an error.
• Infinite Loops: If you use an indefinite loop (WHILE) but forget to change the variable inside the loop, the program will run forever and crash. Always make sure the "condition" can eventually become false!
• Bad Naming: Avoid names like variable1 or thingy. Use user_age or total_price instead.
Key Takeaways for your Exam
1. Be able to identify sequence, selection, and iteration in any code snippet.
2. Understand that variables change, while constants stay the same.
3. Remember that FOR loops are for when you know the number of repeats, and WHILE loops are for when you don't.
4. Always use meaningful names for your variables to keep your code "clean."