If It's True
Sometimes you want code to run only when a certain condition is met. Swift's if statement does exactly that.
let angle = 90 if angle == 90 { // this runs only when angle equals 90 pen.turn(degrees: 90) } else { // this runs for any other value pen.turn(degrees: Double(angle)) }
Swift's comparison operators:
a == b // equal to a != b // not equal to a < b // less than a <= b // less than or equal a > b // greater than a >= b // greater than or equal
Conditions that evaluate to true or false are called Boolean expressions, named after mathematician George Boole.
| Standard | Connection |
|---|---|
| Logic & Reasoning | Boolean expressions are the foundation of mathematical logic. A condition is either true or false — there's no middle ground — mirroring two-valued logic in proofs and set theory. |
Classify an Angle
Write code that classifies an angle as acute, right, or obtuse based on its value in degrees. Print the result using print().
- Acute: less than 90°
- Right: exactly 90°
- Obtuse: greater than 90° and less than 180°
Test your code with the values 45, 90, and 120.
let angle = 45 // change this to test if angle < 90 { print("Acute") } else if angle == 90 { print("Right") } else { print("Obtuse") }
| Standard | Connection |
|---|---|
| Geometry — Angle Classification | Angles are classified by measure: acute (0°–90°), right (90°), obtuse (90°–180°), straight (180°), reflex (180°–360°). Identifying angle types is fundamental to geometry. |
Else If
When you have more than two possible outcomes, chain multiple conditions with else if.
let sides = 5 if sides == 3 { print("Triangle") } else if sides == 4 { print("Quadrilateral") } else if sides == 5 { print("Pentagon") } else { print("Other polygon") }
Swift evaluates each condition in order and executes the first block where the condition is true. Once a match is found, the remaining branches are skipped.
| Standard | Connection |
|---|---|
| Geometry — Polygon Classification | Polygons are classified by number of sides: 3 = triangle, 4 = quadrilateral, 5 = pentagon, 6 = hexagon, etc. Each classification is a mutually exclusive category. |
Triangle Classifier
Given three side lengths, classify the triangle as equilateral, isosceles, or scalene.
- Equilateral: all three sides equal
- Isosceles: exactly two sides equal
- Scalene: all three sides different
Then also determine if it could be a right triangle using the Pythagorean theorem (a² + b² = c², where c is the longest side). Test with: (3, 4, 5), (5, 5, 5), (5, 5, 8).
let a = 3, b = 4, c = 5 // change these to test // Side classification if a == b && b == c { print("Equilateral") } else if a == b || b == c || a == c { print("Isosceles") } else { print("Scalene") } // Right triangle check (sort sides first) let sides = [a, b, c].sorted() if sides[0]*sides[0] + sides[1]*sides[1] == sides[2]*sides[2] { print("Also a right triangle!") }
| Standard | Connection |
|---|---|
| Geometry — Triangles | Triangle classification by sides: equilateral (all equal), isosceles (two equal), scalene (all different). The Pythagorean theorem a² + b² = c² identifies right triangles. |
And & Or
Swift's logical operators let you combine conditions.
a && b // AND: true only if both a and b are true a || b // OR: true if at least one of a or b is true !a // NOT: inverts the truth value
Given an angle in degrees, use && and || to classify it as one of:
- Zero angle (= 0°)
- Acute (0° < angle < 90°)
- Right (= 90°)
- Obtuse (90° < angle < 180°)
- Straight (= 180°)
- Reflex (180° < angle < 360°)
- Full rotation (= 360°)
let angle = 135 if angle == 0 { print("Zero angle") } else if angle > 0 && angle < 90 { print("Acute") } else if angle == 90 { print("Right") } else if angle > 90 && angle < 180 { print("Obtuse") } else if angle == 180 { print("Straight") } else if angle > 180 && angle < 360 { print("Reflex") } else if angle == 360 { print("Full rotation") }
| Standard | Connection |
|---|---|
| Geometry — Angles | Angles are classified across the full 0°–360° range. Compound inequalities (e.g. 90° < angle < 180°) represent intervals on the number line — a key IM1 concept. |
Quadrilateral Checker
Given four side lengths and whether opposite sides are parallel (a Boolean), classify the shape.
- Square: all sides equal, both pairs of opposite sides parallel
- Rectangle: opposite sides equal, both pairs parallel
- Rhombus: all sides equal, both pairs parallel, but not a square
- Parallelogram: opposite sides equal and parallel
- Trapezoid: exactly one pair of parallel sides
- Irregular quadrilateral: none of the above
// sides: top, right, bottom, left let top = 80, right = 80, bottom = 80, left = 80 let horizontalParallel = true // top ∥ bottom let verticalParallel = true // left ∥ right let allEqual = top == right && right == bottom && bottom == left let bothParallel = horizontalParallel && verticalParallel let oppEqual = top == bottom && left == right if allEqual && bothParallel { print("Square") } else if oppEqual && bothParallel { print("Rectangle") } else if allEqual && bothParallel { print("Rhombus") } else if oppEqual { print("Parallelogram") } else if horizontalParallel || verticalParallel { print("Trapezoid") } else { print("Irregular quadrilateral") }
| Standard | Connection |
|---|---|
| Geometry — Quadrilaterals | Quadrilaterals form a hierarchy: square ⊂ rectangle ⊂ parallelogram ⊂ quadrilateral. Boolean logic mirrors set inclusion — a square satisfies all conditions that a rectangle satisfies, plus more. |
Right Angle Test
Given three angles of a triangle (in degrees), write a program that verifies they sum to 180°, checks for a right angle, and checks for equiangularity.
- Verifies they sum to 180° (prints a warning if not)
- Checks whether the triangle is right-angled (has a 90° angle)
- Checks whether it is equiangular (all angles equal — which also means equilateral)
Test with: (90, 45, 45), (60, 60, 60), and (30, 60, 90).
let a1 = 90, a2 = 45, a3 = 45 if a1 + a2 + a3 != 180 { print("Warning: angles don't sum to 180°") } else { if a1 == 90 || a2 == 90 || a3 == 90 { print("Right triangle") } if a1 == a2 && a2 == a3 { print("Equiangular (equilateral)") } }
| Standard | Connection |
|---|---|
| Geometry — Triangle Angle Sum | The angle sum property of triangles states that the three interior angles always sum to 180°. An equiangular triangle has three 60° angles and must also be equilateral (equiangular ↔ equilateral for triangles). |
Colour by Rule
Draw a row of 12 squares using a loop, and colour each square based on a rule: squares in positions divisible by 3 get one colour, positions divisible by 2 (but not 3) get another, and the rest get a third colour.
Hint: Swift's modulo operator % gives the remainder: i % 3 == 0 is true when i is divisible by 3.
for i in 1...12 { if i % 3 == 0 { pen.fillColor = .blue } else if i % 2 == 0 { pen.fillColor = .yellow } else { pen.fillColor = .red } // Draw a filled square let square = Pen() for _ in 1...4 { square.addLine(distance: 50) square.turn(degrees: 90) } pen.addShape(pen: square) pen.move(distance: 60) }
| Standard | Connection |
|---|---|
| Number — Divisibility | The modulo operator checks divisibility — a key number theory concept. Multiples of 3 and multiples of 2 create overlapping sets; the union and intersection can be described using Boolean logic. |
Polygon Properties
Write a program that, given the number of sides and side length of a regular polygon, calculates and prints key properties, then draws the polygon.
- The exterior angle (360° ÷ n)
- The interior angle (180° − exterior)
- The perimeter (n × side length)
- Whether the polygon is convex (always true for regular polygons — print a note explaining why)
Then use a loop to draw the polygon at the calculated angles.
let n = 7 // heptagon let sideLength = 80 let exteriorAngle = 360.0 / Double(n) let interiorAngle = 180.0 - exteriorAngle let perimeter = n * sideLength print("Exterior angle: \(exteriorAngle)°") print("Interior angle: \(interiorAngle)°") print("Perimeter: \(perimeter)") print("Regular polygons are always convex") print("because all interior angles < 180°") for _ in 1...n { pen.addLine(distance: Double(sideLength)) pen.turn(degrees: exteriorAngle) }
| Standard | Connection |
|---|---|
| Geometry — Regular Polygons | All interior angles of a regular polygon are equal to 180° − (360°/n). A polygon is convex when all interior angles are less than 180° — satisfied by all regular polygons. |