Continue
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 46 of 92.
The continue statement skips the rest of the current iteration and jumps to the next one. Unlike break, the loop keeps running.
for i in range(3, 9):
if i == 5:
continue
print(i)The loop goes through the numbers from 3 to 8. When i is 5, continue skips print(i), so 5 is missing from the output:
3
4
6
7
8In a while loop, update the variable before continue. A continue above the update would skip it, and the condition would never change:
var i := 0
while i < 5:
i += 1
if i == 3:
continue
print(i)This prints 1, 2, 4 and 5.
Challenge
EasyThe program prints the numbers from 1 to 20.
Add an if statement with continue inside the loop, so the program prints only the even numbers (2, 4, 6, ... 20), one per line.
Try it yourself
extends Node
func _ready():
for i in range(1, 21):
print(i)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler