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 |
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. |
|||
⚫ | |||
⚫ | |||
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 |