Loops/While: Difference between revisions

Content added Content deleted
(added PostScript)
(→‎{{header|PowerShell}}: slightly less confusing example)
Line 653: Line 653:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>$i = 1024
<lang powershell>[int]$i = 1024
while ($i -gt 0) {
while ($i -gt 0) {
$i
$i
$i /= 2
$i /= 2
}</lang>
Since PowerShell automatically converts variables to other types to accommodate for operations the above loop does not stop at 1 like it would in other languages but loops for quite a while until the value is small enough to be considered 0. An explicit cast corrects this:
<lang powershell>$i = 1024
while ($i -gt 0) {
$i
[int]$i /= 2
}</lang>
}</lang>