Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- decreases the counter, causing an infinite loop.
Using += without a number is incomplete syntax.
✗ Incorrect
In PowerShell, to increase the loop counter by 1, use ++ after the variable.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The '-lt' operator means 'less than', so the loop runs while $num is less than 5.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ increases the counter, causing an infinite loop.
Using -= without a number is incomplete syntax.
✗ Incorrect
To count down, decrease the counter by 1 each time using --.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Start at 2 and loop while $i is less than or equal to 10 to include 10.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Start at 1, loop while less than or equal to 5, and increment by 1 each time.