Welcome to Programming Techniques!
In this chapter, we are moving from "thinking" about problems to actually building solutions. Think of these techniques as your "programming toolkit." Just like a builder needs more than just a hammer to build a house, a programmer needs these specific tools to write code that is efficient, easy to read, and works every time.
Don't worry if some of these terms sound technical at first—we will break them down using everyday examples that you already know!
1. The Three Pillars: Programming Constructs
Almost every program ever written is built using three basic "building blocks." These are known as programming constructs. They control the program flow (the order in which instructions are carried out).
A. Sequence
This is the simplest construct. Sequence means that the computer carries out instructions one after the other, in the exact order they are written, from top to bottom.
Example: Making a cup of tea. 1. Boil water. 2. Put tea bag in cup. 3. Pour water. You can't pour the water before you boil it!
B. Branching (Selection)
Branching is when the program has to make a decision. It checks a condition (usually using an IF statement) and decides which path to take.
Example: IF it is raining, THEN take an umbrella, ELSE leave it at home.
C. Iteration (Looping)
Iteration means repeating a section of code. There are two main types:
1. Count-controlled: Repeats a specific number of times (e.g., a FOR loop).
2. Condition-controlled: Repeats until a condition is met (e.g., a WHILE loop).
Example: While your cup is not full, keep pouring water.
Quick Review:
• Sequence: Step-by-step order.
• Branching: Making decisions (IF).
• Iteration: Repeating tasks (Loops).
2. Variables: Global vs. Local
In programming, we use variables to store data. However, where you create a variable determines where it can be used. This is called the variable's scope.
Local Variables
A local variable is declared inside a specific part of a program (like a function or procedure). It only exists while that part of the program is running. It's like a "private" note that only people in one room can see.
Why use them? They save memory and prevent other parts of the program from accidentally changing the data.
Global Variables
A global variable is declared at the very beginning of the program and can be seen and changed by any part of the code. It's like a "public" noticeboard in the school hallway.
The Danger: While they seem easy, using too many global variables is a bad habit! It makes it very hard to find bugs because any part of the program could have changed the value of that variable without you realizing it.
Memory Aid: Local is private, Global is public.
3. Modularity: Functions and Procedures
As programs get bigger, they become messy. Modularity is the process of breaking a large program into smaller, manageable chunks called sub-routines.
Procedures vs. Functions
• Procedures: A block of code that performs a specific task but does not send a value back to the main program.
• Functions: A block of code that performs a task and always returns a value back to the part of the program that called it.
Analogy: A Procedure is like a chef who cooks the meal and puts it on the counter. A Function is like a calculator; you give it numbers, and it "hands back" the answer.
Key Takeaway: Modularity makes code easier to test, easier to read, and allows you to reuse code without typing it out again!
4. Parameter Passing: Passing the Torch
When we use a sub-routine, we often need to send it information. This information is passed using parameters. There are two ways to do this, and this is a very popular exam topic!
A. Passing by Value
The program makes a copy of the data and sends that copy to the sub-routine. The original data stays safe and unchanged.
Example: You give a friend a photocopy of your homework. If they scribble on it, your original copy is still perfect.
B. Passing by Reference
The program sends the memory address of the original data. This means the sub-routine is looking at the actual data, not a copy.
Example: You give a friend the key to your locker. If they go inside and change something, your original stuff has been changed.
Common Mistake: Students often forget that By Value protects the original data, while By Reference allows the sub-routine to change the original variable.
5. Using an IDE (Integrated Development Environment)
An IDE is the software you use to write your code (like PyCharm, IDLE, or Visual Studio). It isn't just a text editor; it's a powerful tool designed to help you code faster and find errors.
Key Features of an IDE:
• Code Editor: Features like "Syntax Highlighting" (coloring keywords) make code easier to read.
• Error Diagnostics / Debugging: It points out exactly where you made a mistake and allows you to run code one line at a time to find bugs.
• Run-time Environment: Allows you to test your program instantly with the click of a button.
• Auto-complete: Suggests variable names or commands as you type (like predictive text on your phone!).
Did you know? Before IDEs, programmers had to write code in basic text files and run separate "compiler" programs manually. If there was an error, they had to guess which line caused it!
Final Summary Checklist
Before you finish this chapter, make sure you can:
• Identify Sequence, Branching, and Iteration in code.
• Explain why Local variables are usually better than Global ones.
• Define the difference between a Procedure and a Function (Does it return a value?).
• Explain the difference between passing a parameter By Value and By Reference.
• Name at least three features of an IDE that help a programmer.
Don't worry if passing by reference feels tricky—it's one of the most advanced concepts in this section! Just remember: Value = Copy, Reference = Original.