• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
 
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 inthe 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 computerto leave the loop.
Read the sample code on p. 98.
Comparisons
The comparison operators used in simple conditions (also known as logical, Boolean, ortrue/false conditions) used in
exit when
statements are
=, <, > not.
 Simple conditions used in
exit when
statements involve comparing two things where thecomparison operator is one of the following six:
Operator Meaning
= equal to< less than> greater thannot= 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 twopossible
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 whya
)“A” < “B”
is true and
“a” < “B”
is false.
When strings are all letters of the same case, “A” < “B” means “A” comesbefore “B” alphabetically, which is true.When strings are not of the same case, the sequence used is not alphabetic; itis the collating sequence of the code for the characters, that is, their positionin the set of ASCII characters. Since the set of upper case letters comesbefore 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 isbased 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 endand 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 thename 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 itsappearance in a for statement
Range — The range of the index of a counted loop is given as follows:
thestarting value of the index, followed by two dots, followed by the finishingvalue of the index
.Scope —
The value of the index may be used in arithmetic statements, or itmay be output. But the index may not be used outside the loop, that is, theindex 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
. Thisis better because
if you want to modify the program, you only have to change theconstant
.
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:
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...