Suppose you have to write a program (for Homework 02) to translate a
  number between 0 and 4 into the closest letter grade. For example 2.8
  needs to be reported as a B- and 2.85 as a B (thus breaking the tie in
  favor of the better grade). 

  0.4  Not an F. Not an F. Maybe a D-. It's a D-. I think so too. 

  0.4 is only 0.3 away from 0.7 (D-) but it is 0.4 away from 0 (F). 

  0.35 Not an F. It's a D-. 
  0.5 This is clearly a D-. 
  0.3499 This is indeed an F. 
  3.8 It's an A-. 
  3.5 A- because same distance (0.2) to A- and B+

  0        0.7  1              2         2.7  3  3.3    3.7  4
  +---------+---+---+------+---+----------+---+---+------+---+
  F        D-   D              C         B-   B  B+     A-   A   

  2.8499 B- because it's to the left of 2.85 which equally distanced from B- and B. 
  2.85 Already discussed. 
  1.1499 D it is. 
  2.15 Jun says C+ and he is right. 

  Where is Sara?

  Where is Alex? 

  Where is Shengyu? 

  This is your homework and will be posted later today. 

--

  Where is Lane? 

public class Tuesday {
  public static void main(String[] args) {
    int i = 9; 
    
    if (i < 20) { // i checks out as < 9
      i = i + 1; // i becomes 10 
    }
    
    System.out.println(i); // prints 10
  }
}

This program does exactly what we anticipated. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tuesday
10


Let's modify this program and try to predict the answer:

public class Tuesday {
  public static void main(String[] args) {
    int i = 9; 
    
    while (i < 20) {       // while this is true 
      i = i + 1;           // i goes up by 1
    }
    
    System.out.println(i); // 20
  }
}

public class Tuesday {
  public static void main(String[] args) {
    int i = 91; 
    
    while (i < 20) {       
      i = i + 1;           
    }
    
    System.out.println(i); 
  }
}

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

This last program produces:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tuesday
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
Inside the loop: 20
Outside the loop: 20


This is called tracing the variables inside the program. 

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

This program runs forever. The loop inside is an infinite loop.

public class Tuesday {
  public static void main(String[] args) {
    int i = 90; 
    int count = 0; 
    
    while (i < 200) { 
      count = count + 1; 
      i = i + 3;     
      System.out.println("Inside the loop: " + i + " and count is " + count); 
    }
    
    System.out.println("Outside the loop: " + i); 
  }
}

This will count the number of times the loop is incrementing i (37). 

The sum of numbers from 1 to n: Jack says it's n * (n + 1) / 2

The sum of the squares from 1 to n: Jack says n * (n + 1) * (2 n + 1) / 6

The sum of the cubes from 1 to n: Jacks says leave me alone and he writes this

import java.util.Scanner; 

public class Tuesday {
  public static void main(String[] args) {
    System.out.print("Type a number: "); 
    int n = (new Scanner(System.in)).nextInt(); 
    int sum = 0; 
    int count = 1; 
    while (count <= n) {
      sum = sum + count * count * count;       
      System.out.println("Just added " + count + "^3 making sum " + sum); 
      count = count + 1; 
    }
    System.out.println( sum ); 
  }
}

Here's how this program runs: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tuesday
Type a number:  [DrJava Input Box]
Just added 1^3 making sum 1
Just added 2^3 making sum 9
Just added 3^3 making sum 36
Just added 4^3 making sum 100
Just added 5^3 making sum 225
Just added 6^3 making sum 441
Just added 7^3 making sum 784
Just added 8^3 making sum 1296
1296


Next time (in lab) we will talk about for loops and nested for loops. 

--

Read Loops from here

http://silo.cs.indiana.edu:8346/cgi-bin/c212/spr2014/textbook?action=next&pic=three/image029.jpg