Flow-control structures: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
(→‎{{header|Wren}}: Updated to version 0.4.0.)
Line 2,871: Line 2,871:


=={{header|Wren}}==
=={{header|Wren}}==
Wren has just two control flow statements, '''break''' and '''return'''.
Wren has three 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.
'''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. It can also be used to exit the current module 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'''.
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: Line 2,887:
var i = 1
var i = 1
while (true) {
while (true) {
if (i == 1) {
i = i + 1
continue // jumps to next iteration
}
System.print(i)
System.print(i)
if (i == n) break // exits while loop
if (i == n) break // exits while loop
Line 2,900: Line 2,906:
for (n in a) {
for (n in a) {
func.call(n)
func.call(n)
if (n > 2) return // end script
if (n > 2) return // end module and hence the script as its a single module script
var error = fiber.try() // catch any error
var error = fiber.try() // catch any error
System.print("Caught error: " + error)
System.print("Caught error: " + error)
Line 2,907: Line 2,913:
{{out}}
{{out}}
<pre>
<pre>
1
2
2
Caught error: Demo error
Caught error: Demo error
1
2
2
3
3