Loops/Continue: Difference between revisions

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


=={{header|Wren}}==
=={{header|Wren}}==
Wren does not currently have a ''continue'' keyword but, depending on the circumstances, I can think of four ways in which to work around its absence.
From v0.4.0 Wren has a ''continue'' keyword which works in the expected fashion.
<lang ecmascript>// Invert the condition.
<lang ecmascript>for (i in 1..10) {
// OK for simple cases.
for (i in 1..10) {
System.write(i)
System.write(i)
if (i%5 != 0) {
System.write(", ")
} else {
System.print()
}
}

System.print()

// Use a flag to indicate whether to continue or not.
// OK as a more general approach.
for (i in 1..10) {
System.write(i)
var cont = false
if (i%5 == 0) {
if (i%5 == 0) {
System.print()
System.print()
cont = true
continue
}
}
if (!cont) System.write(", ")
System.write(", ")
}
}


System.print()
System.print()</lang>

// Break out of an inner infinite loop.
// Not good if you also need to use 'break' normally.
for (i in 1..10) {
while (true) {
System.write(i)
if (i%5 == 0) {
System.print()
break
}
System.write(", ")
break
}
}

System.print()

// Return from an inner closure.
// Heavy handed, other methods usually preferable.
for (i in 1..10) {
Fn.new {
System.write(i)
if (i%5 == 0) {
System.print()
return
}
System.write(", ")
}.call()
}</lang>


{{out}}
{{out}}
<pre>
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10

1, 2, 3, 4, 5
6, 7, 8, 9, 10

1, 2, 3, 4, 5
6, 7, 8, 9, 10

1, 2, 3, 4, 5
1, 2, 3, 4, 5
6, 7, 8, 9, 10
6, 7, 8, 9, 10