IGCSE Computer Science
0478
Digitized Notes
8.1 Programming Concepts
UNDERSTAND AND USE THE CONCEPT OF SEQUENCE
Structured programming is a programming paradigm aimed at improving the clarity, quality, and
development time of a computer program by making extensive use of subroutines, block
structures, for loops, and if/else decision-making structures. This section covers two fundamental
concepts: sequence and selection.
A sequence in programming refers to the execution of instructions in the order they are written -
from top to bottom in a script. This linear progression makes understanding the flow of a
program straightforward.
PYTHON PSEUDOCODE
# Sequence Example // Sequence Example
print("Start Program") OUTPUT "Start Program"
age = 20 age ← 20
age = age + 1 age ← age + 1
print("Age next year:", age) OUTPUT "Age next year: ", age
print("End Program") OUTPUT "End Program"
UNDERSTAND AND USE THE CONCEPT OF SELECTION
Selection uses conditions to decide which pieces of code to execute. This decision-making
process allows programs to take different actions based on inputs or other conditions. This
examination board generally uses two different methods of selection, the 'IF' statement and the
'CASE' statement.
IF STATEMENTS
Usage:
• IF statements are used to execute a block of code if a specified condition is true. They can
also include else and elif (else if) clauses to handle multiple conditions and outcomes.
• Ideal for complex conditions that involve logical operators (AND, OR, NOT) or
comparisons that are not solely based on a single variable's value.
• Well-suited for scenarios where the conditions for decision-making are not exhaustive or
cannot be easily enumerated.
Characteristics:
• Flexibility in evaluating conditions that can be expressions involving variables, function
calls, and logical operations.
• Can become cumbersome or less readable with many elif clauses, making code harder to
follow.
• Directly supports execution of alternative blocks of code (else clause).
PYTHON PSEUDOCODE
# IF Statement Example // IF Statement Example
score = 75 score ← 75
if score >= 50: IF score >= 50 THEN
print("You passed!") OUTPUT "You passed!"
else: ELSE
print("Try again!") OUTPUT "Try again!"
ENDIF
CASE STATEMENTS
Usage
• Used to select one of many blocks of code to execute, based on the value of a single
variable.
• Ideal for when you have a variable that can take one out of a small set of possible values
and you want to execute different code for each value.
• Makes code cleaner and more readable when dealing with multiple discrete values that a
single variable can take.
Characteristics
• More readable and concise for enumerated conditions (e.g., days of the week, specific
known values).
• Cannot directly handle complex conditions involving logical operators across multiple
variables without additional nesting or checks.
• Python's match statement supports pattern matching, which is more powerful than
traditional CASE statements, allowing for matching types, sequences, and more.
PYTHON PSEUDOCODE
umber = 2 // CASE Statement Example
match number: day_number ← 3
case 1: CASE OF day_number
print "One" 1: day ← "Monday"
case 2: 2: day ← "Tuesday"
print "Two" 3: day ← "Wednesday"
case _: OTHERWISE: day ← "Unknown"
print "Something else" ENDCASE
OUTPUT "Day is: ", day
DIFFERENCES
• Complexity of Conditions | IF statements are better for complex logical conditions,
whereas CASE (or match) statements are ideal for checking one variable against a series
of values.
• Readability | CASE statements can enhance readability when dealing with a clear
enumeration of simple conditions. IF statements can become unwieldy with too many elif
clauses but offer more flexibility in condition evaluation.
• Use Cases | Use IF statements when conditions are varied and involve multiple variables
or complex logic. Use CASE statements for simpler, value-based decision paths on a
single variable.
Example - The below statement the below statement compares if the temperature is greater than
30, or less then 10, the else part of the statement would be used if it was any other value:
temperature = 30
if temperature > 30:
print("It's hot outside.")
elif temperature < 10:
print("It's cold outside.")
else:
print("It's nice outside.")
Example - The below statement the below statement compares if the status_code is exactly one
of the values stated.
status_code = 404
match status_code:
case 200:
print("Success!")
case 404:
print("Not Found.")
case _:
print("Some other error.")
The choice between IF and CASE statements largely depends on the specific needs of your code,
the complexity of the conditions you're evaluating, and the need for readability and
maintainability of your code. Python's match statement adds extra versatility to the traditional
concept of CASE statements, bridging some gaps between these two control flow mechanisms