Loops/Nested: Difference between revisions
m
→{{header|J}}: update vernacular and spell out some details
m (→{{header|J}}: update vernacular and spell out some details) |
|||
Line 1,806:
=={{header|J}}==
In J, using loops is usually a bad idea -- the underlying implementation implements a rich set of highly optimized special case loops. That said, general case loops can be used and can be a good choice when the operations provided by the primitives need to be severely pruned.
<lang J>use=: ({.~ # <. 1+i.&20)@:,</lang>
Here's how the problem could be solved, using loops:
<lang J>doubleLoop=:
for_row. i.#y do.
for_col. i.1{$y do.
if. 20=t do.''return.end.
end.
end.
{{out|Example use}}
<pre> use ?.20 20 $ 21
Line 1,835:
The first approach is probably a couple thousand times faster than the second.
(In real life, good problem definitions might typically involve "use cases" (which are specified in terms of the problem domain, instead in terms of irrelevant details). Of course, "Rosetta Code" is about how concepts would be expressed in different languages. However, even here, tasks which dwell on language-specific issues are probably not a good use of people's time.)
=={{header|Java}}==
|