Chapter 5: Conditionals and Loops

(5.1) Boolean expressions

Relational operators

    a == b
    a != b
    a < b
    a <= b
    a > b
    a >= b

Logical operators

    !p
    p && q
    p || q

(5.2) The if statement

  1. Simple if
  2. if-else
  3. Blocking
  4. Nested ifs

Simple if

if-else statement

Blocking.

Listing 5.5 Guessing

Blocking styles

    // 1.  Textbook style    // 2.  Alternate style

    if (condition)           if (condition) {
    {                          statement; ...
      statement; ...         }
    }                        else {
    else                       statement; ...
    {                        }
      statement; ... 
    }

Blocking styles

  1. Avoid this:

    if (condition) {
      statement; ...
    } else { // don't do this!!!
       statement; ...
    }
    

    The else is hidden.

Nested if statements

How to write:

    // Textbook        // Simpler?

    if (c1)            if (c1)
      a1;                a1;
    else               else if (c2)
      if (c2)            a2;
        a2;            else
      else               a3;
        a3;

Listing 5.6 MinOfThree

(5.3) Comparing data

Floating Point Comparisons

== requires exact equality, but floating point operations are inexact, so we usually want to find whether |ab| < tolerance.

if (Math.abs(a - b) < tol)
   ...
else
   ...

Comparing characters

The Unicode character set determines the ordering of characters.

Comparing Strings (1)

Comparing Strings (2)

The comparison is made on the basis of alphabetical order, or, to speak more precisely, lexicographic order, the order determined by the character codes.

Part 2

Graphics applications

(5.7) Determining event sources

Example

LeftRight, LeftRightPanel (Listings 5.12–5.13)

(5.8) Check and Radio Buttons

Check boxes

Example

StyleOptions, StyleOptionsPanel (Listings 5.14–5.15)

Radio buttons

Example

QuoteOptions, QuoteOptionsPanel (Listings 5.16–5.17)

Part 3: Iteration

(5.4) The while statement

Syntax and Semantics of while

Syntax:

while (expr)
  statement;

Semantics:

Listing 5.7 Average

Listing 5.8 WinPercentage

Infinite loops

Nested Loops

Listing 5.9 PalindromeTester

Remarks on Lab 4

While, Break, and Continue

Why While/Break Is Bad

Normal exit from a while loop implies the condition is false.

while (x > 0)
{
    something;
    if (x == 2)
      break;
    x--;
}
// assert x <= 0 ???

The break statement makes it harder to reason about code.

(5.5) Iterators

Using an Iterator (Example)

(Assume c is some type of collection)

Iterator citer = c.iterator();
while (citer.hasNext()) {
  Object thing = citer.next();
  // process thing ...
}

Scanners as Iterators

Listing 5.10 URLDissector.java

Uses nested loops, with a Scanner as iterator for each loop.

(5.6) Array Lists

Methods of ArrayList

See Figure 5.8 for details.

ArrayList() // constructor
add(object)
add(index, object)
get(index)
set(index, object)

Constructor

What is E?

ArrayList<E>()

E is the type of object we want the list to contain.

new ArrayList<String>()
new ArrayList<Integer>()

More ArrayList Methods

clear()
remove(index)
indexOf(object)
contains(object)
isEmpty()
size()

Examples

Demo in DrJava

Example

Chapter Summary