Prev Main Next

Making Choices

You often find that you want to make a computer program ask a yes-or-no question, and to behave one way if the answer is yes, another if the answer is no.

This does not necessarily mean to ask the user a question. Instead, the question can be written in the program, and the answer is obtained without the user getting involved at all. A program might ask millions of questions while it runs. The user does not want to answer them.


If-else statements

An if-else statement has the form

  if(condition)
  {
    statements (group 1)
  } 
  else 
  {
    statements (group 2)
  }
where you put in appropriate things for the parts in red. The idea is to ask if the condition is true. If it is, then the first group of statements is performed, but not the second group. If the condition is false, then the second group of statements is done, but not the first.

Remember that Java has a very strict syntax. The parentheses around the condition are required. If you leave them out, you will get an error.

Warning. Do not put a semicolon at the end of the line that starts if. For example, do not write
  if(x > y);
  {
    w = 0;
  }
  else 
  {
    w = 1;
  }


Example

Suppose that you want to make variable m hold the larger of the values of variables x and y.

   int m, x, y;

   ... Here, get values of x and y somehow.

   if(x > y)
   {
     m = x;
   }
   else  
   {
     m = y;
   }


Shortened form of if-else statements

If you want to do just a single short statement when the condition is either true or false, then you can omit the braces and write it on the same line as if or else. For example, you can write

   int m, x, y;

   ... Here, get values of x and y somehow.

   if(x > y) m = x;
   else  m = y;
But only do this if it does not result in a line that is too long. If a line is more than 80 characters long, it is too long.


Omitting the else part

You can omit the word else and the statement (or compound statement) that follows it. If you omit the else part, then the program will only do something if the condition is true. For example,

  int x,y;
  x = 40;
  y = 100;
  if(x < 10) 
  {
    y = 0;
  }
  System.out.println(y);
prints 100, since y did not get changed.


Questions.

  1. What is wrong with the following if-else statement?

        if x < 0
        {
          y = 0;
        }
        else 
        {
          y = x;
        }
      
    Answer[10]

  2. What is wrong with the following if-else statement?

        If(x < 0)
        {
          y = 0;
        }
        else 
        {
          y = x;
        }
      
    Answer[20]

  3. What is the value of variable w after the following is performed?

         int w, x, y;
         x = 21;
         y = 3;
         if(x/y > 3)
         {
           w = y + 1;
         }
         else
         {
           w = 80;
         }
      
    Answer[11]


Multiple cases

When you have more than one case, you normally use the form

  if(condition1)
  {
    ...
  }
  else if(condition2)
  {
    ...
  }
  else if(condition3)
  {
    ...
  }
  else
  {
    ...
  }
That is, all but the first and last cases start with else if. Notice that you do not indent deeper and deeper as you go.


Prev Main Next