15-100 Lecture 7 (Wednesday, September 13, 2006)

Today's Quiz

On today's quiz, you were asked to make a new scanner, read and print out two numbers, and read and print out two Strings.

  Scanner keyboard = new Scanner(System.in);
  //Using a file instead of System.in was also acceptable
  int x = keyboard.nextInt();
  int y = keyboard.nextInt();
  String r = (String) keyboard.next();
  String s = (String) keyboard.next();

Thus far, our progams have basically been a simple set of one-after-another instructions. But, programs can also make decisions. And, the way they do it is very natural to people. Consider the following: If pasta is cheaper than chicken, order the pasta.

The logic above is represented in code as below:

  if (chickenPrice < pastaPrice) {
    System.out.println ("Chicken, please.");
  } else {
    System.out.println ("Pasta, please.");

If we consider the code segment above, we observe the reseved word, "if". It begins the decision making process. The next thing to follow is the predicate. It is a so-called "boolean expression". Basically, it is a statement that is either true or false.

The code block, immediately following the "if (predicate)" and set apart by the {}s is executed if, and only if, the predicate is true. The second block, prefaced with the "else" is executed if, and only if, the predicate is false. An if statement need not have an else. Consider the following example:

  if ( (officeFloor - myFloor) > 0) {
    System.out.println ("Hey, wait. Let's take the elevator.");
  else {
    System.out.println("Hey, let's go to my office.");

It is not technically necessary to have the {}s after either the if- or the else- block. Absent {}s, the next statment after the "if" or "else" is assumed to be the body.

If there are multiple statmenets between an "if" and "else", and they aren't contained within {}s, this is a syntax error. But, if there are multiple statements after an if, without an else, or in an else, only the first one is conditional -- the rest are outside of the conditional, regardless of indentation. Notice this problem in the example below:

    if (amountOfMoney < 50.00) 
      System.out.println ("Wait up! I need to use the ATM!");
      amountOfMoney += getMoneyFromATM(50.00);

The Operators

In making comparisons, we can use any of several boolean operators: <, >, <=, >=, ==, !=. They all function exactly as you would expect. The only one that is a little tricky is the ==-operator. Please notice that it is ==, not just =. Remember, a single equal sign is an assignment, a double is the equality operator.

These operators work on numbers and "char". We'll talk a bit more about comparing Strings in a few days.

The result of any of these operators is a value, true or false.

The boolean type

Java has a special data type for the binary values true and false. It is the boolean type. It can be assigned literal values, as follows:

  boolean lightsOn = true;
  lightsOn = false;

Or, they can be assigned the true or false result of a boolean expression, as follows:

  boolean aliceIsTaller = (aliceHeight > bobHeight);

Boolean expressions can be joined together using the logical AND and logical OR operators. && is the logical and. If boolean expressions are joined together with the &&-operator, both must be true for the whole expression to be true. Similarly, if they are joined together with the ||-operator, only one must be true. Please consider the following example:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));
  boolean okayToSnack = ( (hoursSinceLastMeal > 3) && (hoursTillNextMeal > 2 ) );

Boolean Variables as Predicates

Boolean variables have a value that is either true or false, so they can be used as predicates. For example, consider the following:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));

  if (needAirConditioning) {

... is equivalent to, but less cluttered than, the following:

  boolean needAirConditioning = ( (temp > 75) || (relativeHumidity > 0.65));

  if (needAirConditioning == true) {

The ! operator

There is an additional operator that is valid for boolean expressions. It is the !-operator, sometimes known as the negation or NOT operator. Very simple, it turns true into false and vice-versa.

boolean aliceIsTaller = (aliceHeight > bobHeight); if (!aliceIsTaller) { System.out.println ("Bob is at least as tall as Alice."); }

Brackets on If/Else Statements

When using an if statement, if you leave off the brackets, it will automatically assume the next line of code to be included in the if statement. If you want to make use of this, it is very important that you understand it fully.

  System.out.println("Inside the if Statement");
  System.out.println("Not inside the if Statement");

The above code fragment acts just like the following one:

    System.out.println("Inside the if Statement");
  System.out.println("Not inside the if Statement");

There is nothing wrong with always including the brackets. In fact, it generally makes your code much easier to read and troubleshoot. These rules also apply for else.