Jump to content

Flow-control structures: Difference between revisions

→‎{{header|Wren}}: Updated to version 0.4.0.
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
(→‎{{header|Wren}}: Updated to version 0.4.0.)
Line 2,871:
 
=={{header|Wren}}==
Wren has just twothree control flow statements, '''break''', '''continue''' and '''return'''.
 
'''Break''' exits from the nearest enclosing for or while loop and transfers control to the next statement after that.
 
'''Continue''' (from v0.4.0) jumps to the next iteration of the nearest enclosing for or while loop.
'''Return''' exits from a method or function and can be optionally followed by a value. Controls returns to the caller. Although undocumented, it can also be used to exit the script entirely from 'top level' code.
 
'''Return''' exits from a method or function and can be optionally followed by a value. Controls returns to the caller. Although undocumented, itIt can also be used to exit the scriptcurrent entirelymodule from 'top level' code.
 
Whilst part of the standard library rather than the language itself, the '''Fiber.abort''' method can be used to exit the script when an error occurs. However, it is possible to catch the error using '''Fiber.try'''.
Line 2,885 ⟶ 2,887:
var i = 1
while (true) {
if (i == 1) {
i = i + 1
continue // jumps to next iteration
}
System.print(i)
if (i == n) break // exits while loop
Line 2,900 ⟶ 2,906:
for (n in a) {
func.call(n)
if (n > 2) return // end module and hence the script as its a single module script
var error = fiber.try() // catch any error
System.print("Caught error: " + error)
Line 2,907 ⟶ 2,913:
{{out}}
<pre>
1
2
Caught error: Demo error
1
2
3
9,488

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.