Lesson+4

LESSON 4
//**Repetitive structures: Counted loops**// Counted loops, sometimes called iterative loops, have the exit condition specified within the loop statement itself. they are called counted loops because they count how many times they have repeated until the exit condition. Try this:

**var count, square: int ** ** for count: 1..5 square:= count **2 put "The square of ", count, "is ", square, "." end for

You should have received an error message. This is due to the fact that the above program declares the variable **count** twice. Note: in Turing, a **counted loop automatically declares a variable!** in this case count. The syntax of a counted loop is **//for [variable name]: first .. last// //the stuff you want repeated goes here// //end for// **as with infinite and conditional loops the counted loop's for statement must be accompanied by an end for statement.


 * //Task:// **

1) Rewrite Lesson 3 using a counted loop 2) Write a program which generates a random number between 1 and 50. Prompt the user to guess the number. Continue prompting until the number is guessed or 15 tries have been reached. 3) Modify 2) such that the user specifies the range from which the random number is chosen and the number of guesses he or she is to receive.


 * You will need //randomize// and //randint// in order to complete this assignment. **