Algorithms and Programming Techniques IGCSE
Algorithms and Programming Techniques IGCSE
Example:
Start
|
Read Input
|
If number > 10
/ \
Yes No
| |
Print End
7.2.2 Pseudocode
Pseudocode is a method of designing algorithms using a structured, yet simple,
language that resembles programming syntax. It does not require strict rules but
should be understandable by humans.
Example of Pseudocode:
Algorithm to find the largest number
Start
Read number1, number2, number3
If number1 > number2 AND number1 > number3
Print "number1 is largest"
Else if number2 > number1 AND number2 > number3
Print "number2 is largest"
Else
Print "number3 is largest"
End
Example:
If age >= 18:
Print "Adult"
Else:
Print "Minor"
7.5.5 Loops
Loops allow a block of code to be executed repeatedly.
For Loop: Used to iterate over a range or a collection.
o Example: for i in range(5): print(i)
7.7 Recursion
Recursion is when a function calls itself in order to solve a problem. It is useful
for problems that can be broken down into smaller, similar problems.
Base Case: The condition that stops the recursion.
Recursive Case: The condition that leads to another function call.
Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)