Programming Fundamentals/Loops/PowerShell
Appearance
loops.ps1
[edit | edit source]# This program demonstrates While, Do, and For loop counting using
# user-designated start, stop, and increment values.
#
# References:
# https://en.wikiversity.org/wiki/PowerShell/Loops
function Get-Value($name)
{
Write-Host $('Enter ' + $name + ' value:')
[int] $value = Read-Host
return $value
}
function While-Loop($start, $stop, $increment)
{
Write-Host $('While loop counting from ' + $start + ' to ' +
$stop + ' by ' + $increment + ':')
$count = $start
While ($count -le $stop) {
Write-Host $count
$count = $count + $increment
}
}
function Do-Loop($start, $stop, $increment)
{
Write-Host $('Do loop counting from ' + $start + ' to ' +
$stop + ' by ' + $increment + ':')
$count = $start
Do {
Write-Host $count
$count = $count + $increment
} While ($count -le $stop);
}
function For-Loop($start, $stop, $increment)
{
Write-Host $('For loop counting from ' + $start + ' to ' +
$stop + ' by ' + $increment + ':')
For ($count = $start; $count -le $stop; $count += $increment) {
Write-Host $count
}
}
function Main()
{
$start = Get-Value 'starting'
$stop = Get-Value 'ending'
$increment = Get-Value 'increment'
While-Loop $start $stop $increment
Do-Loop $start $stop $increment
For-Loop $start $stop $increment
}
Main
Try It
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own PowerShell compiler / interpreter / IDE.
Online
[edit | edit source]- There are some options, but none are fully functional at this time.