LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 618 min read

Control Flow

Learn GDScript's if/elif/else, while and for loops, break and continue, and the match statement — GDScript's answer to switch.

Introduction

GDScript's control flow will feel familiar if you know Python — indentation defines blocks instead of braces or an end keyword, and there's a genuinely useful match statement for multi-way branching.

What You Will Learn
  • GDScript's if/elif/else syntax.
  • while and for loops, including for loops over ranges and arrays.
  • break and continue, both fully supported in GDScript.
  • The match statement for clean multi-way branching.

if, elif, else

Blocks are defined purely by indentation — no braces, no end keyword.

var score := 72
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: F")
Output

Click Run to see what this code prints.

while Loops

var count := 1
while count <= 3:
print("count:", count)
count += 1
Output

Click Run to see what this code prints.

for Loops

GDScript's for loop can iterate over a numeric range() (like Python's), or directly over the elements of an array.

for i in range(5):
print(i)
print("--")
var enemies := ["Goblin", "Skeleton", "Slime"]
for enemy in enemies:
print("Spawning:", enemy)
Output

Click Run to see what this code prints.

break and continue

Unlike some scripting languages (Lua, for one), GDScript supports both break and continue directly — no workaround needed.

for i in range(10):
if i == 5:
break
if i % 2 == 0:
continue
print("odd:", i)
Output

Click Run to see what this code prints.

The match Statement

match compares a value against several patterns in order, similar to a switch statement in C-family languages, but more flexible — patterns can include ranges, arrays, and wildcards.

var input_action := "jump"
match input_action:
"move_left":
print("Moving left")
"move_right":
print("Moving right")
"jump":
print("Jumping!")
_:
print("Unknown action")
Output

Click Run to see what this code prints.

The _ Wildcard

The underscore pattern (_) matches anything not caught by an earlier case — GDScript's equivalent of a switch statement's default case.

Common Mistakes

Avoid These Mistakes
  • Mixing tabs and spaces for indentation — GDScript, like Python, is strict about consistent indentation defining blocks.
  • Forgetting the colon (:) at the end of an if/elif/else/while/for/match line.
  • Writing a long elif chain when a match statement would be clearer for comparing one value against many options.

Best Practices

  • Configure your editor to insert spaces (not tabs) consistently, or vice versa — just stay consistent within a project.
  • Reach for match instead of a long elif chain once you're comparing one value against three or more specific options.
  • Prefer for enemy in enemies over manually indexing an array with a while loop when you don't need the index itself.

Frequently Asked Questions

Not as separate syntax — the idiomatic equivalent is a while true: loop with an explicit break condition inside.

Yes — a single case can list several values separated by commas, or use range-like array patterns for more advanced matching.

It's a direct, deliberate borrow from Python's syntax, chosen for readability and to keep the language approachable for beginners and non-programmers alike.

Key Takeaways

  • GDScript uses indentation, not braces, to define blocks — consistency matters.
  • for loops work over range() or directly over array elements.
  • Both break and continue are fully supported.
  • match provides clean multi-way branching with a _ wildcard default case.

Summary

You can now branch and loop through GDScript code confidently. Next, you'll cover functions in depth, including the lifecycle callbacks like _ready() and _process() that Godot calls automatically.

Next Lesson →

Functions & Lifecycle Callbacks