In this example, I am going to use the while loop in Power Shell to add up two numbers. The command is as follows:-
$val1=1; $val2=2; while ($val1 -lt $val2) {$val1++; $val3 = $val1 + $val2; write-host "The sum is $val3"}
The while loop above will compare the first number with the second number and if the first number is less than the second number then the first number will get incremented by one and then both the numbers will get summed up and get displayed on the command screen. The loop will continue until the first number is greater or equal to the second number!
The outcome of the above program is as follows:-
The sum is 4
Notice that the third $var3 variable is used to keep the result of the addition!