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

m
imported>Joeypas
 
(6 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,152 ⟶ 4,173:
=={{header|Quackery}}==
 
===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 that exactly meets the specification of the task.
 
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 )
<syntaxhighlight lang="Quackery"> [ stack ] is f.incr action ( --> s )
[ stack ] is f.indexend ( --> s )
<syntaxhighlight lang="Quackery"> [ stack ] is f.actionincr ( --> s )
[ stack ] is f.end index ( --> s )
 
[ ' [ f.action f.end
f.incr f.index ] ] is f.stacks ( --> [ )
 
[ f.index share ] is index ( --> n )
 
[ 1+ f.incr replace ] is incr ( n --> )
 
[ true f.end replace ] is end ( b --> )
 
[ 1 false ]'[
Line 4,175 ⟶ 4,198:
1 incr
f.end share until ]
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>
f.stacks
witheach release ] is from ( n --> )
 
<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.
</syntaxhighlight>
 
<code>index</code> returns the current index.
<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is satisfied, incrementing the index at the end of each iteration.
 
The default increment is <code>1</code>, but this can be overridden for a single iteration by the word <code>incr</code> within the nest being performed by <code>from</code>. <code>incr</code> takes a number from the stack, and sets the next incrementationincrement to be that number ''plusfor 1''the current iteration. ThisIf may<code>incr</code> seemis counter-intuitive,performed within some iterations but thenot taskall, statesthe "''inincrement additionwill tobe the<code>1</code> normalfor incrementation''"those and,iterations hey,where a specificationit is anot specificationperformed.
<code>index</code> returns the current index.
 
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 default increment is <code>1</code>, but this can be overridden for a single iteration by the word <code>incr</code> within the nest being performed by <code>from</code>. <code>incr</code> takes a number from the stack, and sets the next incrementation to be that number ''plus 1''. This may seem counter-intuitive, but the task states "''in addition to the normal incrementation''" and, hey, a specification is a specification.
 
The word <code>end</code> sets the ending condition to <code>true</code>, so the loop will end at the end of the current iteration.
 
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,201 ⟶ 4,223:
dip [ char , join ]
again ]
join reverse echo$ ] is echo, ( n --> )
 
0
Line 4,210 ⟶ 4,232:
say ": "
index echo, cr
index 1+ incr ]
dup 42 = if end ]
drop</syntaxhighlight>
Line 4,258 ⟶ 4,280:
41: 49,752,014,150,467
42: 99,504,028,301,131</pre>
 
===Without indexed loop word===
 
<code>prime</code>is defined at [[Miller–Rabin primality test#Quackery]].
 
<code>echo,</code> is as above.
 
<syntaxhighlight lang="Quackery"> 0 42
[ dup prime if
[ dip 1+
over echo
say ": "
dup echo, cr
dup + ]
. 1+
over 42 = until ]
2drop
</syntaxhighlight>
 
{{out}}
 
Output is as above.
 
=={{header|R}}==
Line 5,174 ⟶ 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,981

edits