Lesson goal: The while-loop to guesss your number

Previous: You guess my number | Home | Next: Solving equations

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 of using a while loop: you think of a random number between 1 and 100. The computer will keep guessing WHILE it hasn't guessed your number correctly. Get the use of the "while?"

So go ahead, think of a number between 1 and 100. If you answer the questions truthfully, the computer will guess your number in 10 tries or less every time.

Now you try. Put a condition in the while ... do line to have the loop keep running as long as variable ans $\ne$ "y".

Type your code here:


See your results here: