Chapter 6: More Conditionals and Loops

(6.1) The switch statement

Evaluates one expression and compares it to different values; executes statements corresponding to the first value that the expression equals.

Syntax (simplified)

switch (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
}

Example (1)

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 
}

Example (2)

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 and Switch Statements

Listing 6.1 GradeReport

(6.2) The Conditional Operator

Example

// with if        // with conditional operator

if (y > 10)       x = y > 10 ? 1 : 2;
  x = 1;
else
  x = 2;

(6.3) The do statement

Example of do Statement

double x = 12.0;
do {
  System.out.println(x);
  x /= 2.0;
}
while (x > 3.5)

What if x is initialized as 3.1?

Listing 5.12 ReverseNumber

(6.4) The for Statement

Syntax and Semantics of for

Syntax:

for (init; test; update)
  statement

Semantics: equivalent to

    init;
    while (test) {
      statement;
      update;
    }

Variables declared in for

Variables declared in a block

Updates in for

Listing 6.3 Multiples

Listing 6.4 Stars

Enhanced for statement (1)

Enhanced for statement (2)

Enhanced for statement (3)

Usage: given a collection,

for (type elt : collection)
  statement;

is equivalent to

Iterator iterator = collection.iterator();
for (type elt: iterator)
  statement;

Example: foreach.java

Comparing loops (1)

Number of iterations:

Comparing loops (2)

When is the condition evaluated?

Comparing loops (3)

Definite and indefinite iteration

Loop Bugs and Tricks

Key Points

Exercise

Write a method that prints the powers of 2 from 20 = 1 to 210 = 1024.

Lab 5 Introduction

(6.5) Drawing with loops ...

Loops and conditionals can result in some interesting graphical programs.

Example

Bullseye, BullseyePanel (Listings 6.5–6.6)

Another Example

Boxes, BoxesPanel (Listings 6.7–6.8)

(6.6) Dialog boxes

Pop-up windows that engage the user briefly

Dialog boxes: Methods (1)

The class javax.swing.JOptionPane has useful methods for creating dialog boxes:

Dialog boxes: Methods (2)

Get user input, using msg as prompt:

static String showInputDialog(Object msg)

Dialog boxes: Methods (3)

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

Dialog boxes: Methods (4)

Show a message with an OK button:

static void showMessageDialog (Component parent,
                               Object msg)

Example

EvenOdd (Listing 6.9)

Demonstrates the three types of dialog box

Chapter Summary