Wednesday next week there is an exam. 

Study guides to be posted (and old exams). 

The exam is over the first six chapters. 

int x=18, y=10; if (x < 10) { if (x > 5) y=1; } else y=2; 

At the end y will be 2. 

Now we take the curly braces out. What happens? 

int x=18, y=10; if (x < 10) if (x > 5) y=1; else y=2;

Dangling else. 


Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> int i
> i = 5
5
> i = i + 1
6
> i
6
> i
6
> --i
5
> i
5
> i--
5
> i
4
> i
4
> --i
3
> i
3
> i--
3
> i
2
> i++ + ++i // 2 (and i becomes 3) + 4 (and i remains 4) 
6
> i
4


if (i < 10) {
  i = i + 1; // increment i by 1
}

while (i < 10) {
  i = i + 1; // increment i by 1
}

Write program that calculates 1 + 2 + 3 + ... (n-1) + n

public class One {
  public static void main(String[] args) {
     int n = 6;
     System.out.println( n * (n + 1) / 2 ); 
     int sum = 0; 
     int i = 1;      
     System.out.println("I start with a sum of " + sum); 
     while (i <= n) {
       sum = sum + i; 
       System.out.println("Adding " + i + " sum becomes " + sum); 
       i = i + 1; 
    }
     System.out.println( sum ); 
  }
}

Here's how it runs in DrJava:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> run One
21
I start with a sum of 0
Adding 1 sum becomes 1
Adding 2 sum becomes 3
Adding 3 sum becomes 6
Adding 4 sum becomes 10
Adding 5 sum becomes 15
Adding 6 sum becomes 21
21


while and for loops are entirely equivalent.

INIT; // int sum=0, i=1; 
while (COND) { // i < n
  BODY; // sum = sum + i;
  STEP; // i = i + 1;
}

for ( INIT; COND ; STEP ) {
  BODY;
}

So our program looks like this: 

int sum = 0; 

for (int i = 1; i < n; i = i + 1) {
  sum = sum + i; 
}

System.out.println( sum ); 

Now I practice with for loops:

public class Two {
  public static void main(String[] args) {
    int size = 9; 
    for (int i = 0; i < size; i = i + 1) {
      for (int j = 0; j < size; j = j + 1) {
        if (i == 0 || i == size-1 || j == 0 || i == size/2 && j < size/2)
          System.out.print("* ");  
        else 
          System.out.print("  ");
      }
      System.out.println(); 
    }
  }
}

Here's how this runs in DrJava:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> run Two
* * * * * * * * * 
*                 
*                 
*                 
* * * *           
*                 
*                 
*                 
* * * * * * * * * 


Here's 5.26 discussed:

public class Bae {
  public static void main(String[] args) {
    double gpa = 2.8; 
    
    if (1.5 <= gpa) {
      if (gpa <= 2.0) {
        System.out.println("Student on probation."); 
      } else {
        System.out.println("Student in great shape."); 
      }
    } else {
      System.out.println("Student failing."); 
    }
  }
}

Problem wants us to be quiet when student in great shape. 

public class Bae {
  public static void main(String[] args) {
    double gpa = 2.8; 
    
    if (1.5 <= gpa) {
      if (gpa <= 2.0) {
        System.out.println("Student on probation."); 
      } 
    } else {
      System.out.println("Student failing."); 
    }
  }
}

So we claim we simplified it. 

We also know curly braces are optional when the block is
one statement only. But in this case dropping them would
be a mistake because the else is dangling without them.

public class Bae {
  public static void main(String[] args) {
    double gpa = 2.8; 
    
    if (1.5 <= gpa) 
      if (gpa <= 2.0) 
        System.out.println("Student on probation."); 
    else 
      System.out.println("Student failing.");     
  }
}

Now the else migrated to the inner if and the student
has a 2.8 GPA but is reported as failing. Dangling else
and we need the curly braces to keep this from happening.

--