gdscript while loop
The while
loop in GDScript is used to repeatedly execute a block of code as long as a certain condition is true. Here is the syntax for a while
loop in GDScript:
while condition:
# code to be executed
The condition
is a boolean expression that determines whether the loop should continue or not. As long as the condition
is true, the code inside the loop will be executed. Once the condition
becomes false, the loop will exit and the program will continue with the next line of code after the loop.
Here's an example of a while
loop in GDScript:
var i = 0
while i < 5:
print("Value of i:", i)
i += 1
In this example, the loop will execute as long as the value of i
is less than 5. The value of i
is printed, and then incremented by 1 in each iteration. The output of this code will be:
Value of i: 0
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Please note that the condition
in the while
loop should eventually become false to avoid an infinite loop. If the condition
never becomes false, the loop will continue indefinitely, which can lead to a program freeze or crash.