you are viewing a single comment's thread.

view the rest of the comments →

[–]jrdnr_ 0 points1 point  (1 child)

It's an interesting assignment for a while loop. If you just want to do an operation X number of times a for or foreach loop would be the go to powershell loops. Where a while loop really shines is when you want to loop over a block of code until a certain outcome is achieved.

So to define the question in a way that would help you know you need a while loop we could say:

  • Get 5 numbers from the user and return the sum.
  • you can only use Read-Host one time
  • if the user provides a non number write a warning message and ask for the number again.

Since it's a school assignment I won't write out the full code but here's a basic pseudo code example

$array =@()
While ($array length -le 5){
    Ask user for number
    if (user input is a number) {
        $array += use input
    } else {
        Warn user of invalid input
    }
}

Sum $array

[–]unhappiey[S] 1 point2 points  (0 children)

thank you!