0
0
PowerShellscripting~10 mins

Arithmetic operators in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic operators
Start
Input numbers
Choose operator: +, -, *, /, %
Perform calculation
Show result
End
This flow shows how arithmetic operators take numbers, perform the chosen operation, and produce a result.
Execution Sample
PowerShell
$a = 10
$b = 3
$sum = $a + $b
$diff = $a - $b
$prod = $a * $b
$quot = $a / $b
$mod = $a % $b
Write-Output "$sum, $diff, $prod, $quot, $mod"
This script calculates sum, difference, product, quotient, and remainder of two numbers and prints them.
Execution Table
StepVariableOperationCalculationResult
1$aAssign1010
2$bAssign33
3$sumAddition10 + 313
4$diffSubtraction10 - 37
5$prodMultiplication10 * 330
6$quotDivision10 / 33.33333333333333
7$modModulo10 % 31
8OutputWrite-Output"13, 7, 30, 3.33333333333333, 1"13, 7, 30, 3.33333333333333, 1
💡 All arithmetic operations completed and results output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
$aundefined1010101010101010
$bundefinedundefined3333333
$sumundefinedundefinedundefined131313131313
$diffundefinedundefinedundefinedundefined77777
$produndefinedundefinedundefinedundefinedundefined30303030
$quotundefinedundefinedundefinedundefinedundefinedundefined3.333333333333333.333333333333333.33333333333333
$modundefinedundefinedundefinedundefinedundefinedundefinedundefined11
Key Moments - 3 Insights
Why does division result in a decimal number instead of an integer?
In step 6 of the execution_table, division of 10 by 3 results in 3.3333 because PowerShell performs floating-point division by default.
What does the modulo operator (%) do?
As shown in step 7, modulo returns the remainder after division. Here, 10 % 3 equals 1 because 3 fits into 10 three times with remainder 1.
Are the original variables $a and $b changed by the operations?
No, as seen in variable_tracker, $a and $b keep their original values throughout all steps; operations create new variables.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the value of $diff?
A7
B13
C3
D1
💡 Hint
Check the 'Result' column for step 4 in the execution_table.
At which step does the modulo operation happen?
AStep 5
BStep 7
CStep 6
DStep 8
💡 Hint
Look for the '%' operator in the 'Operation' column in the execution_table.
If $a was 12 instead of 10, what would $mod be at step 7?
A3
B1
C0
D4
💡 Hint
Calculate 12 % 3 and compare with the modulo result in variable_tracker.
Concept Snapshot
Arithmetic operators in PowerShell:
- Use +, -, *, /, % for add, subtract, multiply, divide, modulo
- Division returns decimal if needed
- Modulo gives remainder
- Variables keep original values unless reassigned
- Use Write-Output to display results
Full Transcript
This lesson shows how PowerShell uses arithmetic operators to calculate with numbers. We start by assigning values to variables $a and $b. Then we add, subtract, multiply, divide, and find the remainder using +, -, *, /, and % operators. Each operation stores its result in a new variable. The division operator returns a decimal number when the division is not exact. The modulo operator returns the remainder after division. The original variables $a and $b remain unchanged. Finally, the results are printed using Write-Output. This step-by-step trace helps beginners see how each variable changes and how the operations work.