Welcome to Programming Constructs: The Building Blocks of Code!

Hello future Computer Scientists! You’ve arrived at one of the most important chapters in programming: Constructs. Think of constructs as the essential tools or ingredients needed to write any computer program, from a simple calculator to a complex video game.

In this chapter, we will learn how to organize instructions logically. Once you master these three constructs, you can write almost any program imaginable! Don't worry if this seems tricky at first—we'll break down every concept step-by-step.


The Three Core Programming Constructs

Every structured program relies on three fundamental ways to control the flow of execution:

  1. Sequence (The order)
  2. Selection (The choices)
  3. Iteration (The repetition/loops)

1. Sequence: Doing Things in Order

Sequence is the simplest construct, and it is the foundation of all programming.

What is Sequence?

Sequence means that instructions are executed one after the other, strictly in the order they appear in the code.

Imagine following a recipe: You must chop the vegetables (Step 1) *before* you put them in the oven (Step 2). If you mix up the sequence, the recipe fails!

How Sequence Works in Code

In programming, sequence usually involves basic actions like:

  • Inputting data (e.g., asking the user for their name)
  • Processing data (e.g., calculating a total)
  • Outputting data (e.g., displaying the result)

Example (Pseudocode):

1. INPUT score1
2. INPUT score2
3. CALCULATE total = score1 + score2
4. OUTPUT total

If the computer tried to perform step 3 before step 1 and 2, it wouldn't have the necessary numbers, and the program would crash.

Quick Review: Sequence

Key Takeaway: Sequence is the default path. Every instruction is followed exactly once, top to bottom.


2. Selection: Making Decisions (IF...THEN...ELSE)

Programs wouldn't be very useful if they just did the same thing every time. Selection allows a program to choose different paths based on whether a certain condition is met.

The Role of Conditions and Boolean Logic

Selection relies on a condition, which is a statement that can only be either True or False. This True/False outcome is known as a Boolean value.

Analogy: When you wake up, you check the window. Condition: Is it raining? (True or False).

Comparison Operators

To check conditions, we use comparison operators:

  • \(=\) or \(==\) : Equal to (Used to compare if two things are the same)
  • \(\ne\) or \(<>\) or \(<>\) : Not equal to
  • \(>\) : Greater than
  • \(<\) : Less than
  • \(\ge\) : Greater than or equal to
  • \(\le\) : Less than or equal to

Memory Aid: Think of the greater than/less than symbols as a hungry alligator; it always wants to eat the bigger number!

The IF...THEN...ELSE Structure

The most common form of selection is the IF...THEN...ELSE statement.

Structure:

IF [condition is TRUE] THEN
// Do this block of code (if condition is met)
ELSE
// Do this other block of code (if condition is NOT met)
END IF

Example (Pseudocode):

INPUT age
IF age >= 18 THEN
OUTPUT "You are an adult."
ELSE
OUTPUT "You are a minor."
END IF

Nested Selection

Sometimes, we need to make a decision *inside* another decision. This is called nested selection.

Example: If it is raining, THEN check if you have an umbrella. If you have one, THEN take it. ELSE wear a raincoat.

Case/Switch Statement

For situations where you have many possible outcomes based on a single variable (e.g., choosing an option from a menu), using CASE or SWITCH is neater than many IF...ELSE IF... statements.

Example (Pseudocode):

INPUT choice
CASE OF choice
1: OUTPUT "You chose Option A"
2: OUTPUT "You chose Option B"
OTHERWISE: OUTPUT "Invalid choice"
END CASE

Quick Review: Selection

Selection uses Boolean conditions (True/False) to decide which instructions to run. Key structures are IF...THEN...ELSE and CASE.


3. Iteration: Repeating Instructions (Loops)

What if you need the computer to perform the same task hundreds or thousands of times? Writing the instruction out repeatedly would be long and error-prone. This is where iteration, or looping, comes in!

Iteration saves time, makes code shorter, and is one of the most powerful tools in programming.

Type 1: Count-Controlled Loops (FOR Loops)

A count-controlled loop is used when you know exactly how many times the loop needs to repeat before the program starts the loop.

Pseudocode Example: FOR...TO...NEXT

FOR count = 1 TO 10
OUTPUT "Hello"
NEXT count

This code will print the word "Hello" exactly 10 times. The variable count controls the loop and automatically increases (increments) until it reaches the end value (10).

Type 2: Condition-Controlled Loops

A condition-controlled loop runs indefinitely until a specific condition becomes True or False. We don't know exactly how many times it will run beforehand.

Analogy: Running laps until you hear the bell. You don’t know how long it will take, only that you stop when the condition (hearing the bell) is met.

a) WHILE Loops (Checking Condition at the Start)

The WHILE loop checks the condition before it executes the instructions inside the loop. If the condition is initially False, the code inside the loop will never run.

Pseudocode Example:

SET password = "Secret"
INPUT guess

WHILE guess != password
OUTPUT "Try again."
INPUT guess
END WHILE

OUTPUT "Access Granted."

The loop continues while the guess is not correct.

Common Mistake: Infinite Loops!

If the condition inside a WHILE loop never changes to make the condition False, the loop will run forever, crashing or freezing the program. Always make sure something *inside* the loop changes the condition!

b) REPEAT UNTIL Loops (Checking Condition at the End)

The REPEAT UNTIL loop executes the instructions at least once before checking the condition. The loop continues to repeat until the condition becomes True.

Pseudocode Example:

REPEAT
INPUT user_input
UNTIL user_input >= 0

OUTPUT "Valid positive number entered."

In this example, the program must ask for input at least once, even if the user immediately types in a positive number.

Did you know? (WHILE vs. REPEAT UNTIL)

The key difference is that a WHILE loop may execute 0 times if the condition is false immediately, whereas a REPEAT UNTIL loop will execute a minimum of 1 time.

Quick Review: Iteration

Iteration repeats instructions efficiently.
1. Count-Controlled (FOR): Knows the number of repeats.
2. Condition-Controlled (WHILE/REPEAT): Stops when a condition is met.


Final Summary of Programming Constructs

You have now mastered the three pillars of structured programming. Every program you encounter, regardless of language (Python, Java, etc.), uses these three constructs:

  • Sequence: Straight-line execution. Do A, then B, then C. (Mandatory steps.)
  • Selection: Choosing a path based on a condition (True/False). (Decision points.)
  • Iteration: Repeating a set of instructions until a condition is met or a count is reached. (Efficiency and repetition.)

Well done! Understanding these concepts makes you ready to write powerful and logical algorithms!