0
0
PowerShellscripting~10 mins

For loop 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 print numbers from 1 to 5 using a for loop.

PowerShell
for ($i = 1; $i -le 5; $i[1]) { Write-Output $i }
Drag options to blanks, or click blank then click option'
A++
B--
C-=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- decreases the counter, causing an infinite loop.
Using += without a number is incomplete syntax.
2fill in blank
medium

Complete the code to loop through numbers 0 to 4 and print each.

PowerShell
for ($num = 0; $num [1] 5; $num++) { Write-Output $num }
Drag options to blanks, or click blank then click option'
A-ne
B-gt
C-eq
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-gt' would never run the loop because 0 is not greater than 5.
Using '-eq' would only run when $num equals 5, which is not the loop condition.
3fill in blank
hard

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

PowerShell
for ($i = 5; $i -ge 1; $i[1]) { Write-Output $i }
Drag options to blanks, or click blank then click option'
A++
B--
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ increases the counter, causing an infinite loop.
Using -= without a number is incomplete syntax.
4fill in blank
hard

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

PowerShell
for ($i = [1]; $i [2] 10; $i += 2) { Write-Output $i }
Drag options to blanks, or click blank then click option'
A2
B-lt
C-le
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 would include zero, which is not an even number in this context.
Using '-lt' excludes 10 from the output.
5fill in blank
hard

Fill all three blanks to create a loop that prints squares of numbers from 1 to 5.

PowerShell
for ($num = [1]; $num [2] 5; $num [3]) { Write-Output ($num * $num) }
Drag options to blanks, or click blank then click option'
A1
B-le
C++
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 would print 0 squared, which may not be desired.
Using '-lt' excludes 5 from the output.