Category Archives: TEALS

Branching Statements

Up to this point, we’ve only observed code that has one path in the flow of control.

statement 1;
statement 2;
statement 3;
….
Done

In other words, all of the instructions of the code we’ve seen up to this point are executed in a serial manner and then the program ends.

Wouldn’t it be nice to execute different instructions a set of conditions were true?

Take my phone as an example. It runs software and it behaves differently depending on what’s going on. To ensure the battery lasts all day the screen better be off until I turn it on.

That means, although my phone is on, if my phone is in my pocket the screen is off. When I pull the phone out of my pocket the screen is still off. However, if I press the screen button it turns on!

The state of my phone screen being on or off could be a boolean, couldn’t it?

boolean phoneScreenOn = false;

Also, the state of the button to turn my screen on could be a boolean, too.

boolean buttonToTurnScreenOnIsPress = false;

So, on the event that buttonToTurnScreenOnIsPressed is equal to true, phoneScreenOn needs to equal true.

Booleans and boolean expressions become a programmer’s best friend when they’re coupled with branching statements! Branching statements allow programmers to alter the control flow of code based on Booleans and Boolean statements conditions.

Here’s an example of an if statement.

if(austinsTeaching){
      System.out.println("All students better be taking notes.");
}

Consider the phone screen example, again. We can use an if statement to make sure phoneScreenOn is only set to true when buttonToTurnScreenOnIsPressed is true.

if(buttonToTurnScreenOnIsPressed){
      phoneScreenOn = true;
}

Boolean Expressions

Sure, boolean variables are nice. Boolean expressions are even better. Below are a couple of examples.

Is one greater than two?
1 > 2
Of course not, so the answer is false.

Is two greater than one?
2 > 1
It is, so it must be true!

Is X greater than two?
x > 2
We can’t answer this until we know the value of x!

int x = 0;
boolean isXGTRThan = x > 2; // what's the value of isXGTRThan?

x = 2013;
isXGTRThan = x > 2// what's the value of isXGTRThan?

There are a number of operators that can be used in Boolean expression:

  • greater than: >
  • less than: <
  • equal to: ==
  • greater than or equal to: >=
  • less than or equal to: <=
int x = 5;

boolean expression = x > 2;
x = 3;

expression = x < 4; // what's the value of expression?
x = x; // what's the value of x?
expression = 5 >= x; // what's the value of expression?
expression = x == x; // what's the value of x and expression?

We can build even larger Boolean expressions with logical conjunctions and disjunctions.

  • The conjunction operator is: &&. This denotes a logical ‘and’.
  • The disjunction/disjoint operator is: ||. This denotes a logical ‘or’.
boolean one = true;
boolean two = true;
boolean three = false;

boolean expression = one && two; // what is the value of expression?

expression = one || three; // what is the value of expression?

expression = one && three; // what is the value of expression?

Let’s see how well you know this stuff. 🙂

boolean expression = ((true && false) || (3 < 2)); // what's the value of expression?

-> Branching Statements

Boolean

What is a boolean?

  • It’s a primitive data type. (So, it’s simpler than an object but it’s often used as one or more instance variables in a class.)
  • It can only have one of two values: true or false.
boolean austinsTeaching = true;

boolean mercerIslandRocks = true;

boolean theSeahawksWillLoseTheirNextGame = false;

-> Boolean expressions