<!-- Generated from applied-computing-au vic/unit3-4/sat/C06-2026/C06-Reference-Godot by port-reference-godot-to-wiki.py — do not hand-edit; re-run the port. --> # Control Structures — GDScript **Skill codes on this page** | Code | Level | Skill | |---|---|---| | C625 | 3–4 | control structure — sequence | | C626 | 3–4 | control structure — selection | | C632 | 5–6 | control structure — iteration / repetition | ## C625 — Sequence **Definition.** A set of instructions that executes line by line in the order that it is written. Minimum two lines whose order matters — label the start and end: ```gdscript # C625 sequence: start var zone: int = int($ZoneInput.text) var fare: float = FARE_TABLE[zone] balance -= fare $BalanceLabel.text = "Balance: $%.2f" % balance # C625 sequence: end ``` **Earns the tick:** a labelled block that breaks if you swap two lines. **Doesn't:** one isolated statement (that's C611). **In connect-go-dots** (`grid.gd`) — store the choice first, then switch screens: ```gdscript # connect-go-dots — grid.gd func _open(photo: Texture2D) -> void: Shared.photo = photo # C625 sequence: store the choice… get_tree().change_scene_to_file("res://detail.tscn") # …then change scene ``` ## C626 — Selection **Definition.** A control structure that allows a programmer to write lines of code that are only run when a particular requirement is met. ### If When condition is met, flow is directed to the alternative instruction. ```gdscript if condition: # C626 — selection # code ``` ### If … Then … Else (chained selection) When condition is met, flow can be directed multiple times. *Chained selection:* more than one condition checked in sequence — each branch can test a different condition, and execution stops at the first branch whose condition is true. ```gdscript if condition: # C626 — chained selection # code elif other_condition: # code else: # code ``` ### Switch or Case (`match`) Choose between listed alternatives. Unlike chained selection, every case tests the *same* expression for equality against a different value. ```gdscript match value: # C626 — selection: match 1: # code 2: # code ``` **In club-ladder** (`team.gd`) — a real `match`, with the wildcard `_` as the catch-all: ```gdscript # club-ladder — team.gd func record(result: String) -> void: match result: # C626 — selection: match with a catch-all "win": wins += 1 "draw": draws += 1 _: losses += 1 # anything that is not a win or a draw ``` ### Myki example ```gdscript # C626 selection: pick the fare rule if is_concession: fare = FARE_TABLE[zone] * 0.5 elif is_weekend: fare = WEEKEND_CAP else: fare = FARE_TABLE[zone] ``` *Nested selection* — a selection inside another selection's structure — is also C626 evidence; use the word "nested" in validation. **Earns the tick:** a condition that changes what runs, demonstrated with both outcomes. An `if` whose `else` can never happen is weak evidence. **In tile-matching-game** (`game.gd`) — both selection shapes: ```gdscript # tile-matching-game — game.gd if is_busy or card.is_face_up or card.is_matched: # C626 — guard clause return # bail out early on any bad click if first_card.pair_id == second_card.pair_id: # C626 — selection: two outcomes _handle_match() else: await _handle_mismatch() ``` ## C632 — Iteration / repetition **Definitions.** *WHILE:* an iteration repeated for as long as a condition is met. *FOR:* an iteration repeated a predefined number of times. *Infinite loop:* an iteration that will never reach the condition upon which it can terminate. ### While … May not execute to satisfy condition. ```gdscript while condition: # C632 — iteration # code ``` ### Do … While May not execute if condition is not satisfied once. No `do … while` construct in GDScript — a pre-test `while` loop covers this behaviour: ```gdscript while condition: # C632 — iteration # code ``` ### Repeat … Until Executes at least once. If conditions are not met, there is a possibility of an **infinite loop**. No `repeat … until` construct in GDScript — emulate a post-test loop with `while true:` and a `break` when the condition is met: ```gdscript while true: # C632 — post-test iteration # code if condition: break ``` ### For … Next A counting loop to be executed a set number of times. ```gdscript for i in range(10): # C632 — counting iteration # code ``` ### For … In A counting loop that iterates over items in an array or list. ```gdscript var cats = ["Whiskers", "Fluffy"] for cat in cats: # C632 — iteration over an array print(cat) ``` ### Myki example ```gdscript # C632 iteration: total up today's journeys var total: float = 0.0 for journey in journeys: total += journey["fare"] ``` **In connect-go-dots** (`grid.gd`) — one loop wires every photo button: ```gdscript # connect-go-dots — grid.gd for button in %Photos.get_children(): # C632 — iteration button.pressed.connect(_open.bind(button.texture_normal)) ``` `get_children()` returns an array of the six `TextureButton`s and the loop visits each in turn. (`%Photos` finds the node by its scene-unique name.) **In tile-matching-game** (`deck.gd`) — a real `while` whose end is guaranteed: ```gdscript # tile-matching-game — deck.gd while not file.eof_reached(): # C632 — ends when the file runs out of rows var row := file.get_csv_line() ``` **Earns the tick:** a loop over real data — a `for` over your own array, or a `while` whose condition provably terminates. Be ready to explain *why the loop ends*: the infinite-loop question is a validation favourite. ## Check Your Understanding 1. In a `match`, what does every case compare? >| ### Answer >| The *same* expression against a different value — unlike chained `elif`, where each branch can test something different. 2. GDScript has no `repeat … until`. What is the accepted emulation? >| ### Answer >| `while true:` with `if condition: break` at the end of the body — runs at least once, tests after. 3. What two questions about any loop should you be ready to answer live? >| ### Answer >| What it iterates over, and why it must end.
