0
0
PowerShellscripting~10 mins

While and Do-While loops in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a while loop that prints numbers from 1 to 3.

PowerShell
$i = 1
while ($i [1] 4) {
    Write-Output $i
    $i++
}
Drag options to blanks, or click blank then click option'
A-lt
B-ne
C-eq
D-gt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -gt will make the loop never run because 1 is not greater than 4.
Using -eq will only run if $i equals 4, which is not the intended condition.
2fill in blank
medium

Complete the code to create a do-while loop that prints numbers from 1 to 3.

PowerShell
$i = 1
do {
    Write-Output $i
    $i++
} while ($i [1] 4)
Drag options to blanks, or click blank then click option'
A-eq
B-gt
C-ne
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -gt will cause the loop to never run because 1 is not greater than 4.
Using -eq will only run if $i equals 4, which is not the intended condition.
3fill in blank
hard

Fix the error in the while loop condition to count down from 5 to 1.

PowerShell
$i = 5
while ($i [1] 0) {
    Write-Output $i
    $i--
}
Drag options to blanks, or click blank then click option'
A-eq
B-gt
C-lt
D-ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt will cause the loop to never run because 5 is not less than 0.
Using -eq will only run if $i equals 0, which is not the intended condition.
4fill in blank
hard

Fill both blanks to create a while loop that prints even numbers from 2 to 10.

PowerShell
$i = 2
while ($i [1] 10) {
    Write-Output $i
    $i [2] 2
}
Drag options to blanks, or click blank then click option'
A-le
B+=
C-lt
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt excludes 10, so 10 won't be printed.
Using -= will subtract 2, causing an infinite loop.
5fill in blank
hard

Fill all three blanks to create a do-while loop that prints numbers from 10 down to 1.

PowerShell
$i = [1]
do {
    Write-Output $i
    $i [2] 1
} while ($i [3] 0)
Drag options to blanks, or click blank then click option'
A10
B-=
C-gt
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will not count down from 10.
Using += will increase $i causing an infinite loop.
Using -lt or -le in the condition will cause the loop to stop immediately.