Howdy.

We start with


public class Wednesday {
  public static void main(String[] args) {
     int n = 9; // n is 9
     
     if (n < 20) { // 9 < 20 so 
       n = n + 1;  // n becomes 10
     }
     
     System.out.println( n ); // prints 10
  }
}

Megan predicts what's in the comments and she's right. 


public class Wednesday {
  public static void main(String[] args) {
     int n = 9; // n is 9
     
     while (n < 20) { 
       n = n + 1;  
     }
     
     System.out.println( n ); 
  }
}

Ingrid says this prints the numbers from 10 to 19 and then stops. 

Here's a program like the one she was referring to: 

public class Wednesday {
  public static void main(String[] args) {
     int n = 9; // n is 9
     
     while (n < 20) { 
       n = n + 1;                                     // [1] 
       System.out.println( "Inside the loop: " + n ); // [2]
     }
     
     System.out.println( n ); 
  }
}

Quick question what if [1] and [2] are swapped? 

We get this output:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Wednesday
Inside the loop: 9
Inside the loop: 10
Inside the loop: 11
Inside the loop: 12
Inside the loop: 13
Inside the loop: 14
Inside the loop: 15
Inside the loop: 16
Inside the loop: 17
Inside the loop: 18
Inside the loop: 19
20
>

If n starts bigger than 20 (or 20) Hunter says loop doesn't execute. 


public class Wednesday {
  public static void main(String[] args) {
     int n = 9; 
     
     while (n != 20) { 
       n = n + 1; 
       System.out.println( "Inside the loop: " + n ); 
     }
     
     System.out.println( n ); 
  }
}

Hunter says this equivalent to one of the programs we wrote before.


public class Wednesday {
  public static void main(String[] args) {
     int n = 9; 
     
     while (n != 20) { 
       n = n + 3; 
       System.out.println( "Inside the loop: " + n ); 
     }
     
     System.out.println( n ); 
  }
}

Ingrid and Hunter both say this is an infinite loop. 

Let's discuss the homework now. 

I will give you some numbers and you will tell me which letter grade is closest. 

0        0.7  1  1.3   1.7  2             3  3.3        4
+---------+---+---+-*---+---+-------------+---+--------+
F        D-   D  D+    C-   C             B  B+

1.41421356   ...   D+   (Brian, Aaron and Tori) 

1.732050     ...   C-   (Caulin, Tori) 

3.141592     ...   B    (Ben) 

2.71828      ...   B-   (Yifan)

3.8729

0.5

0.3

What is a tie between D- and F? The answer is 0.35 (Caulin). 

for loops

Suppose you know what a while loop does:

     while <COND> {
      <BODY>
     }

This is the general form of a while loop. 

Here's how we could have written our first loop today with a for: 

     int n; 
     for (n = 9; n < 20; n = n + 1) {
       System.out.println( n ); 
     } 

This is how we wrote it with a while: 

     int n; 
     n = 9; 
     while (n < 20) {
       System.out.println( n ); 
       n = n + 1; 
     } 

What is the general form of a for loop then?

     <DECL>
     for ( <INIT> ; <COND> ; <STEP> ) {
       <BODY>
     }

How do you write this as a while? 

     <DECL>
     <INIT>
     while <COND> {
       <BODY>
       <STEP> 
     } 

So in lab expect exercises with while and for including some experiments
with nested loops. 

--