0
0
PowerShellscripting~10 mins

Why control flow directs execution in PowerShell - Test Your Understanding

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 3 using a loop.

PowerShell
for ($i = 1; $i -le 3; $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 += which requires a value like 1, causing a syntax error.
Using -= or -- which decrease the value and cause an infinite loop.
2fill in blank
medium

Complete the code to check if a number is positive and print a message.

PowerShell
if ($number [1] 0) { Write-Output 'Positive number' }
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 -lt which checks for less than, not greater than.
Using -eq which checks for equality, not greater than.
3fill in blank
hard

Fix the error in the loop condition to stop at 5.

PowerShell
while ($count [1] 5) { Write-Output $count; $count += 1 }
Drag options to blanks, or click blank then click option'
A-gt
B-ge
C-le
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -le which includes 5 and may cause an extra loop.
Using -gt or -ge which reverse the logic.
4fill in blank
hard

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

PowerShell
for ($num = 2; $num [1] 10; $num [2] 2) { Write-Output $num }
Drag options to blanks, or click blank then click option'
A-le
B-lt
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt which excludes 10 from the loop.
Using -= which decreases the number and causes an infinite loop.
5fill in blank
hard

Fill all three blanks to create a dictionary of words and their lengths for words longer than 3 letters.

PowerShell
$lengths = @{}; foreach ([3] in $words) { if ([3].Length -gt 3) { $lengths[[1]] = [2] } }
Drag options to blanks, or click blank then click option'
A$word
B$word.Length
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and loop variable.
Using the word itself as value instead of its length.