Java Reference

In-Depth Information

(c)
x= 1; i= 2;

(d)
x= 2; y= 4; z= 1;

while
(i <= 7) {

while
(y != 0) {

x= x * i;

if
(y%2==0) {

i= i + 2;

x= x * x;

}

y= y / 2;

}
else
{

z= z * x;

y= y - 1;

}

}

Answers to self review exercises

SR1.
See beginning of Sec. 7.1.1.

SR2.
See Sec 7.1.1.

SR3.
(a)

x 1 1 2 6

i 1 2 3 4

(b)

x 1 3 6 6

i 3 2 1 0

(c)

x 1 2 8 48

i 2 4 6 8

(d)

x 2 4 16

y 4 2 1 0

z 1 16

7.2

Understanding and developing loops

Loops are far more complicated than assignments or if-statements, and, general-

ly speaking, learning how to develop loops is more difficult than learning about

assignments or if-statements. In this section, we go into detail about how to think

about loop development and how to annotate a loop with comments that help the

reader (and writer) understand it. We start with a discussion of some notation that

helps simplify discussions of some loops.

Style Note

13.2, 13.2.3

indenting

loops

A note on ranges h..k

We often want to say something about a range of integers, for example, the

integers
5
,
6
,
7
, and
8
. To simplify, we use the notation
5..8
to denote this range.

The notation
h..k
denotes the range of integers
h
,
h+1
,
h+2
, …,
k-1
,
k
.

For example, we might say, “the integers
5..8
have been printed”, or, “
x
is

Search WWH ::

Custom Search