0
0
PowerShellscripting~10 mins

If-elseif-else statements 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 check if a number is positive.

PowerShell
if ($number [1] 0) { Write-Output "Positive" }
Drag options to blanks, or click blank then click option'
A-gt
B-ne
C-eq
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-lt' which means less than, so it would check for negative numbers.
Using '-eq' which checks for equality, not greater than.
2fill in blank
medium

Complete the code to check if a number is zero.

PowerShell
if ($number -gt 0) { Write-Output "Positive" } elseif ($number [1] 0) { Write-Output "Zero" }
Drag options to blanks, or click blank then click option'
A-ne
B-lt
C-ge
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-lt' or '-ge' which check less than or greater or equal, not equality.
Using '-ne' which means not equal, the opposite of what is needed.
3fill in blank
hard

Fix the error in the else statement syntax.

PowerShell
if ($score -ge 90) { Write-Output "A" } elseif ($score -ge 80) { Write-Output "B" } else[1]Write-Output "F" }
Drag options to blanks, or click blank then click option'
Aelse {
Bthen {
C {
Delseif {
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 'then' after else, which is not valid in PowerShell.
Writing 'elseif' instead of 'else' for the final block.
4fill in blank
hard

Fill both blanks to complete the if-elseif structure checking temperature.

PowerShell
if ($temp [1] 30) { Write-Output "Hot" } elseif ($temp [2] 15) { Write-Output "Warm" } else { Write-Output "Cold" }
Drag options to blanks, or click blank then click option'
A-gt
B-lt
C-ge
D-le
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-ge' or '-le' which include equality and may change logic.
Swapping the operators and confusing hot and warm conditions.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as uppercase words and values as their lengths if length is greater than 3.

PowerShell
foreach ($word in $words) { if (($word.Length [1] 3) [2] $true)) { $dict[[3]] = $word.Length } }
Drag options to blanks, or click blank then click option'
A-gt
B-and
C$word.ToUpper()
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-lt' instead of '-gt' which reverses the condition.
Using '-or' instead of '-and' which changes logic.
Not converting the word to uppercase for the dictionary key.