Welcome to the World of Programming!

Hello there! Welcome to one of the most exciting parts of your Computer Science journey. Think of programming as learning a new language that allows you to talk to computers. Computers are incredibly fast, but they aren't very "smart"—they need us to give them a specific "recipe" to follow. That recipe is what we call an algorithm, and writing it in a way the computer understands is programming.

In this chapter, we will look at the building blocks of code. Don't worry if this seems tricky at first; everyone starts at the beginning, and once you understand the logic, you'll be amazed at what you can create!

11.1 Programming Basics

Before we can write complex programs, we need to understand the basic tools in our toolkit. These are the ways we store information and communicate with the user.

Variables and Constants

Imagine you have a row of cardboard boxes. Each box can hold one piece of information.

A Variable is like a box where you can change the contents whenever you want. For example, in a game, your "Score" is a variable because it changes as you play.

A Constant is like a box that is super-glued shut once you put something inside. The value cannot change while the program is running. An example would be the value of \( \pi \) (3.14159) or the number of hours in a day (24).

Quick Tip: Use meaningful names! Instead of naming a variable x, name it userScore. It makes your code much easier to read.

Assignment, Input, and Output

To get our program working, we need to move data around:

  • Assignment: This is how we put a value into a variable. In pseudocode, we use an arrow: myAge ← 17. This means "the variable myAge now holds the value 17."
  • Input: This is how the program gets information from the user (usually from the keyboard).
    Example: INPUT userName
  • Output: This is how the program "talks" back to the user (usually on the screen).
    Example: OUTPUT "Hello!"

Arithmetic and Logical Operators

Computers love math! You can use standard symbols like +, -, * (multiply), and / (divide). You can also use logical operators like AND, OR, and NOT to compare values.

Using Library Routines (Strings)

You don't have to write everything from scratch! Library routines are pre-written pieces of code. For AS Level, you should be familiar with string manipulation. These allow you to pull pieces of text apart.
Example: Finding the LENGTH of a string or taking the LEFT-most 3 characters of a word.

Did you know? In programming, a piece of text is called a "String" because it's just a string of individual characters tied together!

Key Takeaway:

Variables store data that changes; Constants store data that doesn't. We use Assignment to store values, Input to get data, and Output to show it.


11.2 Constructs: Controlling the Flow

A program doesn't always just run in a straight line from top to bottom. Sometimes it needs to make decisions or repeat tasks. We call these control structures.

Selection (Making Decisions)

This is like a fork in the road. The program checks a condition and decides which path to take.

1. IF Statements: Use this for simple "Yes/No" or "True/False" decisions.
Example: IF score > 50 THEN OUTPUT "You Win!" ELSE OUTPUT "Try Again" ENDIF

2. CASE Statements: Use this when you have multiple specific options. It's much neater than writing ten IF statements. Imagine a vending machine: if the user presses 'A', give a snack; if they press 'B', give a drink.

Iteration (Loops)

Sometimes we need to do the same thing over and over. Instead of writing the code 100 times, we use a loop.

  • FOR Loop (Count-controlled): Use this when you know exactly how many times you want to repeat something (e.g., "Run 10 laps").
  • WHILE Loop (Pre-condition): This checks the condition before running the code. If the condition is false at the start, the code might never run at all! (e.g., "While the light is green, keep driving").
  • REPEAT...UNTIL Loop (Post-condition): This runs the code first and then checks the condition. This means the code inside will always run at least once.

Common Mistake: Forgetting to change the variable inside a WHILE loop. If you don't, the condition stays true forever, and you get an infinite loop that crashes your program!

Quick Review: Which loop should I use?

Knowing the count? Use FOR.
Need to check before starting? Use WHILE.
Need to run at least once? Use REPEAT...UNTIL.


11.3 Structured Programming

As programs get bigger, they get messy. Structured programming is the art of breaking a big, scary problem into smaller, manageable "sub-tasks." We use Procedures and Functions for this.

Procedures vs. Functions

Both are "sub-programs" that perform a specific task, but there is one big difference:

  • Procedure: It just goes and does its job. It doesn't give a value back to the main program. (Analogy: Telling someone to "Go clean your room").
  • Function: It does its job and then returns a value to the main program. (Analogy: Asking someone "What is the temperature outside?"—they go check and bring the answer back to you).

Parameters: Passing Information

When you call a procedure or function, you often need to give it some "starting info." This info is called a parameter. There are two ways to pass it:

1. By Value: The program makes a copy of the data. The original data stays safe and unchanged. It's like sending someone a photograph of your homework—they can draw on it, but your original paper stays clean.

2. By Reference: The program gives the sub-program the address of the original data. If the sub-program changes it, the original is changed too! It's like giving someone the actual key to your house—they can go in and move your furniture.

Key Terminology

  • Header: The first line of a procedure/function that defines its name and parameters.
  • Argument: The actual value you pass into the sub-program when you call it.
  • Return Value: The specific piece of data a Function sends back.
Key Takeaway:

Structured programming makes code easier to read, test, and fix. Functions return a value; Procedures do not. By Value is a copy; By Reference is the original.


Keep practicing! Programming is a skill that gets better with every line you write. If your code doesn't work the first time, don't worry—that's just part of the process called "debugging." You've got this!