Loops/Continue: Difference between revisions
added RPL
(mark Scheme example broken) |
(added RPL) |
||
(33 intermediate revisions by 23 users not shown) | |||
Line 1:
{{task|Iteration}}
[[Category:Loop modifiers]]
{{omit from|EasyLang|No continue statement}}
{{omit from|GUISS}}
{{omit from|M4}}
Line 35 ⟶ 36:
{{trans|Python}}
<
I i % 5 == 0
print(i)
L.continue
print(i, end' ‘, ’)</
{{out}}
Line 48 ⟶ 49:
=={{header|360 Assembly}}==
<
LOOPCONT CSECT
USING LOOPCONT,R12
Line 79 ⟶ 80:
XDEC DS CL16
YREGS
END LOOPCONT</
{{out}}
<pre> 1, 2, 3, 4, 5
Line 103 ⟶ 104:
forgo the null statement.
<
use Ada.Text_IO;
Line 117 ⟶ 118:
<<Continue>> --Ada 2012 no longer requires a statement after the label
end loop;
end Loop_Continue;</
'''N.''' This is a more true-to-Ada strategy for 'continue' comprising of an outer iteration loop and an inner labeled single-pass loop. This is a safer strategy than using goto which could be problematic when dealing with complex nested loops.
<
use Ada.Text_IO;
Line 138 ⟶ 139:
end loop Print_Element;
end loop Print_All;
end Loop_Continue;</
=={{header|Agena}}==
Agena doesn't have a continue statement, conditional statements can be used instead.
<
write( i );
if i % 5 = 0
Line 148 ⟶ 149:
else write( ", " )
fi
od</
=={{header|Aikido}}==
<
print (i)
if ((i % 5) == 0) {
Line 158 ⟶ 159:
}
print (", ")
}</
=={{header|ALGOL 60}}==
Line 182 ⟶ 183:
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
[[ALGOL 68]] has no continue reserved word, nor does it need one. The continue reserved word is only syntactic sugar for operations that can be achieved without it as in the following example:
<
print ((i,
IF i MOD 5 = 0 THEN
Line 190 ⟶ 191:
FI
))
OD</
{{Out}}
<pre>
Line 199 ⟶ 200:
=={{header|ALGOL W}}==
Algol W doesn't have a continue statement - conditional statements can be used instead.
<
i_w := 1; s_w := 0; % set output format %
for i := 1 until 10 do begin
Line 207 ⟶ 208:
else writeon( ", " )
end for_i
end.</
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">
set table to {return}
repeat with i from 1 to 10
Line 220 ⟶ 221:
end repeat
return table as string
</syntaxhighlight>
{{out}}
<pre>
Line 231 ⟶ 232:
=={{header|Arturo}}==
<
prints i
if 0 = i%5 [
Line 238 ⟶ 239:
]
prints ", "
]</
{{out}}
Line 244 ⟶ 245:
<pre>1, 2, 3, 4, 5
6, 7, 8, 9, 10</pre>
=={{header|Asymptote}}==
Asymptote's control structures are similar to those in C/C++
<syntaxhighlight lang="asymptote">for(int i = 1; i <= 10; ++i) {
write(i, suffix=none);
if(i % 5 == 0) {
write("");
continue;
} else {
write(", ", suffix=none);
}
}</syntaxhighlight>
=={{header|AutoHotkey}}==
<
Delimiter := (A_Index = 5) || (A_Index = 10) ? "`n":", "
Index .= A_Index . Delimiter
}
MsgBox %Index%</
=={{header|AWK}}==
<
for(i=1; i <= 10; i++) {
printf("%d", i)
Line 262 ⟶ 275:
printf(", ")
}
}</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
20 PRINT I;
30 IF I - INT (I / 5) * 5 = 0 THEN PRINT : GOTO 50"CONTINUE
40 PRINT ", ";
50 NEXT</
==={{header|BASIC256}}===
<
print string(i);
if i mod 5 = 0 then
Line 283 ⟶ 296:
next
print
end</
==={{header|BBC BASIC}}===
BBC BASIC doesn't have a 'continue' statement so the remainder of the loop must be made conditional.
<
PRINT ; i% ;
IF i% MOD 5 = 0 PRINT ELSE PRINT ", ";
NEXT</
==={{header|Commodore BASIC}}===
Commodore BASIC also doesn't have a 'continue' statement. In this example, a GOTO statement is used to simulate 'CONTINUE'. However, Commodore BASIC doesn't have a modulo (remainder) operator, so value of I/5 is check against INT(I/5). If they are the same, the remainder is zero.
<
20 PRINT I;
30 IF INT(I/5) = I/5 THEN PRINT : GOTO 50
40 PRINT ", ";
50 NEXT</
==={{header|FreeBASIC}}===
<
For i As Integer = 1 To 10
Print Str(i);
Line 312 ⟶ 325:
Print
Sleep</
{{out}}
Line 321 ⟶ 334:
==={{header|IS-BASIC}}===
<
110 PRINT STR$(I);
120 IF MOD(I,5)=0 THEN
Line 328 ⟶ 341:
150 PRINT ", ";
160 END IF
170 NEXT</
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">
for i =1 to 10
if i mod 5 <>0 then print i; ", "; else print i
next i
end
</syntaxhighlight>
==={{header|PureBasic}}===
<
For i.i = 1 To 10
Line 350 ⟶ 363:
Next
Repeat: Until Inkey() <> ""</
==={{header|QB64}}===
<
For i = 1 To 10
Print LTrim$(Str$(i));
Line 361 ⟶ 374:
End If
Print ", ";
Next</
==={{header|Run BASIC}}===
{{works with|QBasic}}
<
if i mod 5 <> 0 then print i;", "; else print i
next i</
==={{header|Sinclair ZX81 BASIC}}===
This probably isn't the most idiomatic way to produce the specified output—but it does illustrate ZX81 BASIC's equivalent of <code>if <condition> continue</code>, which is <code>IF <condition> THEN NEXT <loop-control variable></code>.
<syntaxhighlight lang="text">10 FOR I=1 TO 10
20 PRINT I;
30 IF I/5=INT (I/5) THEN PRINT
40 IF I/5=INT (I/5) THEN NEXT I
50 PRINT ", ";
60 NEXT I</
==={{header|TI-89 BASIC}}===
<
Prgm
""→s
Line 391 ⟶ 404:
s&", "→s
EndFor
EndPrgm</
Ti-89 lacks support for multi-argument display command or controlling the print position so that one can print several data on the same line. The display command (Disp) only accepts one argument and prints it on a single line (causing a line a feed at the end, so that the next Disp command will print in the next line). The solution is appending data to a string (s), using the concatenator operator (&), by converting numbers to strings, and then printing the string at the end of the line.
==={{header|True BASIC}}===
<
PRINT STR$(i);
IF REMAINDER(i, 5) = 0 THEN
Line 405 ⟶ 418:
NEXT i
PRINT
END</
==={{header|VB-DOS, PDS}}===
<syntaxhighlight lang="qbasic">
OPTION EXPLICIT
Line 418 ⟶ 431:
IF (i MOD 5) THEN PRINT ","; ELSE PRINT
NEXT i
END</
==={{header|Visual Basic .NET}}===
<
Console.Write(i)
If i Mod 5 = 0 Then
Line 428 ⟶ 441:
Console.Write(", ")
End If
Next</
=={{header|bc}}==
Line 434 ⟶ 447:
{{works with|OpenBSD bc}}
<
print i
if (i % 5) {
Line 442 ⟶ 455:
print "\n"
}
quit</
=={{header|BCPL}}==
In BCPL, the <tt>continue</tt> statement is named <tt>loop</tt>.
<syntaxhighlight lang="bcpl">get "libhdr"
let start() be
for i = 1 to 10
$( writen(i)
if i rem 5 = 0
$( wrch('*N')
loop
$)
writes(", ")
$)</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5
6, 7, 8, 9, 10</pre>
=={{header|Befunge}}==
Befunge outputs numbers with a space after them, so the formatting is slightly off in this version.
<syntaxhighlight lang="befunge">
1>:56+\`#v_@
+v %5:.:<
Line 453 ⟶ 484:
>" ,",,v
^ <
</syntaxhighlight>
This version outputs a 'backspace' ASCII character to try to correct the format, but it may or may not work depending on if the character is accounted for by the output
<syntaxhighlight lang="befunge">
1>:56+\`#v_@
+v5:,8.:<
Line 463 ⟶ 494:
>" ,",v
^ ,<
</syntaxhighlight>
=={{header|Bracmat}}==
Bracmat has no continue statement.
<
& whl
' ( 1+!i:~>10:?i
Line 477 ⟶ 508:
)
)
);</
=={{header|C}}==
{{trans|C++}}
<
printf("%d", i);
if(i % 5 == 0){
Line 488 ⟶ 519:
}
printf(", ");
}</
=={{header|C sharp|C#}}==
{{trans|Java}}
<
class Program {
Line 507 ⟶ 538:
}
}
}</
=={{header|C++}}==
{{trans|Java}}
<
cout << i;
if(i % 5 == 0){
Line 518 ⟶ 549:
}
cout << ", ";
}</
=={{header|C3}}==
{{trans|Java}}
<syntaxhighlight lang="c3">for (int i = 1; i <= 10; i++)
{
io::print(i);
if (i % 5 == 0)
{
io::printn();
continue;
}
io::print(", ");
}</syntaxhighlight>
=={{header|Chapel}}==
<
write(i);
if i % 5 == 0 then {
Line 528 ⟶ 572:
}
write(", ");
}</
=={{header|Clipper}}==
Line 534 ⟶ 578:
Works as is with Harbour 3.0.0 (Rev. 16951)
<
?? i
IF i % 5 == 0
Line 541 ⟶ 585:
ENDIF
?? ", "
NEXT</
=={{header|Clojure}}==
Clojure doesn't have a continue keyword. It has a recur keyword, although I prefer to work with ranges in this case.
<
(print n)
(if (zero? (rem n 5))
(println)
(print ", ")))</
To address the task, however, here's an example loop/recur:
<
(when-let [x (first xs)]
(print x)
Line 558 ⟶ 602:
(println)
(print ", "))
(recur (rest xs))))</
=={{header|COBOL}}==
<
PROGRAM-ID. loop-continue.
Line 581 ⟶ 625:
GOBACK
.</
Note: COBOL does have a <code>CONTINUE</code> verb, but this is a no-operation statement used in <code>IF</code> and <code>EVALUATE</code> statements.
Line 587 ⟶ 631:
=={{header|ColdFusion}}==
Remove the leading space from the line break tag.
<
for( i = 1; i <= 10; i++ )
{
Line 598 ⟶ 642:
writeOutput( "," );
}
</cfscript></
=={{header|Common Lisp}}==
Line 606 ⟶ 650:
The second uses the implicit <code>tagbody</code> and <code>go</code>.
The third is a do loop with conditionals outside of the output functions.
<
((> i 10))
(format t "~a~:[, ~;~%~]" i (zerop (mod i 5))))
Line 624 ⟶ 668:
(if (zerop (mod i 5))
(terpri)
(write-string ", ")))</
These use the <code>loop</code> iteration form, which does not contain an implicit tagbody (though one could be explicitly included).
Line 630 ⟶ 674:
the second uses <code>block</code>/<code>return-from</code> to obtain the effect of skipping the rest of the code in the <code>block</code> which makes up the entire loop body.
<
do (write i)
if (zerop (mod i 5))
Line 643 ⟶ 687:
(terpri)
(return-from continue))
(write-string ", ")))</
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
var n: uint8 := 0;
while n < 10 loop
n := n + 1;
print_i8(n);
if n % 5 == 0 then
print_nl();
continue;
end if;
print(", ");
end loop;</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5
6, 7, 8, 9, 10</pre>
=={{header|D}}==
<
void main() {
Line 657 ⟶ 718:
write(", ");
}
}</
{{out}}
<pre>1, 2, 3, 4, 5
Line 663 ⟶ 724:
===Shorter version===
<syntaxhighlight lang="d">
import std.stdio;
Line 670 ⟶ 731:
foreach(i; 1..11) i % 5 ? writef("%s, ", i) : writeln(i);
}
</syntaxhighlight>
=={{header|dc}}==
Line 680 ⟶ 741:
{{works with|OpenBSD dc}}
<
[2Q]sA # A = code to break loop
[[, ]P 1J]sB # B = code to print comma, continue loop
Line 691 ⟶ 752:
li 1 + si # i += 1
li 10!<C # continue loop if 10 >= i
]sC li 10!<C # enter loop if 10 >= i</
This program uses <tt>J</tt> and <tt>M</tt> to force the next iteration of a loop.
Line 699 ⟶ 760:
=={{header|Delphi}}==
<
var
i: integer;
Line 713 ⟶ 774:
write(', ');
end;
end.</
{{Out}}
Line 723 ⟶ 784:
=={{header|DWScript}}==
<
for i := 1 to 10 do begin
Line 732 ⟶ 793:
end;
Print(', ');
end;</
=={{header|Dyalect}}==
Line 738 ⟶ 799:
{{trans|Swift}}
<
print(i, terminator: "")
if i % 5 == 0 {
Line 745 ⟶ 806:
}
print(", ", terminator: "")
}</
{{out}}
Line 755 ⟶ 816:
===Direct Approach===
<
loop n =
Line 767 ⟶ 828:
| else = ", "
_ = loop 1 ::: IO</
===Using list===
<
loop [] = return ()
Line 780 ⟶ 841:
| else = ", "
_ = loop [1..10] ::: IO</
This version is more generic and can work for any given range of values.
=={{header|Elixir}}==
<
def continue do
Enum.each(1..10, fn i ->
Line 794 ⟶ 855:
end
Loops.continue</
{{out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
for int i = 1; i <= 10; ++i
write(i)
if i % 5 == 0
writeLine()
continue
end
write(", ")
end
</syntaxhighlight>
{{out}}
<pre>
Line 803 ⟶ 881:
=={{header|Erlang}}==
<
-module(continue).
-export([main/0, for_loop/1]).
Line 824 ⟶ 902:
for_loop(N+1)
end.
</syntaxhighlight>
{{out}}
<pre>1, 2, 3, 4, 5
Line 831 ⟶ 909:
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
FOR I=1 TO 10 DO
PRINT(I;CHR$(29);) ! printing a numeric value leaves a blank after it
Line 842 ⟶ 920:
END FOR
PRINT
</syntaxhighlight>
=={{header|Euphoria}}==
{{works with|Euphoria|4.0.3, 4.0.0 or later}}
<
for i = 1 to 10 do
Line 856 ⟶ 934:
end if
end for
any_key()</
Version without newline after 10 below.
<
for i = 1 to 10 do
Line 876 ⟶ 954:
end for
any_key()
</syntaxhighlight>
=={{header|F Sharp|F#}}==
Line 882 ⟶ 960:
In any case, it is not needed to complete this task.
==={{trans|Ada}}===
<
printf "%d" i
if i % 5 = 0 then
printf "\n"
else
printf ", "</
===Using [[Comma quibbling#The Function]]===
<
let fN g=quibble (Seq.initInfinite(fun n ->if (n+1)%5=0 || (n+1)=List.length g then "\n" else ", ")) g
fN [1] |> Seq.iter(fun(n,g)->printf "%d%s" n g)
Line 895 ⟶ 973:
fN [1..10] |> Seq.iter(fun(n,g)->printf "%d%s" n g)
fN [1..11] |> Seq.iter(fun(n,g)->printf "%d%s" n g)
</syntaxhighlight>
{{out}}
<pre>
Line 910 ⟶ 988:
=={{header|Factor}}==
There is no built-in <code>continue</code> in Factor.
<
[ number>string write ]
[ 5 mod 0 = "\n" ", " ? write ] bi
] each</
=={{header|Fantom}}==
Line 919 ⟶ 997:
While and for loops support <code>continue</code> to jump back to begin the next iteration of the loop.
<
class LoopsContinue
{
Line 937 ⟶ 1,015:
}
}
</syntaxhighlight>
=={{header|Forth}}==
Although this code solves the task, there is no portable equivalent to "continue" for either DO-LOOPs or BEGIN loops.
<
11 1 do
i dup 1 r.
5 mod 0= if cr else [char] , emit space then
loop ;</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
write(*, '(I0)', advance='no') i
if ( mod(i, 5) == 0 ) then
Line 956 ⟶ 1,034:
end if
write(*, '(A)', advance='no') ', '
end do</
{{works with|Fortran|77 and later}}
<
C one nonstandard character on the line labelled 5001. Many F77
C compilers should be okay with it, but it is *not* standard.
Line 1,025 ⟶ 1,103:
5001 FORMAT (I3, ',', $)
C5001 FORMAT (I3, ',', ADVANCE='NO')
END</
===Relying instead upon the looping features of FORMAT===
For historical reasons, 6 is often the default unit number for standard output.
<syntaxhighlight lang="fortran">
WRITE (6,1) (I,I = 1,10)
1 FORMAT (4(1X,I0,","),1X,I0)
END
</syntaxhighlight>
Here the break and continuation comes through the workings of the FORMAT interpreter. The feature 4(etc) means four repetitions of the format items within the brackets, and as each datum from the WRITE statement arrives, it is aligned with the next format item that can receive a datum, the I-format specifier (here I0, which means an integer of only as many digits as are needed for the value) and until such a reciever is encountered, intervening format items are acted upon - 1X means "one space", and the quotes surround a text literal. Accordingly, the first datum generates a space, a one-digit value, and a comma, as does the second and so on. When the sixth datum is received, the end of the format statement has been reached, and the convention is to write the current line and start a new line of output, and further, go back in the FORMAT specification to the first-encountered open-bracket symbol (the rightmost) which in this case is not the beginning of the FORMAT statement but the one that has a repetition count of four in front of it, and, resume interpretation. When the last datum has been accepted, naturally, the line is printed.
Line 1,042 ⟶ 1,120:
This sort of scheme facilitates a compact way of printing a table with a heading, where the WRITE statement simply pours forth the data and relies on something like FORMAT("heading",/,(complex details for one line)) - thus printing the table line-by-line with only the first line having the heading, a saving on having a write and format statement pair for the heading and a second pair for the table body.
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
long num
for num = 1 to 10
if ( num mod 5 )
NSLog(@"%ld, \b",num)
else
NSLog(@"%ld",num)
end if
next
HandleEvents</syntaxhighlight>
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=bf629ae9a09ffa1f5ecc95b89854b14b Click this link to run this code]'''
<
Dim siCount As Short
Line 1,054 ⟶ 1,147:
Next
End</
Output:
<pre>
Line 1,062 ⟶ 1,155:
=={{header|GAP}}==
<
if RemInt(i, 5) = 0 then
Print(i, "\n");
Line 1,071 ⟶ 1,164:
# 1, 2, 3, 4, 5
# 6, 7, 8, 9, 10</
=={{header|GDScript}}==
{{works with|Godot|4.0.1}}
{{trans|11l}}
<syntaxhighlight lang="gdscript">
extends MainLoop
func _process(_delta: float) -> bool:
for i in range(1,11):
if i % 5 == 0:
print(i)
continue
printraw(i, ", ")
return true # Exit
</syntaxhighlight>
=={{header|GML}}==
<
{
show_message(string(i))
Line 1,080 ⟶ 1,192:
if(i <= 10)
continue
}</
=={{header|Go}}==
<
import "fmt"
Line 1,096 ⟶ 1,208:
fmt.Printf(", ")
}
}</
{{Out}}
<pre>
Line 1,104 ⟶ 1,216:
=={{header|Groovy}}==
<
print i
if (i % 5 == 0) {
Line 1,111 ⟶ 1,223:
}
print ', '
}</
=={{header|Haskell}}==
Line 1,117 ⟶ 1,229:
The below code uses a guard (| symbol) to compose functions differently for the two alternative output paths, instead of using continue like in an imperative language.
<
main = forM [1..10] out
where
out x | x `mod` 5 == 0 = print x
| otherwise = (putStr . (++", ") . show) x</
=={{header|Haxe}}==
<
Sys.print(i);
if (i % 5 == 0) {
Line 1,131 ⟶ 1,243:
}
Sys.print(', ');
}</
=={{header|HicEst}}==
<
IF( MOD(i, 5) == 1 ) THEN
WRITE(Format="i3") i
Line 1,140 ⟶ 1,252:
WRITE(APPend, Format=" ',', i3 ") i
ENDIF
ENDDO </
=={{header|Icon}} and {{header|Unicon}}==
The following code demonstrates the use of 'next' (the reserved word for 'continue'):
<
every writes(x := 1 to 10) do {
if x % 5 = 0 then {
Line 1,152 ⟶ 1,264:
writes(", ")
}
end</
However, the output sequence can be written without 'next' and far more succinctly as:
<
=={{header|Io}}==
<
write(i)
if(i%5 == 0, writeln() ; continue)
write(" ,")
)</
=={{header|J}}==
Line 1,167 ⟶ 1,279:
For example, one could satisfy this task this way:
<
J does support loops for those times they can't be avoided
(just like many languages support gotos for those time they can't be avoided).
<
z=.''
for_i. 1 + i.y do.
Line 1,185 ⟶ 1,297:
end.
i.0 0
)</
Though it's rare to see J code like this.
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn main() {
for i in 1..11 {
if i % 5 == 0 {
println("{}", i)
continue
}
print("{}, ", i)
}
}
</syntaxhighlight>
=={{header|Java}}==
<
System.out.print(i);
if(i % 5 == 0){
Line 1,197 ⟶ 1,322:
}
System.out.print(", ");
}</
=={{header|JavaScript}}==
Using the <code>print()</code> function from [[Rhino]] or [[SpiderMonkey]].
<
for (var i = 1; i <= 10; i++) {
output += i;
Line 1,210 ⟶ 1,335:
}
output += ", ";
}</
Line 1,217 ⟶ 1,342:
For example:
<
return n ? rng(n - 1).concat(n) : [];
}
Line 1,227 ⟶ 1,352:
}, ''
)
);</
Output:
<
6, 7, 8, 9, 10
</syntaxhighlight>
=={{header|jq}}==
jq does not have a "continue" statement.
In jq 1.4, the simplest way to accomplish the given task is probably as follows:
<
(""; . + "\($i)" + (if $i % 5 == 0 then "\n" else ", " end))</
=={{header|Jsish}}==
<
for (var i = 1; i <= 10; i++) {
printf("%d", i);
Line 1,249 ⟶ 1,374:
}
printf(", ");
}</
{{out}}
Line 1,257 ⟶ 1,382:
=={{header|Julia}}==
<syntaxhighlight lang="julia">
for i in 1:10
print(i)
Line 1,266 ⟶ 1,391:
print(", ")
end
</syntaxhighlight>
{{out}}
Line 1,275 ⟶ 1,400:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,285 ⟶ 1,410:
print("$i, ")
}
}</
{{out}}
Line 1,294 ⟶ 1,419:
=={{header|Lambdatalk}}==
<
{def loops_continue
{lambda {:i}
Line 1,307 ⟶ 1,432:
-> 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10. (end of loop)
</syntaxhighlight>
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$i = 0
while($i < 10) {
$i += 1
if($i % 5 === 0) {
fn.println($i)
con.continue
}
fn.print($i\,\s)
}
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|langur}}==
<syntaxhighlight lang="langur">for .i of 10 {
write .i
if .i div 5 { writeln(); next }
write ", "
}</
=={{header|Lasso}}==
<
loop_count
loop_count % 5 ? ', ' | '\r'
loop_count < 100 ? loop_continue
'Hello, World!' // never gets executed
^}</
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
i is number
n is number
procedure:
for i from 1 to 11 step 1 do
display i
modulo i by 5 in n
if n is equal to 0 then
display lf
continue
end if
display ", "
repeat</syntaxhighlight>
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|Lingo}}==
<
repeat with i = 1 to 10
put i after str
Line 1,335 ⟶ 1,501:
put ", " after str
end repeat
put str</
=={{header|Lisaac}}==
<
i.print;
(i % 5 = 0).if { '\n'.print; } else { ','.print; };
};</
=={{header|LiveCode}}==
<
put n
if n is 5 then put return
if n < 10 and n is not 5 then put ","
end repeat</
=={{header|Lua}}==
<
io.write( i )
if i % 5 == 0 then
Line 1,358 ⟶ 1,524:
io.write( ", " )
end
end</
or
<
io.write( i )
if i % 5 == 0 then
Line 1,368 ⟶ 1,534:
io.write( ", " )
::continue::
end</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
\\ A For {} loop
Line 1,408 ⟶ 1,574:
}
Checkit
</syntaxhighlight>
=={{header|Maple}}==
<
printf( "%d", i );
if irem( i, 5 ) = 0 then
Line 1,418 ⟶ 1,584:
end if;
printf( ", " )
end do:</
This can also be done as follows, but without the use of "next".
<
printf( "%d%s", i, `if`( irem( i, 5 ) = 0, "\n", ", " ) )
end do:</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
For[i = 1, i <= 10, i++,
tmp = tmp <> ToString[i];
Line 1,435 ⟶ 1,601:
];
];
Print[tmp]</
=={{header|MATLAB}} / {{header|Octave}}==
Line 1,441 ⟶ 1,607:
Loops are considered slow in Matlab and Octave,
it is preferable to vectorize the code.
<
or
<
A non-vectorized version of the code is shown below in Octave
<
printf(' %2d', i);
if ( mod(i, 5) == 0 )
Line 1,453 ⟶ 1,619:
continue
end
end</
=={{header|Maxima}}==
<
block(
[s: ""],
Line 1,468 ⟶ 1,634:
)
)
)$</
Using sprint and newline
<syntaxhighlight lang="maxima">
for n:1 thru 10 do (
sprint(n),
if n=5 then newline())$
</syntaxhighlight>
=={{header|MAXScript}}==
<
(
format "%" i
Line 1,480 ⟶ 1,652:
) continue
format ", "
)</
<nowiki>Insert non-formatted text here</nowiki>
Line 1,487 ⟶ 1,659:
As the [[Loop/Continue#Ada|Ada solution]], we can complete the task just with conditional.
<
for i = 1 step 1 until 10:
if i mod 5 = 0:
Line 1,495 ⟶ 1,667:
fi; endfor
message s;
end</
Since <tt>message</tt> append always a newline at the end,
Line 1,503 ⟶ 1,675:
'''Note''': <tt>mod</tt> is not a built in; like TeX, "bare Metafont" is rather primitive, and normally a set of basic macros is preloaded to make it more usable; in particular <tt>mod</tt> is defined as
<
=={{header|Modula-3}}==
Line 1,513 ⟶ 1,685:
Module code and imports are omitted.
<
IO.PutInt(i);
IF i MOD 5 = 0 THEN
Line 1,520 ⟶ 1,692:
END;
IO.Put(", ");
END;</
=={{header|MOO}}==
<
for i in [1..10]
s += tostr(i);
Line 1,532 ⟶ 1,704:
endif
s += ", ";
endfor</
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Loops/Continue in Neko
Tectonics:
Line 1,554 ⟶ 1,726:
}
$print(", ");
}</
{{out}}
Line 1,564 ⟶ 1,736:
=={{header|Nemerle}}==
{{trans|C#}}
<
using System.Console;
using Nemerle.Imperative;
Line 1,579 ⟶ 1,751:
}
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 1,598 ⟶ 1,770:
end i_
</syntaxhighlight>
=={{header|NewLISP}}==
<
(print i)
(if (= 0 (% i 5))
(println)
(print ", ")))</
=={{header|Nim}}==
{{trans|Python}}
<
if i mod 5 == 0:
echo i
continue
stdout.write i, ", "</
=={{header|NS-HUBASIC}}==
<
20 PRINT I;
30 IF I-I/5*5=0 THEN PRINT :GOTO 50"CONTINUE
40 PRINT ",";
50 NEXT</
=={{header|Nu}}==
<syntaxhighlight lang="nu">
for i in 1..10 {
print -n $i
if $i mod 5 == 0 {
print ""
continue
}
print -n ", "
}
</syntaxhighlight>
=={{header|Objeck}}==
<
function : Main(args : String[]) ~ Nil {
for(i := 1; i <= 10; i += 1;) {
Line 1,633 ⟶ 1,817:
};
}
}</
=={{header|OCaml}}==
There is no continue statement for for loops in OCaml,
but it is possible to achieve the same effect with an exception.
<
try
print_int i;
Line 1,649 ⟶ 1,833:
1, 2, 3, 4, 5
6, 7, 8, 9, 10
- : unit = ()</
Though even if the continue statement does not exist,
it is possible to add it with camlp4.
=={{header|Octave}}==
<
for i = 1:10
v = sprintf("%s%d", v, i);
Line 1,663 ⟶ 1,847:
endif
v = sprintf("%s, ", v);
endfor</
=={{header|Oforth}}==
<
| i |
10 loop: i [
i dup print 5 mod ifZero: [ printcr continue ]
"," .
] ;</
=={{header|Ol}}==
We use continuation to break the execution of the inner body.
<syntaxhighlight lang="scheme">
(let loop ((i 1))
(when (less? i 11)
(call/cc (lambda (continue)
(display i)
(when (zero? (mod i 5))
(print)
(continue #f))
(display ", ")))
(loop (+ i 1))))
</syntaxhighlight>
{{Out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|Oz}}==
By using the "continue" feature of the for-loop, we bind C to a nullary procedure which, when invoked, immediately goes on to the next iteration of the loop.
<
{System.print I}
if I mod 5 == 0 then
Line 1,683 ⟶ 1,886:
end
{System.printInfo ", "}
end</
=={{header|PARI/GP}}==
<
print1(n);
if(n%5 == 0, print();continue);
print1(", ")
)</
=={{header|Pascal}}==
Line 1,696 ⟶ 1,899:
=={{header|Perl}}==
<
print $_;
if ($_ % 5 == 0) {
Line 1,703 ⟶ 1,906:
}
print ', ';
}</
It is also possible to use a goto statement
to jump over the iterative code section for a particular loop:
<
print $_;
if ($_ % 5 == 0) {
Line 1,716 ⟶ 1,919:
print ', ';
MYLABEL:
}</
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
Line 1,729 ⟶ 1,933:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
The following works just as well, with identical output
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
Line 1,740 ⟶ 1,950:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
=={{header|PHP}}==
<
echo $i;
if ($i % 5 == 0) {
Line 1,750 ⟶ 1,960:
}
echo ', ';
}</
=={{header|Picat}}==
Picat doesn't have a continue statement. So I just use a conditional that ends the body of the predicate.
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
main =>
foreach (I in 1..10)
printf("%d", I),
if (I mod 5 == 0) then
nl
else
printf(", ")
end,
end.
</syntaxhighlight>
{{out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|PicoLisp}}==
PicoLisp doesn't have an explicit 'continue' functionality.
It can always be emulated with a conditional expression.
<
(print I)
(if (=0 (% I 5))
(prinl)
(prin ", ") ) )</
=={{header|Pike}}==
<
for(int i = 1; i <= 10; i++){
write(sprintf("%d",i));
Line 1,771 ⟶ 2,003:
write(", ");
}
}</
=={{header|PL/I}}==
<
do i = 1 to 10;
put edit (i) (f(3));
if mod(i,5) = 0 then do; put skip; iterate loop; end;
put edit (', ') (a);
end;</
=={{header|Plain English}}==
In Plain English, continue is spelled <code>repeat</code> and is the only way to specify an end of a loop.
<
Start up.
Demonstrate continue.
Line 1,795 ⟶ 2,027:
If the counter is evenly divisible by 5, write "" on the console; repeat.
Write ", " on the console without advancing.
Repeat.</
=={{header|Pop11}}==
<
for i from 1 to 10 do
printf(i, '%p');
Line 1,806 ⟶ 2,038:
endif;
printf(', ')
endfor;</
=={{header|PowerShell}}==
{{trans|C}}
<
Write-Host -NoNewline $i
if ($i % 5 -eq 0) {
Line 1,817 ⟶ 2,049:
}
Write-Host -NoNewline ", "
}</
=={{header|Prolog}}==
Prolog doesn't have a continue statement. So I just use a conditional that ends the body of the predicate.
{{works with|GNU Prolog}}
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">
:- initialization(main).
print_list(Min, Max) :-
Min < Max,
write(Min),
Min1 is Min + 1,
(
Min mod 5 =:= 0
-> nl
; write(',')
),
print_list(Min1, Max).
print_list(Max, Max) :-
write(Max),
nl.
main :-
print_list(1, 10).
</syntaxhighlight>
{{out}}
<pre>
1,2,3,4,5
6,7,8,9,10
</pre>
=={{header|Python}}==
<
if i % 5 == 0:
print
continue
print
=={{header|Quackery}}==
<
[ i^ 1+ dup echo
5 mod 0 = iff
cr done
say ", " ]</
=={{header|R}}==
{{trans|C++}}
<
{
cat(i)
Line 1,844 ⟶ 2,108:
}
cat(", ")
}</
=={{header|Racket}}==
Line 1,851 ⟶ 2,115:
but an explicit <tt>continue</tt> construct is rarely used:
<
#lang racket
Line 1,866 ⟶ 2,130:
(printf "~a~n" i)))
(printf "~a, " i))
</syntaxhighlight>
=={{header|Raku}}==
Line 1,872 ⟶ 2,136:
{{trans|Perl}}
{{works with|Rakudo Star|2010.08}}
<syntaxhighlight lang="raku"
.print;
if $_ %% 5 {
Line 1,879 ⟶ 2,143:
}
print ', ';
}</
or without using a loop:
<syntaxhighlight lang="raku"
=={{header|REBOL}}==
<
Title: "Loop/Continue"
URL: http://rosettacode.org/wiki/Loop/Continue
Line 1,905 ⟶ 2,169:
prin ", "
]
]</
{{Out}}
Line 1,917 ⟶ 2,181:
=={{header|Red}}==
<
prin i
if i = 10 [break]
Line 1,923 ⟶ 2,187:
]
1,2,3,4,5
6,7,8,9,10</
=={{header|REXX}}==
===version 1===
(This program could be simpler by using a '''then/else''' construct, but an '''iterate''' was used to conform to the task.)
<
do j=1 for 10 /*this is equivalent to: DO J=1 TO 10 */
Line 1,938 ⟶ 2,202:
say /*force REXX to display on next line. */
end /*j*/
/*stick a fork in it, we're all done. */</
Program note: the comma (<big><b>,</b></big>) immediately after the '''charout''' BIF indicates to use the terminal output stream.
Line 1,948 ⟶ 2,212:
===version 2===
<
$= /*nullify the variable used for display*/
do j=1 for 10 /*this is equivalent to: DO J=1 TO 10 */
Line 1,955 ⟶ 2,219:
if j==5 then $= /*start the display line over again. */
end /*j*/
/*stick a fork in it, we're all done. */</
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br>
=={{header|Ring}}==
<
for i = 1 TO 10
see i
Line 1,968 ⟶ 2,232:
see ", "
next
</syntaxhighlight>
=={{header|RPL}}==
You need an <code>IF..THEN..ELSE</code> structure to do that in RPL.
« ""
1 10 '''FOR''' j
j +
'''IF''' j 5 MOD '''THEN''' ", " + '''ELSE''' "" '''END'''
'''NEXT''' DROP
» '<span style="color:blue">TASK</span>' STO
=={{header|Ruby}}==
<
print i
if i % 5 == 0 then
Line 1,978 ⟶ 2,251:
end
print ', '
end</
The "for" look could be written like this:
<
1.upto(10) do |i| ...
10.times do |n| i=n+1; ...</
Without meeting the criteria (showing loop continuation), this task could be written as:
<
=={{header|Rust}}==
<
for i in 1..=10 {
print!("{}", i);
Line 1,996 ⟶ 2,269:
print!(", ");
}
}</
=={{header|Salmon}}==
<
{
print(x);
Line 2,008 ⟶ 2,281:
};
print(", ");
};</
=={{header|Sather}}==
There's no <code>continue!</code> in Sather. The code solve the task without forcing a new iteration.
<
main is
i:INT;
Line 2,024 ⟶ 2,297:
end;
end;
end;</
=={{header|Scala}}==
Line 2,031 ⟶ 2,304:
===The intuitive way===
<
print(i)
if (i % 5 == 0) println() else print(", ")
}</
===Functional solution===
Line 2,043 ⟶ 2,316:
#The map makes for both elements in the List a conversion to a comma separated String, yielding a List of two Strings.
#Both comma separated strings will be separated by an EOL
<
println(List(a._1, a._2) /*3.*/ .map(_.mkString(", ") /*4.*/ ).mkString("\n") /*5.*/ )</
=={{header|Scheme}}==
For R7RS Scheme. In this functional solution, there is no "continue". Instead, the "loop" function is directly called in the tail end (this is [[Recursion|Tail Recursion]]).
<
(scheme write))
(define (loop-fn start end)
(cond ((zero? (modulo i 5))
(newline) (loop (+ 1 i)))
(else
(display ", ")
(loop (+ 1 i)))))))
(loop start))
(loop-fn 1 10)</syntaxhighlight>
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=1:10
printf("%2d ",i)
if modulo(i,5)~=0 then
Line 2,066 ⟶ 2,347:
end
printf("\n")
end </
{{out}}
<pre> 1 , 2 , 3 , 4 , 5
Line 2,072 ⟶ 2,353:
=={{header|Sidef}}==
<
print i
if (i %% 5) {
Line 2,079 ⟶ 2,360:
}
print ', '
}</
=={{header|Simula}}==
{{works with|SIMULA-67}}
<
begin
integer i;
Line 2,095 ⟶ 2,376:
loop:
end
end</
{{out}}
<pre>
Line 2,104 ⟶ 2,385:
=={{header|Smalltalk}}==
{{works with|Pharo}} {{works with|Smalltalk/X}} actually works with all dialects ¹
<syntaxhighlight lang="smalltalk">
1 to: 10 do: [ :i |
[ :continue |
Line 2,115 ⟶ 2,396:
] valueWithExit.
]
</syntaxhighlight>
¹ if valueWithExit is not present in the Block class, it can be added as:
<
^ self value:[^ nil]</
=={{header|SNOBOL4}}==
SNOBOL4 has no looping statements or conditional statements. Indeed the only branching facilities it has are:
* Unconditional branch to label. <code>:(LABEL)</code>
* Branch to label on success. <code>:S(LABEL)</code>
* Branch to label on failure. <code>:F(LABEL)</code>
(The success/failure labels can both be in the branching clause.)
Despite this, any looping structure can be performed by careful use of these.
<syntaxhighlight lang="snobol4">
line =
i = 1
LOOP le(i, 10) :F(LOOP.END)
line = line i
eq(remdr(i, 5), 0) :S(LOOP.OUT)
line = line ', ' :(LOOP.INC)
LOOP.OUT OUTPUT = line
line =
LOOP.INC i = i + 1 :(LOOP)
LOOP.END OUTPUT = line
END
</syntaxhighlight>
{{Out}}
<pre>
$ snobol4 junk.sno
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|Spin}}==
Line 2,125 ⟶ 2,441:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<syntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,144 ⟶ 2,460:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>
Line 2,152 ⟶ 2,468:
=={{header|SPL}}==
<
s += n
? n%5, s += ", "
Line 2,158 ⟶ 2,474:
#.output(s)
s = ""
<</
{{out}}
<pre>
Line 2,168 ⟶ 2,484:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 2,186 ⟶ 2,502:
END WHILE Loop;
END @
</syntaxhighlight>
Output:
<pre>
Line 2,201 ⟶ 2,517:
See '''[https://www.stata.com/help.cgi?continue continue]''' in Stata help. Notice that the _continue option of '''[https://www.stata.com/help.cgi?display display]''' has another purpose: it suppresses the automatic newline at the end of the display command.
<
display `n' _continue
if mod(`n',5)==0 {
Line 2,208 ⟶ 2,524:
}
display ", " _continue
}</
=={{header|Suneido}}==
<
for (i = 1; i <= 10; ++i)
{
Line 2,221 ⟶ 2,537:
}
}
Print(ob.Join(','))</
{{Out}}
<
6,7,8,9,10
ok</
=={{header|Swift}}==
<
print(i, terminator: "")
if i % 5 == 0 {
Line 2,236 ⟶ 2,552:
}
print(", ", terminator: "")
}</
{{Out}}
<pre>
Line 2,244 ⟶ 2,560:
=={{header|Tcl}}==
<
puts -nonewline $i
if {$i % 5 == 0} {
Line 2,251 ⟶ 2,567:
}
puts -nonewline ", "
}</
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
DECLARE @i INT = 0;
DECLARE @str VarChar(40) = '';
Line 2,270 ⟶ 2,586:
SET @str = @str +', ';
END;
</syntaxhighlight>
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
numbers=""
Line 2,283 ⟶ 2,599:
numbers=""
ENDLOOP
</syntaxhighlight>
{{Out}}
<pre>
Line 2,291 ⟶ 2,607:
=={{header|UNIX Shell}}==
<
while (( Z<=10 )); do
echo -e "$Z\c"
Line 2,300 ⟶ 2,616:
fi
(( Z++ ))
done</
{{works with|Bash}}
<
echo -n $i
if [ $((i%5)) -eq 0 ]; then
Line 2,310 ⟶ 2,626:
fi
echo -n ", "
done</
=={{header|UnixPipes}}==
<
=={{header|Ursa}}==
{{trans|Python}}
<
for (set i 1) (< i 11) (inc i)
if (= (mod i 5) 0)
Line 2,324 ⟶ 2,640:
end if
out i ", " console
end for</
=={{header|Vala}}==
<
stdout.printf("%d", i);
if (i % 5 == 0) {
Line 2,334 ⟶ 2,650:
}
stdout.printf(", ");
}</
=={{header|VBA}}==
<
Dim value As Integer
For value = 1 To 10
Line 2,348 ⟶ 2,664:
End If
Next value
End Sub</
=={{header|Vedit macro language}}==
<
Num_Type(#1, LEFT+NOCR)
if (#1 % 5 == 0) {
Line 2,358 ⟶ 2,674:
}
Message(", ")
}</
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
for i in 1..11 {
print(i)
if i%5==0{
println('')
continue
}
print(', ')
}
}</syntaxhighlight>
{{Out}}
<pre>
1, 2, 3, 4, 5
6, 7, 8, 9, 10
</pre>
=={{header|Wren}}==
From v0.4.0 Wren has a ''continue'' keyword which works in the expected fashion.
<
System.write(i)
if (i%5 == 0) {
Line 2,371 ⟶ 2,704:
}
System.print()</
{{out}}
Line 2,386 ⟶ 2,719:
The way you implement continue in X86 Assembly is the same way as how you would create a loop:
you just implement a (conditional) jump to another line of code.
<
extern _printf
Line 2,474 ⟶ 2,807:
pop ebx
ret
</syntaxhighlight>
{{Out}}
<pre>
Line 2,483 ⟶ 2,816:
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<
PROGRAM "loopcontinue"
Line 2,499 ⟶ 2,832:
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{Out}}
<pre>
Line 2,511 ⟶ 2,844:
Only the first three characters of a command are required.
<
integer N;
for N:= 1 to 10 do
[IntOut(0, N); if remainder(N/5) \#0\ then Text(0, ", ") else CrLf(0)]</
{{Out}}
Line 2,524 ⟶ 2,857:
=={{header|Yabasic}}==
<
print str$(i);
if mod(i, 5) = 0 then
Line 2,533 ⟶ 2,866:
next
print
end</
=={{header|zkl}}==
<
// or foreach n in ([1..10]){print(n,(n%5) and ", " or "\n")}</
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
var i: i8 = 1;
while (i <= 10) : (i += 1) {
try stdout_wr.print("{d}", .{i});
if (i == 5) {
try stdout_wr.writeAll("\n");
continue;
}
try stdout_wr.writeAll(", ");
}
}</syntaxhighlight>
|