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.
In J, using loops is usually a bad idea.
 
HereSo, here's how the problem statement (ignoring the "requirement" for loops) could be solved, without explicit loops (there's a conceptual nested loop and a short circuited search loop implemented within these primitives):
<lang J>use=: ({.~ # <. 1+i.&20)@:,</lang>
Here's how the problem could be solved, using loops:
<lang J>doubleLoop=:verb define{{
for_row. i.#y do.
for_col. i.1{$y do.
smoutputecho t=.(<row,col) { y
if. 20=t do.''return.end.
end.
end.
)}}</lang>
{{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}}==
6,951

edits