Loops/Continue: Difference between revisions
Content added Content deleted
(jq) |
m ({{Out}} / moved Category to top) |
||
Line 1:
[[Category:Loop modifiers]]▼
{{task|Iteration}}Show the following output using one loop. ▼
{{omit from|GUISS}}▼
{{omit from|M4}}▼
{{task|Iteration}}
1, 2, 3, 4, 5
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.
=={{header|Ada}}==
Ada doesn't have a continue statement,
so we have to use a goto statement.
In this example it is indeed not needed,
but that is not always the case.
An example is a loop where a number of interdependent conditions
are checked before executing the main body of the loop.
Without a continue statement (or goto), one ends up with
nested statements with the main body to the far right of the page.
'''B.N.''' You should always try to avoid using a goto,
Line 65 ⟶ 71:
))
OD</lang>
{{Out}}
<pre>
+1, +2, +3, +4, +5
Line 240 ⟶ 246:
=={{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.
<lang lisp>(do ((i 1 (1+ i))) ((> i 10))
(format t "~a~:[, ~;~%~]" i (zerop (mod i 5))))
Line 258 ⟶ 268:
(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.
<lang lisp>(loop for i from 1 to 10
Line 291 ⟶ 303:
=={{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.
{{trans|bc}}
Line 309 ⟶ 323:
]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.
=={{header|Delphi}}==
Line 329 ⟶ 345:
end.</lang>
{{Out}}
<pre>
1, 2, 3, 4, 5
Line 351 ⟶ 367:
===Direct Approach===
<lang ela>open console imperative
Line 364 ⟶ 379:
===Using list===
<lang ela>open console imperative
Line 439 ⟶ 453:
=={{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.
{{trans|Ada}}
<lang fsharp>for i in 1 .. 10 do
Line 603 ⟶ 618:
}
}</lang>
{{Out}}
<pre>
1, 2, 3, 4, 5
Line 620 ⟶ 635:
=={{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.
<lang haskell>import Control.Monad (forM)
Line 652 ⟶ 668:
=={{header|J}}==
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>
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
z=.''
Line 699 ⟶ 717:
=={{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:
<lang jq>reduce range(1;11) as $i
(""; . + "\($i)" + (if $i % 5 == 0 then "\n" else ", " end))</lang>
Line 744 ⟶ 763:
printf( ", " )
end do:</lang>
This can also be done as follows, but without the use of "next".
<lang Maple>for i to 10 do
Line 763 ⟶ 783:
=={{header|MATLAB}} / {{header|Octave}}==
Loops are considered slow in Matlab and Octave,
it is preferable to vectorize the code.
<lang Matlab>disp([1:5; 6:10])</lang>
or
Line 777 ⟶ 798:
end
end</lang>
=={{header|Maxima}}==
Line 808 ⟶ 828:
=={{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.
<lang metafont>string s; s := "";
Line 820 ⟶ 841:
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.
'''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 ⟶ 850:
=={{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]].
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.
Line 907 ⟶ 932:
=={{header|OCaml}}==
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
try
Line 920 ⟶ 946:
6, 7, 8, 9, 10
- : unit = ()</lang>
Though even if the continue statement does not exist,
it is possible to add it with camlp4.
=={{header|Octave}}==
Line 965 ⟶ 992:
}</lang>
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) {
Line 1,004 ⟶ 1,032:
=={{header|PicoLisp}}==
PicoLisp doesn't have an explicit 'continue' functionality.
It can always be emulated with a conditional expression.
<lang PicoLisp>(for I 10
(print I)
Line 1,090 ⟶ 1,118:
=={{header|Racket}}==
It is possible to skip loop iterations in Racket,
but an explicit <tt>continue</tt> construct is rarely used:
<lang racket>
Line 1,133 ⟶ 1,162:
]</lang>
{{Out}}
<pre>One liner (compare to ALGOL 68 solution):
1, 2, 3, 4, 5
Line 1,273 ⟶ 1,301:
Print(ob.Join(','))</lang>
{{Out}}
<lang Suneido>1,2,3,4,5
6,7,8,9,10
Line 1,287 ⟶ 1,315:
print(", ")
}</lang>
{{Out}}
<pre>
1, 2, 3, 4, 5
Line 1,332 ⟶ 1,360:
ENDLOOP
</lang>
{{Out}}
<pre>
1, 2, 3, 4, 5
Line 1,393 ⟶ 1,421:
[IntOut(0, N); if remainder(N/5) \#0\ then Text(0, ", ") else CrLf(0)]</lang>
{{Out}}
<pre>
1, 2, 3, 4, 5
Line 1,402 ⟶ 1,430:
<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>
▲{{omit from|GUISS}}
▲{{omit from|M4}}
▲[[Category:Loop modifiers]]
|