The If-Else Statement

From CompSciWiki
Revision as of 13:12, 21 March 2007 by Umheurin (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Control Structures


Introduction

To further your knoledge on if-statments we will now add the else part to the if-statements. If-Else Statements add the element that if the conditions are false then "do" somthing else.

   

{{{Body}}}

The If-Else Statement

Sometimes it is not sufficient to simply tell the computer "If such-and-such condition is true do this". Somtimes you will want to say "If such-and-such is true do this, otherwise do that". A simple way of doing this would be to have a separate if-statement that will return the opposite value of the first:

if (number%2 == 0)
{
    System.out.println("The number is even");
}//if

if (number%2 !=0)
{
    System.out.println("The number is odd");
}//if

While this approach works, it is needlessly complicated; there is a better way of doing the same task, but with less work. The keyword else can be used to tell the computer do execute a different body of code if the condition of an if-statement returns false. We can rewrite the code from above to take advantage of this keyword like this:

if (number%2 == 0)
{
    System.out.println("The number is even");
}//if

else
{
    System.out.println("The number is odd");
}//else

If we step through this last example the computer will evaluate the condition number%2 == 0 and check the result. If the result is true the body of the if-statement will be executed, just like in the previous examples. If the result is false then the body of the else-statement is executed.

The keyword else and the corresponding body form what is called an else-block or else-statement. It is very important to note that an else-block is always associated with an if-block, and each if-block can only ever be associated with zero or one else-blocks.

Together the keywords if, else, the condition, and the two bodies form a code-block known as an if-else block or if-else statement.


Anatomy of the If-Else Statement

The if-else statement is made up of the following parts:

  • the keyword if
  • the condition
  • the body of the if-statement
  • the keyword else
  • the body of the else-statement

The general form of an if-else statement is:

if (condition)
{
    code to execute if condition == true
}

else
{
    code to execute if condition == false
}


If-Else Ladders

Now that you know that if statements be be connected using an else, we can take that one step further and throw else-if statements into the mix.

The following program asks the user for two sports scores and tells the user which team won or if the game was a draw. The key components to pay attention to are lines 07 to 14.

00 public static void main(String[] args)
01 {
02    int scoreA, scoreB;
03    
04    scoreA = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Team A's score"));
05    scoreB = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Team B's score"));
06
07    if(scoreA > scoreB)
08    {
09        System.out.println("Team A won");
10    }//if
11
12    else if(scoreA < scoreB)
13    {
14        System.out.println("Team B won");
15    }//else if
16
17    else
18    {
19        System.out.println("The game was a draw");
20    }//else
21 }//main