Spring Semester 2005

Lecture Notes Eight: Curly Braces, Again, and Conditions.

# Equivalence of Programs

Let's start from this code:
```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {

int x = 1;
if (x > 0) {
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
}
} else {
System.out.println(x + " <= 0");
}

}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
0 < 1 < 5
frilled.cs.indiana.edu%```
You can try this program with many values for `x`.

The program above is extremely clear, since it uses curly braces.

(It would be good if you could draw a flow chart for it, too).

In any event, am equivalent program would be:

```class One {
public static void main(String[] args) {

int x = 1;

if (x > 0 && x < 5) System.out.println(" 0 < " + x + " < 5");

if (x <= 0) 	    System.out.println(x + " <= 0");

}
}```
For the same inputs it produces the same outputs and behaves the same.

There are other ways of writing a program that's equivalent to the one we started from.

(Do you understand how the two programs discussed above behave in the exact same way?)

# The Dangling `else` Problem

Let's consider the first program and start removing curly braces:
```class One {
public static void main(String[] args) {

int x = 1;
if (x > 0) {
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
}
} else {
System.out.println(x + " <= 0");
}

}
}```
If we remove the blue curly braces (they are not necessary, really) we obtain:
```class One {
public static void main(String[] args) {

int x = 1;
if (x > 0) {
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
}
} else
System.out.println(x + " <= 0");

}
}```
The program still remains the same.

Now if in the resulting program

```class One {
public static void main(String[] args) {

int x = 1;
if (x > 0) {
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
}
} else
System.out.println(x + " <= 0");

}
}```
we start reasoning as follows: the `if` in blue is just a statement.

Therefore the curly braces that surround it are not grouping more than one statement.

Therefore they are not needed (the block they define has only one statement).

And from this reason we can just drop them.

We obtain:

```class One {
public static void main(String[] args) {

int x = 1;
if (x > 0)
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
}
else
System.out.println(x + " <= 0");

}
}```
The program still runs, but it needs to be re-arranged.

So we carefully re-indent it to obtain:

```class One {
public static void main(String[] args) {

int x = 1;
if (x > 0)
if (x < 5) {
System.out.println(" 0 < " + x + " < 5");
} else
System.out.println(x + " <= 0");
}
}```
And now we have a different program.

The program above (try it for `x = 10`) does not run correctly.

(It reports that `10` is less than `0`).

Here's what this program is equivalent to now:

```class One {
public static void main(String[] args) {

int x = 1;

if (x > 0 && x < 5) System.out.println(" 0 < " + x + " < 5");

if (x > 0 && ! (x < 5) ) System.out.println(x + " <= 0");

}
}```
(Can you see why?)

See, there were three pairs of curly braces in the original code.

The first one we removed: was redundant.

The second one we removed: we removed it for the wrong reason.

Its purpose was not to create a block of one statement.

Its purpose was to keep the inner `if` without an `else`.

By removing it, the `else` start sliding, gravitating towards the inner `if`, and binding to it.

(The third pair of curly braces also can be removed, like the first).

# The Use of `booleans`

In the program above we could also say:
```class One {
public static void main(String[] args) {

int x = 1;

boolean a = x > 0;

boolean b = x < 5;

if (a && b) System.out.println(" 0 < " + x + " < 5");

if (a && ! b ) System.out.println(x + " <= 0");

}
}```
If `a` and `b` are complicated expressions with meaningful short names this improves the legibility of your code.

Last updated by Adrian German for A201/A597/I210 on `Thu Feb 3 15:18:30 EST 2005`