<< Chapter < Page | Chapter >> Page > |
Exercise Is
for (;;;)
legal? If so, what does it mean?
Exercise Modify the program so that the square root is computed only once.
Concept The
break
statement is used to
exit a loop
from an arbitrary location in its body; the
continue
statement is used
to
skip the rest of a loop body and return to evaluate the condition
for continuing the loop.
Program: Control08.java
// Learning Object Control08
// continue statementspublic class Control08 {
static final int N = 10; public static void main(/*String[] args*/) { int sum = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) { if (i % 3 == 0)
continue; else
sum = sum + i; }
else if (i % 3 == 0) sum = sum + i;
else continue;
} System.out.println(sum);
}}
This program sums all the positive integers less than
N
that are
divisible by 2 or 3 but not by both. For
N=10
, the result
is
.
N
and the variable
sum
are allocated and initialized.for
loop is standard and is executed for the values 0 through
.i
is divisible by 2 and also by 3 (for example, 6), the
continue
statement is executed and the variable
sum
is not modified.i
is divisible neither by 2 nor by 3 (for example, 5), the
continue
statement is executed and the variable
sum
is not modified.i
is added to
sum
.sum
is printed.Exercise Modify the program so that it explicitly checks for divisibility by 6, instead of checking for divisibility by 2 and 3 inseparate statements.
Exercise Modify the program so that
continue
is not used.
Concept A
switch
statement is a generalization of an
if
statement.
Instead of selecting between two alternatives depending on the valueof a boolean-valued expression, an integer-valued expression is used and there can be
multiple alternatives introduced by the keyword
case
.
Since there are a very large number ofinteger values, an alternative labeled
default
is executed when the
value in the expression is not explicitly listed in one of the alternatives.
Important: In an
if
-statement, the end of the statement (or block of statements)
of the first alternative causes a transfer of controlto the end of the
if
statement, skipping over the statement (or block
of statements) in the second (
else
) alternative. This
does not happen in
a
switch
: control “drops through” from the end of one alternative to the beginning
of the next alternative.A
break
statement must be used to transfer control from the end of an alternative
to the end of the
switch
statement.
Program: Control09.java
// Learning Object Control09
// switch statementspublic class Control09 {
public static void main(/*String[] args*/) {
int year = 2001; int month = 4;
int days; switch (month) {
case 2: days = (year % 4 == 0) ? 28 : 29;
break; case 4:
case 6: case 9:
case 11: days = 30;
default: days = 31;
} System.out.println(days);
}}
This program computes the number of days in a month.
year
and
month
, are
given initial values.case
depending on the value of the variable
month
. Jeliot displays
Entering a switch statement
.case
associated with
4
is selected.
Jeliot displays
This case is selected
.
The assignment statement assigns 30 to
days
.days
.Exiting a switch statement
.days
is printed.Exercise Explain why the second assignment statement is executed; fix the program.
Exercise Explain why the sequence of
case
's for
works.
Exercise Modify the program so that the
case
's for the 31-day months
are given explicitly and so that the days are computed correctly in leap years.
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?