Loops/While: Difference between revisions

m
Category:Simple, {{out}} / Category to top
m (Category:Simple, {{out}} / Category to top)
Line 1:
{{task|Iteration}} [[Category:Conditional loops]] [[Category:Simple]]
{{task|Iteration}}Start an integer value at 1024. Loop while it is greater than 0. Print the value (with a newline) and divide it by two each time through the loop.
{{omit from|GUISS|No loops and we cannot read values}}
Start an integer value at 1024. Loop while it is greater than 0.
{{task|Iteration}}Start an integer value at 1024. Loop while it is greater than 0. Print the value (with a newline) and divide it by two each time through the loop.
 
=={{header|0815}}==
Line 35 ⟶ 38:
END WHILE
</lang>
'''output'''{{out}} (+ sign indicates "problem state" (non system key) issued WTO's
<pre style="overflow:scroll">
+1024
Line 142 ⟶ 145:
 
=={{header|AppleScript}}==
AppleScript does not natively support a standard out. Use the Script Editor's Event Log as the output.
Use the Script Editor's Event Log as the output.
<lang AppleScript >set i to 1024
repeat while i > 0
Line 1,156 ⟶ 1,160:
 
=={{header|PostScript}}==
PostScript has no real <code>while</code> loop, but it can easily be created with an endless loop and a check at the beginning:
but it can easily be created with an endless loop and a check at the beginning:
<lang postscript>1024
{
Line 1,234 ⟶ 1,239:
(displayln n)
(loop (quotient n 2))))</lang>
 
===Macro===
<lang racket>#lang racket
Line 1,259 ⟶ 1,265:
end
/*stick a fork in it, we're done.*/</lang>
{{out}}
'''output'''
<pre>
1024
Line 1,286 ⟶ 1,292:
end
/*stick a fork in it, we're done.*/</lang>
{{out}}
'''output'''
<pre>
1024
Line 1,310 ⟶ 1,316:
/*stick a fork in it, we're done.*/</lang>
'''output''' is the same as version 2.
<br><br>
 
===version 4, index reduction===
Line 1,380 ⟶ 1,386:
 
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
===Imperative===
Line 1,388 ⟶ 1,393:
i /= 2
}</lang>
 
===Tail recursive===
<lang scala> @tailrec
Line 1,405 ⟶ 1,411:
}
loop.foreach(println(_))</lang>
 
===Stream===
Finite stream (1024..0) filtered by takeWhile (1024..1).
Line 1,596 ⟶ 1,603:
 
=={{header|Ursala}}==
Unbounded iteration is expressed with the -> operator. An expression
An expression (p-> f) x, where p is a predicate and f is a function, evaluates to x,
evaluates to x, f(x), or f(f(x)), etc. as far as necessary to falsify p.
 
Printing an intermediate result on each iteration is a bigger problem
because side effects are awkward. Instead, the function g in this
Instead, the function g in this example iteratively constructs a list of results, which is displayed
which is displayed on termination.
 
The argument to g is the unit list <1024>. The predicate p is ~&h,
The predicate p is ~&h, the function that tests whether
the function that tests whether the head of a list is non-null
the head of a list is non-null (equivalent to non-zero). The iterated function f is that which conses
The iterated function f is that which conses the
the truncated half of the head of its argument with a copy of the whole argument.
whole argument. The main program takes care of list reversal and formatting.
formatting.
<lang Ursala>#import nat
 
Line 1,728 ⟶ 1,735:
1
</pre>
 
{{omit from|GUISS|No loops and we cannot read values}}
 
[[Category:Conditional loops]]
Anonymous user