Loops/Increment loop index within loop body: Difference between revisions

m
m (→‎With indexed loop word: Change of heart re behaviours of incr and end)
 
(4 intermediate revisions by 3 users not shown)
Line 1,907:
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=easylang>
fastfunc isprim num .
if num mod 2i = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 21
.
return 1
Line 1,944 ⟶ 1,941:
n=42 99504028301131
</pre>
 
=={{header|EMal}}==
{{trans|Java}}
</syntaxhighlight lang="emal">
int LIMIT = 42
fun isPrime = logic by int n
if n % 2 == 0 do return n == 2 end
if n % 3 == 0 do return n == 3 end
int id = 35
while d * d <= n
if n % d == 0 do return false end
d += 2
if n % d == 0 do return false end
d += 4
end
return 0true
end
for int i = LIMIT, int n = 0; n < LIMIT; ++i
if not isPrime(i) do continue end
++n
writeLine("n = " + n + ",\ti = " + i)
i += i - 1
end
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 4,154 ⟶ 4,175:
===With indexed loop word===
 
Quackery has an iterative looping word that can change its step size mid-iteration (see [[Loops/For with a specified step#Quackery]]) but it is not well suited to this task. A better solution (i.e. more idiomatic) is to define a new looping word, <code>from</code>, that exactly meets the specification of the task.
 
<syntaxhighlight lang="Quackery"> [ stack ] is f.action ( --> s )
[ stack ] is f.end ( --> s )
[ stack ] is f.incr ( --> s )
[ stack ] is f.index ( --> s )
 
[ ' [ f.action f.end
f.incr f.index ] ] is f.stacks ( --> [ )
 
[ f.index share ] is index ( --> n )
 
[ f.incr replace ] is incr ( n --> )
 
[ true f.end replace ] is end ( b --> )
 
[ 1 false ]'[
Line 4,177 ⟶ 4,198:
1 incr
f.end share until ]
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>
f.stacks
witheach release ] is from ( n --> )
 
</syntaxhighlight>
 
<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is satisfiedset to <code>true</code>, incrementing the index at the end of each iteration.
 
<code>index</code> returns the current index.
Line 4,190 ⟶ 4,208:
The task states "''in addition to the normal incrementation''" but this is counter-intuitive. To make the loop task compliant you will need to precede <code>f.incr</code> with <code>1+</code> in <code>incr</code>. You will also need to delete the <code>1+</code> before <code>incr</code> in the task code below.
 
The word <code>end</code> sets the ending condition to the boolean it takes from the stack<code>true</code>, so the loop will end at the end of the current iteration if the boolean is <code>true</code>.
 
As with other iterative looping words in Quackery (e.g. <code>times</code>, <code>witheach</code>, etc.) the word <code>done</code> will terminate the current iteration immediately.
Line 4,205 ⟶ 4,223:
dip [ char , join ]
again ]
join reverse echo$ ] is echo, ( n --> )
 
0
Line 4,215 ⟶ 4,233:
index echo, cr
index 1+ incr ]
dup 42 = if end ]
drop</syntaxhighlight>
 
Line 5,200 ⟶ 5,218:
{{libheader|Wren-fmt}}
Although it might appear as though one can change the index variable within a ''for'' loop, it does in fact change a local copy of the variable and the iteration is not affected at all. Consequently, the only way to complete this task in Wren is to use a ''while'' loop.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isPrime = Fn.new { |n|
1,983

edits