Today is Tue Jun 25. 

We start with the announced minute paper. 

Next we work out some problems. 

if (...) {
  ...
} else {
  ...


Notice the curly braces. 

They help eliminate ambiguity. 

Sometimes they are not needed. 

For example: 

2 + 3 * 5 is the same as (2 + (3 * 5))

But ((2 + 3) * 5) is no longer the same calculation. 

So we have to be careful. 

http://www.cs.indiana.edu/classes/a201-dger/sum2002/practiceThree.html

Exercise One 

What is the output produced by the following code when embedded in a complete program?

  int x = 3;
  if (2 > x)
    System.out.print(1);
  else
    System.out.print(2);
  if (x < 2)
    System.out.println(3);
  System.out.print(4); 

We worked this out, the code was equivalent to: 

  int x = 3;

  if (2 > x) { 
    System.out.print(1);
  } else { 
    System.out.print(2);
  } 

  if (x < 2) { 
    System.out.println(3);
  } else { 

  } 

  System.out.print(4); 

So a typical question is: given code like above add
curly braces to make the sequence of steps unambiguous. 

That's always a meaningful exercise. 

Exercise Two 

What is the output produced by the following code when 
embedded in a complete program?

  int x = 3;
  if (2 > x)
    System.out.print(1);
  else
    System.out.print(2);
  if (x < 2) {
    System.out.println(3);
  System.out.print(4); } 

Note that curly braces have been added to the code from 
the previous exercise, and the same comment about indentation applies.

Answer: we rewrite the code as follows, 

  int x = 3;

  if (2 > x) { 
    System.out.print(1);
  } else { 
    System.out.print(2);
  } 

  if (x < 2) {
    System.out.println(3);
    System.out.print(4); 
  } else {

  }  

In some sense when we indent we write down a flowchart. 

We worked this out: 

Question Three 

What is the output produced by the following code when 
embedded in a complete program?

int x = 3;
if (x > 5)
  if (x < 10)
    System.out.print(1);
else
  System.out.print(2);
System.out.print(3); 

We also asked: what is a good value for x to have 2 printed? 

We tried with 6 and 11. 

The rewriting of the code produced this:

int x = 3;

if (x > 5) { 
  if (x < 10) { 
    System.out.print(1);
  } else { 
    System.out.print(2);
  } 
}

System.out.print(3); 
 
Now all questions have easy answers. 

Next the question was: 

  int x = 3;
  if (x > 5) {
    if (x < 10)
      System.out.print(1); }
  else
    System.out.print(2);
  System.out.print(3); 

If we indent, we get: 

  int x = 3;
  if (x > 5) {
    if (x < 10)
      System.out.print(1); 
  } else
    System.out.print(2);
  System.out.print(3); 

If we add curly braces to remove ambiguity: 

  int x = 3;

  if (x > 5) {
    if (x < 10) { 
      System.out.print(1); 
    } else {

    }
  } else { 
    System.out.print(2);
  } 

  System.out.print(3); 

Again we tried with 3, 6 and 11 for x. 

We then worked out the problems from the minute paper:

class Three {
  public static void main(String[]args) {
    if(args[0].equals("true")) {
      if(args[1].equals("true")) {
        System.out.println("I am Statement One.");
      } else {

      }
    } else {
      System.out.println("I am Statement Two.");
    }
  }
}

The good thing about this program is that it only takes four inputs. 

class Four {
  public static void main(String[]args) {
    if(args[0].equals("true"))
      if(args[1].equals("true")) {
        System.out.println("I am Statement One.");
      } else {

      }
    else {
      System.out.println("I am Statement Two.");
    }
  }
}

Four is identical with Three and we explained why. 

However in moving from Four to Five we make a mistake:

class Five {
  public static void main(String[]args) {
    if(args[0].equals("true"))
      if(args[1].equals("true")) {
        System.out.println("I am Statement One.");
      }
    else {
      System.out.println("I am Statement Two.");
    }
  }
}

The mistake is called "dangling else". 

Eliminating an empty else branch isn't always harmless. 

Just because the else is optional doesn't mean we can always remove it.

So back to Three what is the correct way to remove the else branch? 

--