Welcome to the World of Code!
In this chapter, we are going to explore Programming Concepts. Think of these as the "building blocks" of every computer program ever made. Whether it's a simple calculator or a massive video game, they all use the same basic principles to work. Don't worry if it seems a bit like learning a new language at first—by the end of this, you'll see that coding is really just a way of giving very clear instructions!
1. The Three Golden Rules of Programming
Every program you write is built using three main ideas. These are the "combining principles" that make code flow:
• Sequence: This is the simplest one. It just means the computer follows your instructions one by one, from top to bottom. Just like following a recipe!
• Selection: This is how the computer makes decisions. It uses "if" statements to decide which path to take. (Example: IF it is raining, take an umbrella.)
• Iteration: This is a fancy word for "looping." It's when the computer repeats a set of instructions over and over again.
Quick Review: The Three Pillars
Sequence = Order
Selection = Decisions
Iteration = Repetition
2. Storing Data: Variables and Constants
To do anything useful, a program needs to remember information. We do this using Identifiers (names we give to pieces of data).
Variable Declaration
A Variable is like a storage box with a label. You can change what's inside the box while the program is running. For example, in a game, your score is a variable because it changes every time you get a point.
Constant Declaration
A Constant is a value that cannot change once the program starts.
Why use them? They make your code safer! If you have a value like \( \pi = 3.14 \), you don't want a mistake in your code accidentally changing it to 5.0. It also makes the code easier to read.
Assignment
Assignment is the act of putting a value into a variable. In AQA pseudo-code, we use an arrow: Score ← 10. This means "put the number 10 into the variable named Score."
Did you know? Using meaningful identifier names (like player_lives instead of just x) is vital. It helps other programmers (and your future self!) understand what the code is supposed to do.
Key Takeaway: Use variables for things that change and constants for things that stay the same. Always give them clear, descriptive names!
3. Selection (Making Decisions)
Computers aren't "smart" on their own; they just follow the logic we give them using Selection.
The most common form is the IF statement. We can also "nest" them. Nested Selection is when you put one IF statement inside another one. It’s like a decision tree.
Example:
IF GameWon = True THEN
IF Score > HighScore THEN
... update high score ...
ENDIF
ENDIF
Don't worry if this seems tricky! Just think of it as "Layer 1" (Did I win?) and "Layer 2" (Was my score the best ever?). You only check Layer 2 if Layer 1 is true.
4. Iteration (Loops)
Iteration is when we repeat code. There are two main types you need to know:
Definite Iteration (Count-Controlled)
We use this when we know exactly how many times we want to repeat something. We usually use a FOR loop for this.
Example: FOR i ← 1 TO 5 (This will run exactly 5 times).
Indefinite Iteration (Condition-Controlled)
We use this when we don't know how many times we'll need to repeat. We keep going WHILE or UNTIL a condition is met.
• WHILE loop: Checks the condition at the start. If the condition isn't met, the code inside might never run!
• REPEAT UNTIL loop: Checks the condition at the end. This means the code inside will always run at least once.
Memory Aid:
FOR = We know the count.
WHILE/REPEAT = We're waiting for something to happen.
Key Takeaway: Use Nested Iteration (a loop inside a loop) when you need to repeat a whole process multiple times, like drawing a grid of squares row by row.
5. Subroutines (Procedures and Functions)
A Subroutine is a named block of code that performs a specific task. Instead of writing the same 20 lines of code over and over, you write it once as a subroutine and "call" it by name whenever you need it.
The Two Types of Subroutines:
1. Procedures: They carry out a task but don't send a value back.
2. Functions: They carry out a task and RETURN a value to the main program.
Parameters and Local Variables
• Parameters: These are the "inputs" you pass into a subroutine so it has the data it needs to work.
• Local Variables: These are variables declared inside a subroutine. They only exist while that subroutine is running. This is "good practice" because it prevents different parts of your program from accidentally messing with each other's data!
Analogy: Imagine a "MakeToast" subroutine. The Parameter is the bread you put in. The Function returns the toasted bread to you. Any crumbs left inside the toaster are like Local Variables—they stay inside the toaster and don't affect the rest of your kitchen!
6. Structured Programming
This is a professional approach to coding. It means breaking a big, scary problem down into smaller, manageable subroutines (this is called Decomposition).
Advantages of the Structured Approach:
• It's easier to write and test small pieces of code.
• You can reuse subroutines in different parts of the program.
• The code is much easier for humans to read and fix (debug).
Quick Review Box:
• Subroutine: A mini-program inside your main program.
• Function: Sends a value back (Returns).
• Procedure: Just does the work.
• Local Variable: Stays inside the subroutine.
You're doing great! Programming is all about logic. If you can explain to a friend how to make a cup of tea step-by-step, you already understand the basics of programming concepts!