Next:
Summary
Up:
All Units
Unit 6
Loop statements
Summary
Statements in Java
Repetition of statements
Definite and indefinite loops
The
while
loop
Use of a
while
loop for input
Example of
while
loop: product through repeated sums
Example of
while
loop: division trough repeated subtraction
Example of
while
loop: power through repeated multiplication
Example of
while
loop: counting the occurrences of a character in a string
Characteristic elements in the design of a loop
Common errors in writing
while
loops
Loop schemes
Loop scheme for a counter: number of strings in input
Loop scheme for a counter: number of positive integers
Loop scheme for an accumulator: sum of integers
Loop scheme for an accumulator: product of integers
Loop scheme for an accumulator: concatenation of strings
Loop scheme for characteristic values in a set: maximum with a known interval
Loop scheme for characteristic values in a set: maximum of a non-empty set
Loop scheme for characteristic values in a set: maximum in the general case
Other loop statements
Loop controlled by a counter
The
for
loop
Observations on the
for
loop
Examples of
for
loops
Example of
for
loop: encoding a string
Example of
for
loop: ``cracking'' an encoded string
The
do
loop
Observations on the
do
loop
Example of a
do
loop: input validation
Equivalence between
while
loop and
do
loop
Complete set of control statements
Example: computing the greatest common divisor (GCD)
GCD: by directly exploiting the definition
GCD: problems of the algorithm implementing directly the definition
GCD: using the method by Euclid
GCD: admissible values for the arguments
GCD: using the method by Euclid with rests
Example: length of the longest subsequence
Nested loops
Example of nested loop: print out a pyramid of stars
Example: power by means of a nested loop
Example: power by means of two methods
Flow control statements
Use of the
break
statement to exit a loop
Elimination of
break
Example of elimination of a
break
The
continue
statement (optional)
Statements to exit from blocks and labels for statements (optional)
Exercise: a class to encode texts
The class
EncryptedText
: representation of the objects
The class
EncryptedText
: public interface
The class
EncryptedText
: realization of the methods
The class
EncryptedText
: example of a client
The class
StringTokenizer
Exercises
Next:
Summary
Up:
All Units