Evaluates one expression and compares it to different values; executes statements corresponding to the first value that the expression equals.
break statementdefault caseswitch (expr) { // int or char expr
case value1: // int or char values
statement;
...
break;
case value2: // int or char values
statement;
...
break;
default:
statement;
...
[break;] // optional in default
}
int x = ...;
...
switch (x + 2) {
case 4:
System.out.println("x is 2");
break;
case 5:
System.out.println("x is 3");
break;
default:
System.out.println("x is neither 2 nor 3");
break; // optional
}
Paraphrased as an 'if' statement:
int x = ...;
...
if (x + 2 == 4)
System.out.println("x is 2");
else if (x + 2 == 5)
System.out.println("x is 3");
else
System.out.println("x is neither 2 nor 3");
if ... else if ...; but it is sometimes more convenient.if statement is more general than switch, because it can use comparisons other than equality, and types other than char and int.break statements?default case?break statement at the end of the default case?An if-like expression:
condition ? valueIfTrue : valueIfFalse
// with if // with conditional operator
if (y > 10) x = y > 10 ? 1 : 2;
x = 1;
else
x = 2;
while statement, but with the test for expr occuring at the end rather than at the beginning.Syntax:
do
statement
while (expr);
double x = 12.0;
do {
System.out.println(x);
x /= 2.0;
}
while (x > 3.5)
What if x is initialized as 3.1?
do statement to print out the decimal digits of an integer, in reverse order.% and / to extract the digits.Simple examples:
for (int i = 0; i < 5; i++)
System.out.println(i);
for (int i = 10; i > 0; i--)
System.out.println(i);
Syntax:
for (init; test; update)
statement
Semantics: equivalent to
init;
while (test) {
statement;
update;
}
If it does, the scope of the variable is only within the (block of the) for loop.
for (int i; ...; ...)
...;
// i doesn't exist
Incidentally, variables can be declared in any block, and their scope is the block.
{
int i;
...
}
// i doesn't exist
Examples:
for (double x = 2.5; x < 100.0; x *= 2.0)
System.out.println(x);
for (double sum = 0.0;
scan.hasNext();
sum = sum + scan.nextDouble())
System.out.println(sum);
mult += value;* per iteration.* longer than the previous.Usage: given an iterator,
for (type elt : iterator)
statement;
is equivalent to
while (iterator.hasNext()) {
elt = iterator.next();
statement;
}
iterator() methodUsage: given a collection,
for (type elt : collection)
statement;
is equivalent to
Iterator iterator = collection.iterator();
for (type elt: iterator)
statement;
Example: foreach.java
Number of iterations:
for and while iterate 0 or more timesdo iterates at least 1 timeWhen is the condition evaluated?
for and while test test at the startdo tests test at the endDefinite and indefinite iteration
while, do, and for allow both definite and indefinite iterationfor is especially suitable for definite iterationwhile and do are especially suitable for indefinite iteration;) as loop bodyfor loops without init, test, or updateif, while, fordo while, switch, conditional operatorWrite a method that prints the powers of 2 from 20 = 1 to 210 = 1024.
whiledo whileforif statement to test n ≥ 0?Loops and conditionals can result in some interesting graphical programs.
Bullseye, BullseyePanel (Listings 6.5–6.6)
for loop draws concentric circles with decreasing radiusBoxes, BoxesPanel (Listings 6.7–6.8)
paintComponent method generates a new collection of boxes each time it runs
Pop-up windows that engage the user briefly
The class javax.swing.JOptionPane has useful methods for creating dialog boxes:
Get user input, using msg as prompt:
static String showInputDialog(Object msg)
Show a confirmation dialog, with Yes, No, Cancel buttons:
static int showConfirmDialog (Component parent,
Object msg)
Dialog box appears over the parent argument; if null, then at center of screen.
Returns int values such as JOptionPane.YES_OPTION
Show a message with an OK button:
static void showMessageDialog (Component parent,
Object msg)
EvenOdd (Listing 6.9)
Demonstrates the three types of dialog box
switchdo while, for