-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_Loops-Do.ps1
More file actions
42 lines (32 loc) · 1.17 KB
/
04_Loops-Do.ps1
File metadata and controls
42 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Basic Syntax:
# ----------------------------------------------------------------------------------------------------
# do {<statement list>} while (<condition>)
# do {<statement list>} until (<condition>)
# Do loops always run at least once because the condition is evaluated at the end of the loop.
# Do Until
# ----------------------------------------------------------------------------------------------------
# Do Until runs while the specified condition is false.
$number = Get-Random -Minimum 1 -Maximum 10
do {
$guess = Read-Host -Prompt "What's your guess?"
if ($guess -lt $number) {
Write-Output 'Too low!'
}
elseif ($guess -gt $number) {
Write-Output 'Too high!'
}
}
until ($guess -eq $number)
# Do While
# ----------------------------------------------------------------------------------------------------
# Do While is just the opposite. It runs as long as the specified condition evaluates to true.
$number = Get-Random -Minimum 1 -Maximum 10
do {
$guess = Read-Host -Prompt "What's your guess?"
if ($guess -lt $number) {
Write-Output 'Too low!'
} elseif ($guess -gt $number) {
Write-Output 'Too high!'
}
}
while ($guess -ne $number)