Loops/Continue: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
(Added Wren) |
||
Line 2,175: | Line 2,175: | ||
Message(", ") |
Message(", ") |
||
}</lang> |
}</lang> |
||
=={{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. |
|||
<lang ecmascript>// Invert the condition. |
|||
// OK for simple cases. |
|||
for (i in 1..10) { |
|||
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) { |
|||
System.print() |
|||
cont = true |
|||
} |
|||
if (!cont) System.write(", ") |
|||
} |
|||
System.print() |
|||
// 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}} |
|||
<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 |
|||
6, 7, 8, 9, 10 |
|||
</pre> |
|||
=={{header|X86 Assembly}}== |
=={{header|X86 Assembly}}== |