Observe loop control best practices
Gosu provides for(), while(), and do…while() statements for loop
control. Guidewire recommends a few best practices for your loop control
logic:
Implement conditional operators in loop conditions correctly
As a best practice, Guidewire recommends that
you verify the conditional operators in your conditional expressions
to be certain that you fully satisfy the requirements for your loop control
logic. For example, <,
>, or = might need to be <=, >=, or !=.
Interrupt loop execution as early as possible
As a best practice, Guidewire recommends that
you interrupt loop execution as early as possible with continue or break commands.
Use break to break out of loop iteration altogether
The break command stops execution of the loop altogether, and program execution proceeds immediately to the code that follows the loop.
The following sample Gosu code breaks
out of the loop altogether on the fourth iteration, when i equals 4.
for (i in 1..10) {
if (i == 4) {
break // Break out of the loop on the fourth iteration.
}
print("The number is " + i)
}
print("Stopped printing numbers")
The preceding sample Gosu code produces the following output.
The number is 1
The number is 2
The number is 3
Stopped printing numbers
Notice that the loop stops executing on the fourth
iteration, when i equals
4.
Use a continue statement to continue immediately with the next loop iteration
The continue
statement stops execution of the current iteration, and the loop continues
with its next iteration.
The following sample Gosu code interrupts
the fourth iteration, when i
equals 4, but the loop
continues executing through all remaining iterations.
for (i in 1..10) {
if (i == 4) {
continue // End the fourth iteration here.
}
print("The number is " + i)
}
The preceding sample code produces the following output.
The number is 1
The number is 2
The number is 3
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10
Notice that the loop continues through all nine iterations,
but it interrupts the fourth iteration, when i equals 4.
