Welcome to Programming Techniques!

In this chapter, we are going to dive into the "toolbox" of every software developer. Whether you are building the next viral app or a simple calculator, you need to know how to structure your code so it is efficient, easy to read, and works correctly. We will look at the logic that controls programs, how to manage data, and the modern ways we organize code into "objects."

Don't worry if some of these terms sound like a foreign language right now—by the end of these notes, you'll be speaking "computer" fluently!

1. The Three Building Blocks: Programming Constructs

Every single program in the world, no matter how complex, is built using just three basic structures. Think of these like the "Lego bricks" of coding.

A. Sequence

Sequence is the simplest construct. It means the computer carries out instructions one after another, in the exact order they are written.
Example: Making a cup of tea. 1. Boil water, 2. Add tea bag, 3. Pour water. You wouldn't pour the water before boiling it!

B. Branching (Selection)

Branching (also known as selection) is where the program makes a decision. It uses a condition (like an IF statement) to decide which path to take.
Example: IF it is raining, take an umbrella. ELSE, leave the umbrella at home.

C. Iteration

Iteration is just a fancy word for looping. It means repeating a section of code until a certain condition is met.
- Count-controlled loops: Repeat a set number of times (e.g., FOR i = 1 to 10).
- Condition-controlled loops: Repeat until something changes (e.g., WHILE bag_not_full... or REPEAT... UNTIL).
Example: Keep walking (looping) UNTIL you reach the bus stop.

Quick Review:
- Sequence: Step-by-step.
- Branching: Decisions.
- Iteration: Repetition.

Key Takeaway: These three constructs control the "flow" of a program. If you can master these, you can write any logic!

2. Recursion: The "Mirror" Technique

Recursion is a programming technique where a function calls itself to solve a problem. It breaks a big problem down into smaller versions of the same problem.

For a recursive function to work, it MUST have two things:
1. A Base Case: A simple condition that tells the function when to stop. Without this, the program will loop forever and crash (a "Stack Overflow").
2. A General Case: The part where the function calls itself again with a slightly simpler version of the problem.

Recursion vs. Iteration

You can usually write the same logic using either recursion or a standard loop (iteration).
- Recursion: Often results in shorter, more "elegant" code. However, it uses more memory because every time the function calls itself, it takes up space on the Stack.
- Iteration: Usually faster and uses less memory, but the code can sometimes be more complex to write for certain mathematical problems.

Analogy: Imagine a set of Russian Nesting Dolls. To find the tiny prize in the middle, you open a doll (the recursive call). If there's another doll inside, you repeat the process. When you reach the smallest doll that doesn't open, that is your Base Case!

Common Mistake: Forgetting the base case! If your recursive function never stops, your computer will run out of memory very quickly.

3. Managing Data: Variables and Scope

Programs need to store data, but where that data "lives" matters.

Local Variables

A local variable is declared inside a specific subroutine (a function or procedure). It only exists while that subroutine is running and cannot be accessed from outside. This is great because it keeps your data safe and prevents other parts of the program from accidentally changing it.

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. While they sound useful, they are generally avoided because they make debugging difficult—any function could be changing the value, making it hard to track down errors.

Key Takeaway: Use Local Variables whenever possible to keep your code "neat" and "modular."

4. Modularity, Functions, and Procedures

Modularity is the practice of breaking a giant program into smaller, manageable chunks called subroutines. This makes the code easier to test, write, and reuse.

Functions vs. Procedures

- Procedures: They perform a task (e.g., ClearScreen).
- Functions: They perform a task AND return a value back to the main program (e.g., CalculateSquareRoot).

Passing Parameters

When you call a subroutine, you often pass it data called parameters. There are two ways to do this:
1. Passing by Value: The computer makes a copy of the data. The original data stays safe and unchanged.
2. Passing by Reference: The computer passes the address (memory location) of the original data. If the subroutine changes the data, the original version is changed too!

Analogy: Passing by Value is like giving someone a photo of your house—they can draw on it, but your actual house stays the same. Passing by Reference is like giving them your house keys—if they paint the walls, your actual house changes!

5. Using an IDE (Integrated Development Environment)

An IDE is the software you use to write code (like PyCharm, Visual Studio, or IDLE). It provides tools to make your life easier:

- Editor: Where you type your code (usually with color-coding/syntax highlighting).
- Error Reporting: Tells you exactly which line your code crashed on.
- Debugging Tools: Allows you to set Breakpoints (pausing the program) and Step through the code line-by-line to see what is happening to your variables.

6. Object-Oriented Programming (OOP)

Object-Oriented Techniques are a modern way of organizing code by grouping data and behaviors together. Instead of just writing a list of instructions, you create "Objects."

Key Terms in OOP:

- Class: The blueprint. (e.g., a blueprint for a "Car").
- Object: An actual instance of that blueprint. (e.g., "My Red Ferrari").
- Attributes: The data about the object (e.g., color, top speed, fuel level).
- Methods: The actions the object can take (e.g., accelerate, brake).
- Encapsulation: Hiding the "inner workings" of an object and only showing what is necessary. (Like driving a car without needing to know exactly how the engine pistons work).
- Inheritance: Creating a new class based on an existing one. (e.g., a "Truck" class can inherit features from the "Car" class but add "Towing Capacity").
- Polymorphism: Allowing different objects to be treated as the same type but behave differently. (e.g., a "Dog" and a "Cat" both have a "MakeSound" method, but one says "Woof" and the other says "Meow").

Did you know? Most modern software, from video games to banking systems, is built using OOP because it makes it so much easier to manage massive amounts of code!

Quick Review Box:
- Class = Blueprint.
- Object = The real thing.
- Inheritance = Passing down traits.
- Encapsulation = Keeping things private.

Key Takeaway: OOP helps us model the real world in our code, making it more intuitive and organized.