Welcome to Programming Concepts!

Hello future computer scientists! This chapter is the foundation of everything you will ever program. Think of programming concepts as the grammar and vocabulary needed to speak to a computer. Once you master these basic ideas—how to store information and how to control the flow of instructions—you can build any program imaginable!

Don't worry if this seems tricky at first. We will break down every concept into simple, manageable pieces using real-world examples.


1. The Building Blocks: Data and Storage

1.1 Data Types

Computers need to know exactly what kind of information they are storing. Are you storing a whole number, text, or just a true/false statement? This is why we use Data Types. Using the correct type helps the computer manage memory efficiently.

Analogy: Think of data types as different containers in your kitchen. You wouldn't store flour in a tiny spice jar!

  • Integer (INT): Stores whole numbers (positive or negative).
    Example: 5, -100, 42
  • Real / Float: Stores numbers that can have a decimal point.
    Example: 3.14, 99.9, -0.5
  • Character / String (STR): Stores text, letters, symbols, or even numbers treated as text (like a phone number). A string is usually a sequence of characters.
    Example: "Hello World", "A", "123 Main Street"
  • Boolean (BOOL): Stores only two values: TRUE or FALSE. These are essential for making decisions.
    Example: Is the light on? (TRUE/FALSE)

Quick Review: If a number needs a decimal, use Real. If it's a piece of text (even if the text contains numbers), use String. If it's just 1 or 0 (or true/false), use Boolean.

1.2 Variables and Constants

To use data in a program, we need to store it in the computer's memory. We use special names to refer to these storage locations.

Variables

A Variable is a named memory location used to store data that can change while the program is running.

  • Analogy: A variable is a labeled box where you can keep putting different things inside.
  • Example: A variable called Score might start at 0 and increase every time the user answers correctly.
Constants

A Constant is a named memory location used to store data that must not change during the execution of the program.

  • Analogy: A constant is a labeled box that is glued shut after the first item is put inside.
  • Example: The value of Pi (\(3.14159\)) or a fixed tax rate.

Why use Constants? If you use a constant like TAX_RATE, and you need to change the rate later, you only have to change the value in one place (at the start of the program), making it much easier to maintain.

1.3 Assignment

Assignment is the process of giving a variable (or constant) its value. In programming pseudocode, we often use the arrow symbol (\(\leftarrow\)) or the equals sign (\(=\)).

Key Concept: When we assign, we are saying: "The item on the left gets the value of the item on the right."

Example of Assignment:

Username \(\leftarrow\) "AQAStudent"
Age \(=\) 16
NewScore \(\leftarrow\) OldScore \(+\) 10

Common Mistake Alert!
In programming, the line Count \(\leftarrow\) Count \(+\) 1 does not mean Count equals Count plus 1 (which is impossible in maths). It means: Take the current value of Count, add 1 to it, and put that new result back into the Count variable.

Key Takeaway for Section 1: Variables store data that changes, Constants store data that doesn't change, and Data Types tell the computer *what* kind of data is being stored.


2. Controlling the Flow: Sequence, Selection, and Iteration

A program is just a set of instructions. The three main ways we structure these instructions are called the Three Basic Programming Constructs.

2.1 Sequence

Sequence means that instructions are executed one after the other, in the exact order they are written, from top to bottom.

  • Analogy: Following a recipe step-by-step: 1. Mix ingredients. 2. Pour into pan. 3. Bake for 30 minutes.

This is the simplest type of program flow.

2.2 Selection (Making Decisions)

Selection (or branching) allows a program to choose between two or more paths based on a condition (usually a Boolean expression).

IF...THEN...ELSE...

This is the most common way to make a decision.

IF Condition IS TRUE THEN
    Do these instructions (Path A)
ELSE
    Do these instructions instead (Path B)
ENDIF

Example: IF Temperature \(<\) 0 THEN Output "Wear a coat" ELSE Output "It's warm enough".

Did you know? You can have an IF...THEN without an ELSE. If the condition is FALSE, the program just skips the instructions and moves on.

CASE / Switch Statements

A CASE (or Switch) structure is used when you need to choose from many different fixed options based on the value of a single variable.

CASE OF Grade
    A: Output "Excellent"
    B: Output "Good"
    OTHERWISE: Output "Keep trying"
ENDCASE

This is much cleaner than writing many nested IF statements when dealing with multiple fixed choices.

2.3 Iteration (Repeating Actions / Loops)

Iteration (or looping) allows a program to repeat a section of code multiple times. This prevents us from having to write the same code over and over again.

FOR Loop (Count-Controlled)

Used when you know exactly how many times you want the loop to run.

FOR Counter \(\leftarrow\) Start Value TO End Value DO
    Instructions to repeat
NEXT Counter

Example: FOR i \(\leftarrow\) 1 TO 10 DO - This loop will run exactly 10 times.

WHILE Loop (Condition-Controlled - Check First)

Used when you don't know how many times the loop will run, but you need to check the condition before running the code inside the loop.

WHILE Condition IS TRUE DO
    Instructions to repeat
ENDWHILE

Important Note: If the condition is FALSE at the very start, the code inside a WHILE loop will never run.

REPEAT UNTIL Loop (Condition-Controlled - Check Last)

Used when you don't know how many times the loop will run, but you need the code inside the loop to execute at least once.

REPEAT
    Instructions to repeat
UNTIL Condition IS TRUE

Memory Trick:
WHILE: Check condition Watch out (at the top).
REPEAT UNTIL: You are Running the code first (at the bottom).

Key Takeaway for Section 2: Sequence is step-by-step; Selection (IF/CASE) is for choosing a path; Iteration (Loops) is for repeating code.


3. Logic and Operations

Programs work by calculating and comparing data using operators.

3.1 Arithmetic Operators

These perform mathematical calculations.

  • \(+\): Addition
  • \(-\): Subtraction
  • \(*\): Multiplication
  • \(/\): Division (results in a Real/Float number)
Special Operators for Integers:
  • DIV (Integer Division): Gives the whole number result of a division, ignoring the remainder.
    Example: 10 DIV 3 \(= 3\)
  • MOD (Modulo): Gives the remainder after integer division.
    Example: 10 MOD 3 \(= 1\) (because 10 divided by 3 is 3, with 1 left over)

Why is MOD useful? It’s great for checking if a number is even or odd (if Number MOD 2 \(= 0\), it’s even) or for cycling through lists.

3.2 Comparison (Relational) Operators

These operators compare two values and always result in a Boolean answer (TRUE or FALSE). They are crucial for selection and iteration.

  • \(==\) or \(=\): Is equal to
  • \(<\): Is less than
  • \(>\): Is greater than
  • \(<=\): Is less than or equal to
  • \(>=\): Is greater than or equal to
  • \(<>\) or \(!=\): Is not equal to

Example: IF UserAge \(>=\) 18 THEN... (This condition evaluates to TRUE or FALSE).

3.3 Boolean (Logical) Operators

These operators combine Boolean results (TRUE/FALSE) to create more complex conditions.

AND

The entire condition is TRUE only if ALL parts linked by AND are TRUE.

Example: You can enter the party if (You have a ticket AND you are 18+).

OR

The entire condition is TRUE if AT LEAST ONE part linked by OR is TRUE.

Example: You pass the class if (Your exam grade is A OR your coursework is A).

NOT

Reverses the Boolean value (TRUE becomes FALSE, FALSE becomes TRUE).

Example: IF NOT (IsRaining) THEN Output "Go outside"

Key Takeaway for Section 3: Arithmetic operators calculate; Comparison operators ask questions (results in TRUE/FALSE); Boolean operators combine those questions.


4. Input and Output (I/O)

For a program to be useful, it must interact with the user or the outside world. This is done through Input and Output.

Input

Input is data that the program receives from the outside world (usually from the user via a keyboard, mouse, or sensor).

In pseudocode, we often use the keyword INPUT or READ.

Example: INPUT UserName (The program pauses and waits for the user to type something, which is then stored in the variable UserName).

Output

Output is data that the program sends to the outside world (usually displayed on the screen, printed, or sent to a file).

In pseudocode, we often use the keyword OUTPUT or PRINT.

Example: OUTPUT "Welcome, " + UserName (The program displays the greeting and the stored username on the screen).

Quick Review Box: I/O

INPUT: Data INto the computer.
OUTPUT: Data OUT of the computer.


Summary Checklist: Programming Essentials

If you understand these concepts, you have a solid programming foundation:

  1. I know the difference between Integer, Real, String, and Boolean data types.
  2. I can explain why a Variable might be used instead of a Constant.
  3. I understand that Assignment means setting the value of a variable.
  4. I can describe the difference between Sequence, Selection, and Iteration.
  5. I know that IF...THEN...ELSE and CASE are forms of Selection.
  6. I can identify when to use a FOR loop (counted) versus a WHILE/REPEAT loop (condition-controlled).
  7. I know that MOD gives the remainder and DIV gives the whole number result.
  8. I can use AND, OR, and NOT to create complex Boolean conditions.