Recall the diagram (flow chart) from last time. 

I graded Lab 03. Attendance in lab last week == submission in GitHub. 

This week I come to labs and take attendance stay until I talk to you. 

Syllabus says you can make up anything.

https://www.cs.indiana.edu/classes/c212-dgerman/fall2018/syllabus.html

Lab 04 posted. Exam next week. Lab 05 is self-assessment Exam 01. 

http://silo.cs.indiana.edu:8346/c212/sum2016/6w2c212a592exam01sols.phps

Lab 04 wants you to self-assess your Lab 03. 

if (gpa >= 2.0) {
  print "You're fine."; 
} else {
  if (gpa < 1.5) {
    print "You are failing."; 
  } else {
    print "You are on probation."; 
  } 
}

--

Dangling else is a situation that is ambiguous. 

if (1 < 2) {
  print "Howdy, ";
  print "Pardner."
}

--

if (1 > 2) {
  print "Howdy, ";
  print "Pardner."
}

--

if (1 < 2) 
  print "Howdy, ";
  print "Pardner."

--

if (1 > 2) 
  print "Howdy, ";
  print "Pardner."

--

Compare the flowcharts for these four snippets of code. 

if (gpa >= 1.5) {
  if (gpa < 2) {
    print "probation"; 
  } else {
    
  } 
} else {
  print "failing"; 
}

If a branch has only one statement the {, }'s are not needed

This:

if (1 < 2) {
  print "Hi.";
}

is the same as this:

if (1 < 2) 
  print "Hi.";

So now I start to simplify

if (gpa >= 1.5) {
  if (gpa < 2) 
    print "probation"; 
} else 
  print "failing"; 

At this point the code is simpler and still works the same. 

Background: 

  if (1 < 2) {
    if (3 < 4) {
      print "What now?"; 
    } 
  }

The code above is the same as:


  if (1 < 2) 
    if (3 < 4) 
      print "What now?"; 
    
So I could think that it's safe to do this:   

if (gpa >= 1.5) 
  if (gpa < 2) 
    print "probation"; 
else 
  print "failing"; 

That's the dangling else.

A dangling else example must have a before and an after. 

int i = ...;

System.out.println( i == i++ ); // true 

System.out.println( i == ++i ); // false

System.out.println( i++ < i ); // true 

for (int i = 0; i < 10; i = i + 1 ) {
                        i += 1
                        i++
                        i = i++ // bad style also i never changes 
                        ++i 
                        i = ++i // also bad style now you're lucky
  System.out.println( i ); 
}

int i = 7; 
while (i < 10) ; {
  System.out.println( i ); 
  i = i + 1; 
}

import java.util.Scanner; 

public class One {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    String line; 
    double sum = 0; 
    int count = 0; 
    System.out.print("Number: "); 
    line = in.nextLine(); 
    while (! line.equals("bye")) {
      double num = Double.parseDouble( line ); 
      sum = sum + num; 
      count += 1; 
      System.out.println( sum / count ); 
      System.out.print("Number: "); 
      line = in.nextLine(); 
    } 
  }
}

So this should work well. 

The book wants you to solve this in a slightly different manner as well. 

--