Spring Semester 2005


Lecture Notes Seven: The Effect of Curly Braces on Man-in-the-Moon Marigolds.

We learned how we could group statements with curly braces.

The thus glued together statements become a monolith.

There is an additional effect that we are trying to identify, describe and learn.

Let's get started.

Basic Blocks (of Statements)

You remember this program that prints two R.E.M songs:
class One {
    public static void main(String[] args) {

	System.out.println("The Great Beyond\n"); 
        System.out.println("I've watched the stars fall silent from your eyes");
	System.out.println("All the sights that I have seen");
	System.out.println("I can't believe that I believed I wished");
	System.out.println("That you could see");
	System.out.println("There's a new planet in the solar system");
	System.out.println("There is nothing up my sleeve\n");

	System.out.println("I'm pushing an elephant up the stairs");
	System.out.println("I'm tossing up punchlines that were never there");
	System.out.println("Over my shoulder a piano falls");
	System.out.println("Crashing to the ground");
	System.out.println("-------\n"); 

	System.out.println("Fretless\n"); 
	System.out.println("He's got his work and she comes easy");
	System.out.println("They each come around when the other is gone");
	System.out.println("Me, I think I got stuck somewhere in between");
	System.out.println("I wouldn't confide in the Prodigal Son");
	System.out.println("The die has been cast, the battle is won");
	System.out.println("The bullets were blanks, a double aught gun");
	System.out.println("I couldn't admit to a minute of fun");
	System.out.println("-------\n"); 

    }
}
It works just as well (and just the same) if you group the songs as follows:
class One {
    public static void main(String[] args) {

	{ 
	    System.out.println("The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");

	}

	{
	    System.out.println("Fretless\n");
	    System.out.println("He's got his work and she comes easy");
	    System.out.println("They each come around when the other is gone");
	    System.out.println("Me, I think I got stuck somewhere in between");
	    System.out.println("I wouldn't confide in the Prodigal Son");
	    System.out.println("The die has been cast, the battle is won");
	    System.out.println("The bullets were blanks, a double aught gun");
	    System.out.println("I couldn't admit to a minute of fun");
	    System.out.println("-------\n");

	}

    }
}
Curly braces are a type of parens so as you see they're properly nested.

Conditionally Executing the Blocks

But now we can use an if statement to print only one or the other:
class One {
    public static void main(String[] args) {

        int song = 1; 

	if (song == 1) { 
	    System.out.println("The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");

	}

        else 

        {
	    System.out.println("Fretless\n");
	    System.out.println("He's got his work and she comes easy");
	    System.out.println("They each come around when the other is gone");
	    System.out.println("Me, I think I got stuck somewhere in between");
	    System.out.println("I wouldn't confide in the Prodigal Son");
	    System.out.println("The die has been cast, the battle is won");
	    System.out.println("The bullets were blanks, a double aught gun");
	    System.out.println("I couldn't admit to a minute of fun");
	    System.out.println("-------\n");

	}

    }
}
We have thus used a statement that has the following structure:
 if (<condition>) {
  <statement> ;
  <statement> ; 
  <statement> ;
  ...  
} else {
  <statement> ;
  <statement> ; 
  <statement> ;
  ...  
}  
The else part (in light blue) is optional.

Thus the program above could also be written as follows:

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

        int song = 1; 

	if (song == 1) { 
	    System.out.println("The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");

	}

	if (song != 1) {
	    System.out.println("Fretless\n");
	    System.out.println("He's got his work and she comes easy");
	    System.out.println("They each come around when the other is gone");
	    System.out.println("Me, I think I got stuck somewhere in between");
	    System.out.println("I wouldn't confide in the Prodigal Son");
	    System.out.println("The die has been cast, the battle is won");
	    System.out.println("The bullets were blanks, a double aught gun");
	    System.out.println("I couldn't admit to a minute of fun");
	    System.out.println("-------\n");

	}

    }
}
At this point it would be good to stop and draw a flowchart for each of the two programs.

A Simplified Example

Compile and run this program.
class One {
    public static void main(String[] args) {

        int x = 1, y = 2; 

	{ 

	    System.out.print("x is "); 
	    System.out.println(x); 

	}

	{

	    System.out.print("y is "); 
	    System.out.println(y); 

	}

    }
}
No surprises. But make the following change, then re-compile.
class One {
    public static void main(String[] args) {

	{ 

            int x = 1, y = 2; 

	    System.out.print("x is "); 
	    System.out.println(x); 

	}

	{

	    System.out.print("y is "); 
	    System.out.println(y); 

	}

    }
}
It turns out that curly braces (blocks) also define the scope of a variable.

Looping a Block of Statements

Just as a preview consider this:
class One {
    public static void main(String[] args) {

        int i = 1; 

        if (i < 10) {
	    System.out.println("The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");
	
	}

    }
}
You can execute it or not, but it's a one time thing.

If instead we change one keyword:

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

        int i = 1; 

        while (i < 10) { 
	    System.out.println("The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");

	
	}

    }
}
We end up with an infinite loop: i will always be less than 10 (in this program).

We could control how long we run this loop as follows:

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

        int i = 1; 

        while (i < 10) { 

            i = i + 1;  

	    System.out.println(i + ". The Great Beyond\n");
	    System.out.println("I've watched the stars fall silent from your eyes");
	    System.out.println("All the sights that I have seen");
	    System.out.println("I can't believe that I believed I wished");
	    System.out.println("That you could see");
	    System.out.println("There's a new planet in the solar system");
	    System.out.println("There is nothing up my sleeve\n");

	    System.out.println("I'm pushing an elephant up the stairs");
	    System.out.println("I'm tossing up punchlines that were never there");
	    System.out.println("Over my shoulder a piano falls");
	    System.out.println("Crashing to the ground");
	    System.out.println("-------\n");

	
	}

    }
}
A question one might ask is this:

Named Blocks

Just to show some other uses blocks of statements have, consider this example:
class REMcd {
    void theGreatBeyond() {

	System.out.println("The Great Beyond\n"); 

	System.out.println("I've watched the stars fall silent from your eyes");
	System.out.println("All the sights that I have seen");
	System.out.println("I can't believe that I believed I wished");
	System.out.println("That you could see");
	System.out.println("There's a new planet in the solar system");
	System.out.println("There is nothing up my sleeve\n");

	System.out.println("I'm pushing an elephant up the stairs");
	System.out.println("I'm tossing up punchlines that were never there");
	System.out.println("Over my shoulder a piano falls");
	System.out.println("Crashing to the ground");
        System.out.println("-----\n");

    }

    void fretLess() {

	System.out.println("Fretless\n"); 

	System.out.println("He's got his work and she comes easy");
	System.out.println("They each come around when the other is gone");
	System.out.println("Me, I think I got stuck somewhere in between");
	System.out.println("I wouldn't confide in the Prodigal Son");
	System.out.println("The die has been cast, the battle is won");
	System.out.println("The bullets were blanks, a double aught gun");
	System.out.println("I couldn't admit to a minute of fun");
        System.out.println("-----\n"); 

    }

    void losingMyReligion() {

	System.out.println("Losing My Religion\n"); 

	System.out.println("Life is bigger");
	System.out.println("It's bigger than you");
	System.out.println("And you are not me");
	System.out.println("The lengths that I will go to");
	System.out.println("The distance in your eyes");
	System.out.println("Oh no I've said too much");
	System.out.println("I set it up\n");

	System.out.println("That's me in the corner");
	System.out.println("That's me in the spotlight");
	System.out.println("Losing my religion");
	System.out.println("Trying to keep up with you");
	System.out.println("And I don't know if I can do it");
	System.out.println("Oh no I've said too much");
	System.out.println("I haven't said enough");
	System.out.println("I thought that I heard you laughing"); 
	System.out.println("I thought that I heard you sing");
	System.out.println("I think I thought I saw you try");
        System.out.println("-----\n"); 

    }

} 

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

	REMcd play = new REMcd(); // see above 

	play.theGreatBeyond(); // plays one song
        play.fretLess(); // plays another (by the name) 
	play.theGreatBeyond(); // and the first one again 
	play.losingMyReligion(); // ... and another one

    }
}
Compile everything and run java Music

This is just an idea of selectively running (playing) blocks by name.

It's your first view of object-oriented programming, and you will see more of it later.

A REMcd is just a (somewhat primitive) kind of Penguin, or ConsoleReader.

Now we get back to discussing decisions and if statements.

Booleans and Their Operators

class One {
    public static void main(String[] args) {
        boolean a, b; 

        // first: negation 

        a = true; 
	System.out.println("not true is: " + ! a); 

        a = false; 
	System.out.println("not false is: " + ! a); 

        // and: you pass this class if you pass midterm one AND midterm two 

        a = false; 
	b = false; 
	System.out.println("false and false is: " + (a && b)); 

        a = true; 
	b = false; 
	System.out.println("true and false is: " + (a && b)); 

        a = false; 
	b = true; 
	System.out.println("false and true is: " + (a && b)); 

        a = true; 
	b = true; 
	System.out.println("true and true is: " + (a && b)); 

        // or: you pass this class if you pass midterm one OR you pass midterm two 

        a = false; 
	b = false; 
	System.out.println("false or false is: " + (a || b)); 

        a = true; 
	b = false; 
	System.out.println("true or false is: " + (a || b)); 

        a = false; 
	b = true; 
	System.out.println("false or true is: " + (a || b)); 

        a = true; 
	b = true; 
	System.out.println("true or true is: " + (a || b)); 

    }
}
You will see concrete example of conditions using logical connectors soon.

Exercise: try to prove (by writing a program like the one above) that

!a || !b == !(a && b)
This is called de Morgan's law, and it has a dual:
!a && !b == !(a || b)

One Important Note

Note that blocks are monoliths.

The real structure of an if statement is

 if (<condition>) 
  <statement> ;
else 
  <statement> ;
So if the curly braces are missing, indentation doesn't matter.

Witness the following example from last year.


Last updated by Adrian German for A201/A597/I210 on Tue Feb 1 12:08:05 EST 2005