Loops/Continue: Difference between revisions

Content added Content deleted
(jq)
m ({{Out}} / moved Category to top)
Line 1: Line 1:
[[Category:Loop modifiers]]
{{task|Iteration}}Show the following output using one loop.
{{omit from|GUISS}}
{{omit from|M4}}
{{task|Iteration}}
Show the following output using one loop.
1, 2, 3, 4, 5
1, 2, 3, 4, 5
6, 7, 8, 9, 10
6, 7, 8, 9, 10


Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it.
Try to achieve the result by forcing the next iteration within the loop
upon a specific condition, if your language allows it.


=={{header|Ada}}==
=={{header|Ada}}==
Ada doesn't have a continue statement, so we have to use
Ada doesn't have a continue statement,
so we have to use a goto statement.
a goto statement. The previous submitter said continue
is not needed. In this example it is indeed not needed,
The previous submitter said continue is not needed.
In this example it is indeed not needed,
but that is not always the case. An example is a loop
but that is not always the case.
where a number of interdependent conditions are checked
An example is a loop where a number of interdependent conditions
before executing the main body of the loop. Without a
are checked before executing the main body of the loop.
continue statement (or goto), one ends up with nested
Without a continue statement (or goto), one ends up with
statements with the main body to the far right of the page.
nested statements with the main body to the far right of the page.


'''B.N.''' You should always try to avoid using a goto,
'''B.N.''' You should always try to avoid using a goto,
Line 65: Line 71:
))
))
OD</lang>
OD</lang>
{{Out}}
Output:
<pre>
<pre>
+1, +2, +3, +4, +5
+1, +2, +3, +4, +5
Line 240: Line 246:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Common Lisp doesn't have a continue keyword, but the <code>do</code> iteration construct does use an implicit <code>tagbody</code>, so it's easy to <code>go</code> to any label. Four solutions follow. The first pushes the conditional (whether to print a comma and a space or a newline) into the format string. 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.
Common Lisp doesn't have a continue keyword, but the <code>do</code> iteration construct does use an implicit <code>tagbody</code>, so it's easy to <code>go</code> to any label.
Four solutions follow.
The first pushes the conditional (whether to print a comma and a space or a newline) into the format string.
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.
<lang lisp>(do ((i 1 (1+ i))) ((> i 10))
<lang lisp>(do ((i 1 (1+ i))) ((> i 10))
(format t "~a~:[, ~;~%~]" i (zerop (mod i 5))))
(format t "~a~:[, ~;~%~]" i (zerop (mod i 5))))
Line 258: Line 268:
(write-string ", ")))</lang>
(write-string ", ")))</lang>


These use the <code>loop</code> iteration form, which does not contain an implicit tagbody (though one could be explicitly included). The first uses an explicit condition to omit the rest of the loop; 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.
These use the <code>loop</code> iteration form, which does not contain an implicit tagbody (though one could be explicitly included).
The first uses an explicit condition to omit the rest of the loop;
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.


<lang lisp>(loop for i from 1 to 10
<lang lisp>(loop for i from 1 to 10
Line 291: Line 303:


=={{header|dc}}==
=={{header|dc}}==
The four commands <tt># n J M</tt> are special to [[OpenBSD dc]]. The <tt>#</tt> command starts a comment. The <tt>n</tt> command prints a number without a newline.
The four commands <tt># n J M</tt> are special to [[OpenBSD dc]].
The <tt>#</tt> command starts a comment.
The <tt>n</tt> command prints a number without a newline.


{{trans|bc}}
{{trans|bc}}
Line 309: Line 323:
]sC li 10!<C # enter loop if 10 >= i</lang>
]sC li 10!<C # enter loop if 10 >= i</lang>


This program uses <tt>J</tt> and <tt>M</tt> to force the next iteration of a loop. The <tt>''n''J</tt> command breaks ''n'' levels of brackets (like <tt>''n''Q</tt> does so), but then skips to the next <tt>M</tt> command. One can place <tt>M</tt> at the end of the iteration.
This program uses <tt>J</tt> and <tt>M</tt> to force the next iteration of a loop.
The <tt>''n''J</tt> command breaks ''n'' levels of brackets (like <tt>''n''Q</tt> does so), but then skips to the next <tt>M</tt> command.
One can place <tt>M</tt> at the end of the iteration.


=={{header|Delphi}}==
=={{header|Delphi}}==
Line 329: Line 345:
end.</lang>
end.</lang>


{{Out}}
Output:
<pre>
<pre>
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 351: Line 367:


===Direct Approach===
===Direct Approach===

<lang ela>open console imperative
<lang ela>open console imperative
Line 364: Line 379:


===Using list===
===Using list===

<lang ela>open console imperative
<lang ela>open console imperative
Line 439: Line 453:


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<code>continue</code> is a reserved word, but it has no function. In any case, it is not needed to complete this task.
<code>continue</code> is a reserved word, but it has no function.
In any case, it is not needed to complete this task.
{{trans|Ada}}
{{trans|Ada}}
<lang fsharp>for i in 1 .. 10 do
<lang fsharp>for i in 1 .. 10 do
Line 603: Line 618:
}
}
}</lang>
}</lang>
{{Out}}
Output:
<pre>
<pre>
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 620: Line 635:


=={{header|Haskell}}==
=={{header|Haskell}}==
As a functional language, it is not idiomatic to have true loops - recursion is used instead. Below is one of many possible implementations of the task. 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.
As a functional language, it is not idiomatic to have true loops - recursion is used instead. Below is one of many possible implementations of the task.
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.


<lang haskell>import Control.Monad (forM)
<lang haskell>import Control.Monad (forM)
Line 652: Line 668:


=={{header|J}}==
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
J is array-oriented, so there is very little need for loops.
For example, one could satisfy this task this way:


<lang j>_2}."1'lq<, >'8!:2>:i.2 5</lang>
<lang j>_2}."1'lq<, >'8!:2>:i.2 5</lang>


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).
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).
<lang j>3 : 0 ] 10
<lang j>3 : 0 ] 10
z=.''
z=.''
Line 699: Line 717:


=={{header|jq}}==
=={{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:
jq does not have a "continue" statement.
In jq 1.4, the simplest way to accomplish the given task is probably as follows:
<lang jq>reduce range(1;11) as $i
<lang jq>reduce range(1;11) as $i
(""; . + "\($i)" + (if $i % 5 == 0 then "\n" else ", " end))</lang>
(""; . + "\($i)" + (if $i % 5 == 0 then "\n" else ", " end))</lang>
Line 744: Line 763:
printf( ", " )
printf( ", " )
end do:</lang>
end do:</lang>

This can also be done as follows, but without the use of "next".
This can also be done as follows, but without the use of "next".
<lang Maple>for i to 10 do
<lang Maple>for i to 10 do
Line 763: Line 783:
=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==


Loops are considered slow in Matlab and Octave, it is preferable to vectorize the code.
Loops are considered slow in Matlab and Octave,
it is preferable to vectorize the code.
<lang Matlab>disp([1:5; 6:10])</lang>
<lang Matlab>disp([1:5; 6:10])</lang>
or
or
Line 777: Line 798:
end
end
end</lang>
end</lang>



=={{header|Maxima}}==
=={{header|Maxima}}==
Line 808: Line 828:


=={{header|Metafont}}==
=={{header|Metafont}}==
Metafont has no a <tt>continue</tt> (or similar) keyword. As the [[Loop/Continue#Ada|Ada solution]], we can complete the task just with conditional.
Metafont has no a <tt>continue</tt> (or similar) keyword.
As the [[Loop/Continue#Ada|Ada solution]], we can complete the task just with conditional.


<lang metafont>string s; s := "";
<lang metafont>string s; s := "";
Line 820: Line 841:
end</lang>
end</lang>


Since <tt>message</tt> append always a newline at the end, we need to build a string and output it at the end, instead of writing the output step by step.
Since <tt>message</tt> append always a newline at the end,
we need to build a string and output it at the end,
instead of writing the output step by step.


'''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
'''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
Line 827: Line 850:


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Modula-3 defines the keyword <tt>RETURN</tt> as an exception, but when it is used with no arguments it works just like <tt>continue</tt> in [[C]].
Modula-3 defines the keyword <tt>RETURN</tt> as an exception,
but when it is used with no arguments it works just like <tt>continue</tt> in [[C]].


Note, however, that <tt>RETURN</tt> only works inside a procedure or a function procedure; use <tt>EXIT</tt> otherwise.
Note, however, that <tt>RETURN</tt> only works inside a procedure or
a function procedure; use <tt>EXIT</tt> otherwise.


Module code and imports are omitted.
Module code and imports are omitted.
Line 907: Line 932:


=={{header|OCaml}}==
=={{header|OCaml}}==
There is no continue statement for for loops in OCaml, but it is possible to achieve the same effect with an exception.
There is no continue statement for for loops in OCaml,
but it is possible to achieve the same effect with an exception.
<lang ocaml># for i = 1 to 10 do
<lang ocaml># for i = 1 to 10 do
try
try
Line 920: Line 946:
6, 7, 8, 9, 10
6, 7, 8, 9, 10
- : unit = ()</lang>
- : unit = ()</lang>
Though even if the continue statement does not exist, it is possible to add it with camlp4.
Though even if the continue statement does not exist,
it is possible to add it with camlp4.


=={{header|Octave}}==
=={{header|Octave}}==
Line 965: Line 992:
}</lang>
}</lang>


It is also possible to use a goto statement to jump over the iterative code section for a particular loop:
It is also possible to use a goto statement
to jump over the iterative code section for a particular loop:


<lang perl>foreach (1..10) {
<lang perl>foreach (1..10) {
Line 1,004: Line 1,032:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
PicoLisp doesn't have an explicit 'continue' functionality. It can always be
PicoLisp doesn't have an explicit 'continue' functionality.
emulated with a conditional expression.
It can always be emulated with a conditional expression.
<lang PicoLisp>(for I 10
<lang PicoLisp>(for I 10
(print I)
(print I)
Line 1,090: Line 1,118:
=={{header|Racket}}==
=={{header|Racket}}==


It is possible to skip loop iterations in Racket, but an explicit <tt>continue</tt> construct is rarely used:
It is possible to skip loop iterations in Racket,
but an explicit <tt>continue</tt> construct is rarely used:


<lang racket>
<lang racket>
Line 1,133: Line 1,162:
]</lang>
]</lang>


{{Out}}
Output:

<pre>One liner (compare to ALGOL 68 solution):
<pre>One liner (compare to ALGOL 68 solution):
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 1,273: Line 1,301:
Print(ob.Join(','))</lang>
Print(ob.Join(','))</lang>


{{Out}}
Output:
<lang Suneido>1,2,3,4,5
<lang Suneido>1,2,3,4,5
6,7,8,9,10
6,7,8,9,10
Line 1,287: Line 1,315:
print(", ")
print(", ")
}</lang>
}</lang>
{{Out}}
Output:
<pre>
<pre>
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 1,332: Line 1,360:
ENDLOOP
ENDLOOP
</lang>
</lang>
{{Out}}
Output:
<pre>
<pre>
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 1,393: Line 1,421:
[IntOut(0, N); if remainder(N/5) \#0\ then Text(0, ", ") else CrLf(0)]</lang>
[IntOut(0, N); if remainder(N/5) \#0\ then Text(0, ", ") else CrLf(0)]</lang>


{{Out}}
Output:
<pre>
<pre>
1, 2, 3, 4, 5
1, 2, 3, 4, 5
Line 1,402: Line 1,430:
<lang zkl>foreach n in ([1..10]){print(n); if(n%5==0){println(); continue;} print(", ")}
<lang zkl>foreach n in ([1..10]){print(n); if(n%5==0){println(); continue;} print(", ")}
// or foreach n in ([1..10]){print(n,(n%5) and ", " or "\n")}</lang>
// or foreach n in ([1..10]){print(n,(n%5) and ", " or "\n")}</lang>


{{omit from|GUISS}}
{{omit from|M4}}

[[Category:Loop modifiers]]