If Statements


If statements

Java has an if-statement with a similar purpose to the Cinnameg If-statement. It looks a little different, though. The general form is

  if ( expression )
    statement
  else
    statement
where the expression must produce a value of type boolean. If the value is true, then the first statement is done. If the value is false, then the statement that follows else is performed.

Although the meaning of the Cinnameg If-statement and the Java if-statement are the same, there are differences in appearances.

  1. In Java, if is all lower case letters. Do not capitalized it.

  2. In Java you do not write then. The statement to be performed immmediately follows the condition.

  3. In Java, the condition being tested is required to be surrounded by parentheses. Leaving off the parentheses will cause an error.

  4. In Java, there is no end of if marker.

  5. In Java, just one statement follows the condition, and just one statement follows the word else. You typically use a compound statement to include several statements.

The best way to deal with remembering the form is to have some examples to look at and to follow the form in those examples.

Typically (but not always), the statements that are part of an if-statement are compound statements. For example,

  if (x > y)
  {
    m = x;
  }
  else
  {
    m = y;
  }
makes m equal to the larger of x and y. (Notice the indentation in the compound statements.) In cases where the statements are not compound statements, you can either write them on the same line as if or else, or you can write then on separate lines, but indent them. Here are two more acceptable ways to make m equal to the larger of x and y. Form
  if (x > y) m = x;
  else m = y;
puts the statements m = x and m = y on the same lines as if and else. Form
  if (x > y)
    m = x;
  else
    m = y;
puts them on separate lines, but indents them.


Omitting else

If you do not want to do anything when the condition being tested is false, just omit the word else and the statement that follows it. For example,

  m = x;
  if (x < y) m = y;
is yet another way to make m equal to the larger of x and y. Notice that else has been omitted.


Handling several cases

Suppose that you want to make s be the sign of x, defined to be -1 if x < 0, to be 1 if x > 0 and to be 0 if x is 0. One way to do that is as follows.

  if(x < 0) 
  {
    s = -1;
  }
  else
  {
    if(x > 0)
    {
      s = 1;
    }
    else
    {
      s = 0;
    }
  }
That breaks the problem into two cases (either x is negative or it is not) and then further breaks the second case into two more cases (either x is positive or it is 0). But for this problem, there are really three cases, and it is more reasonable to present it in a form that shows the three cases. You usually do that by writing else if, as follows.
  if(x < 0) 
  {
    s = -1;
  }
  else if(x > 0)
  {
    s = 1;
  }
  else
  {
    s = 0;
  }
Notice that you do not indent further and further. All three cases are indented the same amount. Of course, you could shorten this further to the following.
  if(x < 0) s = -1;
  else if(x > 0) s = 1;
  else s = 0;
A few spaces make that even more readable.
  if     (x < 0) s = -1;
  else if(x > 0) s = 1;
  else           s = 0;


Context

Each statement inside an if-statement is also a context, regardless of whether it is a compound statement or not. To create a variable m and make it be the larger of x and y, you can write

  int m;
  if(x > y) m = x;
  else m = y;
but it will not work to write
  if(x > y) int m = x;
  else int m = y;
since, in the latter case, variable m is destroyed as soon as you get out of the if-statement. There is no analog of an "open if" in Java.


More information

If-statements are discussed in Chapter 3.1 of Savitch and Carrano. You can also find a switch statement covered in Chapter 3.3. We have not discussed the switch statement, but it can be used in certain circumstances to make choices.


Problems

  1. [solve] The following Java if-statement is not correct. What is wrong with it?

      If(x == 0) y = 0;
    

  2. [solve] The following Java if-statement is not correct. What is wrong with it?

      if(x == 0) 
        y = 0;
        z = 1;
      else
        y = 1;
    

  3. [solve] The following Java if-statement is not correct. What is wrong with it?

      if x == 0
      {
         y = 1;
      }
      else 
      {
         y = x;
      }
    

  4. [solve] Suppose that variable x, of type double, already has a value. Write statements that create variable ax, of type double, and makes ax equal to the absolute value of x. (There is a function called Math.abs that does this, but do not use it for this question. Use an if-statement instead.)


Summary

Use an if-statement to make a decision while a program runs.

The idea behind a Java if-statement is the same as the idea behind a Cinnameg If-statement, but there are differences in how you write them.


Review

A compound statement is a sequence of zero or more statements enclosed in {...}. It is considered to be a single statement.

A compound statement is a context. Any variables that are created inside the compound statement are destroyed at the end of it.