Student Name
Chapter 6 Notes for the Turing textbook Repetition Loops Run Turing and try the sample code on page 96. A loop statement causes the same set of instruction to run repeatedly. loop is the keyword that starts a loop. end loop is the keyword that ends a loop. An infinite loop continues repeating forever. To stop the execution of a program containing an infinite loop click the Stop button in the Execution window or enter illegal input .
Conditional Loops A conditional loop is a loop that stops itself as soon as a particular condition holds.A sentinel is a signal that stops a loop. Always indicate what the sentinel is so the user knows how to gracefully exit the loop . Run Turing and try the sample code on page 97. exit when are the keywords that set the condition which, when true, cause the computer to leave the loop. Read the sample code on p. 98.
Comparisons The comparison operators used in simple conditions (also known as logical, Boolean, or true/false conditions) used in exit when statements are =, <, > not. Simple conditions used in exit when statements involve comparing two things where the comparison operator is one of the following six: Operator Meaning = equal to < less than > greater than not= not equal to <= less than or equal >= greater than or equal
Student Name
Chapter 6 Notes for the Turing textbook Repetition Simple conditions are also known as logical, Boolean, or true/false conditions. A simple comparison can be either true or false. That is, it can have only one of the two possible Boolean values true or false. Do not use the “=” comparison with real values because when stored in a computer, real numbers may not be completely accurate . Instead of “=” use “>=” or “>= “.
Comparing Strings In comparing strings, explain why a)“A” < “B” is true and “a” < “B” is false. When strings are all letters of the same case, “A” < “B” means “A” comes before “B” alphabetically, which is true. When strings are not of the same case, the sequence used is not alphabetic; it is the collating sequence of the code for the characters, that is, their position in the set of ASCII characters. Since the set of upper case letters comes before the set of lower case letters, “a” < “B” is false. b) “Bob” > “Bill” is true When the first characters of the two strings are the same, the comparison is based on the next two. c) “Stop” = “Stop “ is false Two strings are not equal to each other unless they are the same length. These two strings are not equal because the second has a blank at the end and is therefore five characters long.
d) “Stop” not = “Stop “ is true These two strings are not equal, so the statement “Stop” not= “Stop “ is true.
Run Turing and try the sample code on page 100. Read the sample code on page 101. Run Turing and try the sample code on page 102.
Student Name
Chapter 6 Notes for the Turing textbook Repetition Counted Loops Run Turing and try the sample code on page 103. List the 3 types of loops: infinite loop, conditional loop, and counted loop. A counted loop is a loop that repeats a fixed number of times . The keyword that starts a counted loop is for , which is immediately followed by the name of the index (also known as the counter ) for the loop. The index of a counted loop is a variable of type int with the following special properties: Declaration — Cannot be declared. Its declaration is implied by its appearance in a for statement Range — The range of the index of a counted loop is given as follows: the starting value of the index, followed by two dots, followed by the finishing value of the index. Scope — The value of the index may be used in arithmetic statements, or it may be output. But the index may not be used outside the loop, that is, the index is only available within the scope of the loop. Read the sample code on page 104. Rather than using a number in a for loop, it is better to declare and use a constant . This is better because if you want to modify the program, you only have to change the constant .
Read the sample code on page 105.
Counting in Multiples Read the sample code on page 105 in Section 6.3.1. The by clause is used when you want to increase the index by a number greater than one . An example of a loop that outputs even numbers from 2 to 20 using the by clause is:
Student Name
Chapter 6 Notes for the Turing textbook Repetition for count : 2 .. 10 by 2 put count end for Indenting the Body of Loops Paragraphing the program means indenting the body of a loop . It makes the program easier to understand because you can see the scope of the loop.
Loops that Count Backwards Read the sample code on pages 106-7. The keywords that begin a loop that counts backwards are for decreasing . An example of a loop that counts backwards from 5 to 1 is
for decreasing count : 1 .. 5 … body of loop … end for An example of a common error that might be committed in the above loop counting backwards from 5 to 1 is to reverse the order of the upper and lower limits of the loop.
Counted Loop Examples Read the sample code on pages 107-8.
Counted Loops with Exits
Student Name
Chapter 6 Notes for the Turing textbook Repetition It is often useful to be able to stop a counted loop. This is done with an exit when . Run Turing and try the sample code on page 109.
Random Exit from Loop Turing has two predefined procedures for generating random numbers. the procedure randint generates a random integer. the procedure rand generates a random real number.
random real numbers If a variable named number has been declared as real, the statement rand (number) will give number a value randomly chosen between 0 and 1 exclusive. (By “exclusive” we mean not including 0 or 1 .) Run Turing and try the sample code on page 110.
random integers If a variable named count has been declared as an integer, the statement that assigns a random value to count is
randint (count, 1, 10) Run Turing and try the sample code on page 110.
Compound Conditions Review the description of simple condition (above).
Student Name
Chapter 6 Notes for the Turing textbook Repetition A compound condition is formed by combining simple conditions using the Boolean operators called and and or. When two simple conditions are combined using and, the compound condition is true when both conditions are true . When two simple conditions are combined using or, the compound condition is true when either one (or both) of the simple conditions are true . A third Boolean operator is not . It does not connect two conditions; it acts on only one. e.g. not A > B which is the same as not (A > B) is true when the value of A is less than or equal to the value of B. Usually we avoid the not operator with combinations of “<”, “>”, and “=”. For example, not A > B can be written A <= B .
You are now ready to work on the exercises on pages 112 – 115.