Welcome to Additional Programming Techniques!

In the previous chapters, you learned the basics: how to make a computer follow instructions, use loops, and make decisions. Now, we are going to level up! This chapter is all about the "power tools" of programming. These techniques will help you write programs that can handle large amounts of data, remember information in files, and stay organized even as they get bigger and more complex.

Don't worry if some of this seems tricky at first! Programming is a skill that takes practice. Think of these techniques like learning to drive—at first, there are a lot of buttons and mirrors to watch, but soon it becomes second nature.

1. String Manipulation

In computer science, a string is just a sequence of characters (letters, numbers, or symbols). String manipulation is a fancy way of saying "changing or joined pieces of text."

Concatenation

This is when you join two or more strings together to create a new one. Think of it like snapping Lego bricks together.

Example: "Computer" + "Science" becomes "ComputerScience".

Slicing

Slicing is when you "cut out" a specific part of a string. You tell the computer which position to start at and where to end.

Example: If you take a slice of the string "Chocolate", you might just want "Choc".

Quick Review: Imagine a string is like a bead necklace. Concatenation is adding more beads to the end. Slicing is snipping a section of beads out of the middle.

Key Takeaway: Strings aren't just static text; we can glue them together or chop them up to get exactly what we need.

2. File Handling

Up until now, your programs probably "forgot" everything as soon as you turned them off. File handling allows your program to save data to a permanent file (like a .txt file) so it can be loaded back later.

There are four main steps you must follow, always in this order:

  1. Open: You must open the file before you can do anything with it.
  2. Read: Bringing data from the file into your program.
  3. Write: Sending data from your program into the file.
  4. Close: This is the most important step! If you don't close the file, your data might not save properly or the file could get corrupted.

Common Mistake: Forgetting to Close the file! It’s like leaving the fridge door open; eventually, things go wrong. Always make sure your program shuts the file when it's done.

Key Takeaway: Files allow for permanent storage. Use Open, Read/Write, and Close to manage them.

3. Arrays and Records

When we have lots of data, we don't want to create 100 different variables. Instead, we use Arrays.

One-Dimensional (1D) Arrays

Think of a 1D array like a single row of lockers. Each locker has a number (an index), and you can store one piece of data inside each one. In most languages, we start counting at 0!

Two-Dimensional (2D) Arrays

A 2D array is like a grid or a spreadsheet. It has rows and columns. To find a specific piece of data, you need two coordinates: the row number and the column number.

Analogy: A 1D array is a street of houses. A 2D array is a giant apartment block with multiple floors and multiple rooms on each floor.

Records

While an array usually stores the same type of data (like a list of all integers), a Record is used to store a collection of related data that might be different types. For example, a student record might store: Name (String), Age (Integer), and PassedExam (Boolean).

Did you know? 2D arrays are often used to represent game boards, like Noughts and Crosses or Chess!

Key Takeaway: Arrays handle lists and grids of data. Records group different types of information about one single thing.

4. SQL (Structured Query Language)

Sometimes data is stored in massive databases. SQL is the language we use to search through those databases to find exactly what we want. You only need to know three main commands for the J277 exam:

  • SELECT: Which fields (columns) do you want to see? (e.g., Name, Age)
  • FROM: Which table is the data in?
  • WHERE: What is the search criteria? (e.g., WHERE Age > 15)

Example: SELECT Name FROM Students WHERE Grade == 'A'
This would give you a list of names for every student who got an A.

Key Takeaway: SQL is like a filter for databases. It helps you find a "needle in a haystack."

5. Subprograms: Procedures and Functions

As programs get bigger, they get messy. Subprograms allow us to break a large program into smaller, manageable "chunks" of code. This is called structured programming.

Procedures

A procedure is a small block of code that performs a specific task. You "call" it, it does its job, and then the program moves on.

Functions

A function is very similar to a procedure, but with one big difference: it returns a value back to the main program.

Memory Aid: A Function Finds a result and gives it back to you. A Procedure just performs the process.

Quick Review:
- Procedure: "Go to the kitchen and turn on the light." (Task done).
- Function: "Go to the kitchen and tell me how many apples are in the bowl." (The number is returned to you).

Key Takeaway: Subprograms make code easier to read, test, and reuse. Functions give a value back; procedures do not.

6. Scope: Local and Global Variables

Variables have a "lifespan" or a "zone" where they exist. This is called Scope.

  • Global Variables: These are declared at the very top of the program. Every part of the program (including all subprograms) can see and use them.
  • Local Variables: These are declared inside a specific subprogram. They only exist while that subprogram is running. Once the subprogram finishes, the local variable is deleted.

Why use Local Variables? They are much safer! They prevent a subprogram from accidentally changing a variable that is being used somewhere else in the program.

Analogy: A Global variable is like a school-wide announcement that everyone hears. A Local variable is like a whisper between two students in one classroom—the rest of the school doesn't know it exists.

Key Takeaway: Local variables keep your code "tidy" and prevent bugs. Use them whenever possible!

7. Random Number Generation

Computers are usually very predictable, but sometimes we want them to be random—like when rolling a dice in a game or shuffling a playlist. Most languages have a built-in library to generate random numbers. You simply provide a range, such as \( (1, 6) \), and the computer picks one for you.

Key Takeaway: Random numbers add "chance" to our programs, making games and simulations possible.

Final Encouragement: You've just covered some of the most advanced concepts in the Programming Fundamentals section! Take a break, try writing a small program that uses a 1D array or a simple function, and watch it all come together. You've got this!