Lesson goal: The while-loop to average numbers

Previous: Type some words | Home | Next: Mouse clicks

As mentioned, while-loops can make a chunk of program code run over and over until the condition of the while-loop becomes false. Here's an example that asks you to type a number. As long as you type a positive number, it'll keep computing the average of your numbers. As soon as you type in a negative number, it'll stop and tell you the average.

The code won't quite work. What condition you put in the while ... do line that will keep it looping as long as $n \ge 0$?