Difference between revisions of "The If-Else Statement"

From CompSciWiki
Jump to: navigation, search
(Changed to CodeBlock template)
Line 11: Line 11:
 
Sometimes it is not sufficient to simply tell the computer "If such-and-such condition is true do this".  Sometimes 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:
 
Sometimes it is not sufficient to simply tell the computer "If such-and-such condition is true do this".  Sometimes 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:
  
<pre>
+
{{CodeBlock
if (number%2 == 0)
+
|Code=
{
+
if (number%2 == 0)
    System.out.println("The number is even");
+
{
}//if
+
    System.out.println("The number is even");
 +
}//if
  
if (number%2 !=0)
+
if (number%2 !=0)
{
+
{
    System.out.println("The number is odd");
+
    System.out.println("The number is odd");
}//if
+
}//if
</pre>
+
}}
  
 
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 to 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:
 
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 to 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:
  
<pre>
+
{{CodeBlock
if (number%2 == 0)
+
|Code=
{
+
if (number%2 == 0)
    System.out.println("The number is even");
+
{
}//if
+
    System.out.println("The number is even");
 +
}//if
  
else
+
else
{
+
{
    System.out.println("The number is odd");
+
    System.out.println("The number is odd");
}//else
+
}//else
</pre>
+
}}
  
 
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.
 
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.
Line 53: Line 55:
  
 
The general form of an if-else statement is:
 
The general form of an if-else statement is:
<pre>
+
{{CodeBlock
if (condition)
+
|Code=
{
+
if (condition)
    code to execute if condition == true
+
{
}
+
    code to execute if condition == true
 +
}
  
else
+
else
{
+
{
    code to execute if condition == false
+
    code to execute if condition == false
}
+
}
</pre>
+
  
 +
}}
  
 
==If-Else Ladders==
 
==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. We use ''else-if'' statements to create long ladders of ''if'' statements. Generally, whenever you have a bunch of ''if'' statements that are related to each other, you combine them into one big ladder of statements. This can also prevent certain problems from occurring. Let us begin by presenting a simple problem below.
 
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. We use ''else-if'' statements to create long ladders of ''if'' statements. Generally, whenever you have a bunch of ''if'' statements that are related to each other, you combine them into one big ladder of statements. This can also prevent certain problems from occurring. Let us begin by presenting a simple problem below.
  
<PRE>
+
{{CodeBlock
 +
|Code=
 +
int x = 5;
  
int x = 5;
+
if (x > 4)
 +
{
 +
  System.out.println("x is greater than 4");
 +
}
  
if (x > 4)
+
if (x > 2)
{
+
{
  System.out.println("x is greater than 4");
+
  System.out.println("x is greater than 2 but less than 5");
}
+
}
  
if (x > 2)
+
}}
{
+
  System.out.println("x is greater than 2 but less than 5");
+
}
+
 
+
</PRE>
+
  
 
In this case both lines will be printed because '''x''' is greater than 2 and 4. We only want to print out "x is greater than 4". We can use an ''if-else'' statement to fix this problem. Here is the corrected code:
 
In this case both lines will be printed because '''x''' is greater than 2 and 4. We only want to print out "x is greater than 4". We can use an ''if-else'' statement to fix this problem. Here is the corrected code:
  
<PRE>
+
{{CodeBlock
 +
|Code=
 +
int x = 5;
  
int x = 5;
+
if (x > 4)
 +
{
 +
  System.out.println("x is greater than 4");
 +
}
 +
else if (x > 2)
 +
{
 +
  System.out.println("x is greater than 2 but less than 5");
 +
}
  
if (x > 4)
+
}}
{
+
  System.out.println("x is greater than 4");
+
}
+
else if (x > 2)
+
{
+
  System.out.println("x is greater than 2 but less than 5");
+
}
+
 
+
</PRE>
+
 
    
 
    
 
The addition of the ''else-if'' statement has solved the problem at hand. Now the correct output will be printed regardless of the value of '''x'''.
 
The addition of the ''else-if'' statement has solved the problem at hand. Now the correct output will be printed regardless of the value of '''x'''.
Line 108: Line 111:
 
This 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'''.
 
This 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'''.
  
<pre>
+
{{CodeBlock
 +
|Code=
 +
 
 
   int scoreA = 25;
 
   int scoreA = 25;
 
   int scoreB = 30;
 
   int scoreB = 30;
Line 126: Line 131:
 
       System.out.println("The game was a draw");
 
       System.out.println("The game was a draw");
 
   }//else
 
   }//else
</pre>
+
}}
  
 
The preceding code may also be written as:  
 
The preceding code may also be written as:  
  
<pre>
+
{{CodeBlock
 +
|Code=
 +
 
 
   int scoreA = 25;
 
   int scoreA = 25;
 
   int scoreB = 30;
 
   int scoreB = 30;
Line 140: Line 147:
 
   else
 
   else
 
       System.out.println("The game was a draw");
 
       System.out.println("The game was a draw");
</pre>
+
}}
 
}}
 
}}

Revision as of 02:10, 5 December 2011

COMP 1010 Home > Control Stuctures


Introduction

The If-Else statement adds further functionality to the If-Statement discussed in the previous section. If-Else Statements add the ability to tell the computer what to do if the condition tested in the if-statement returns a value of false.

The If-Else Statement

Sometimes it is not sufficient to simply tell the computer "If such-and-such condition is true do this". Sometimes 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 to 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. We use else-if statements to create long ladders of if statements. Generally, whenever you have a bunch of if statements that are related to each other, you combine them into one big ladder of statements. This can also prevent certain problems from occurring. Let us begin by presenting a simple problem below.

 int x = 5;

 if (x > 4)
 {
   System.out.println("x is greater than 4");
 }

 if (x > 2)
 {
   System.out.println("x is greater than 2 but less than 5");
 } 

In this case both lines will be printed because x is greater than 2 and 4. We only want to print out "x is greater than 4". We can use an if-else statement to fix this problem. Here is the corrected code:

 int x = 5;

 if (x > 4)
 {
   System.out.println("x is greater than 4");
 }
 else if (x > 2)
 {
   System.out.println("x is greater than 2 but less than 5");
 } 

The addition of the else-if statement has solved the problem at hand. Now the correct output will be printed regardless of the value of x.

You are allowed to have as many else-if statements as you want. The only requirement is that the start of the ladder begins with an if statement. You are also allowed to use else statements. The requirement for else statements is that you are only allowed 1 and it must be at the very end of the ladder. The next program will display a full blown if - else if - else ladder.

This 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.

 int scoreA = 25;
  int scoreB = 30;

  if(scoreA > scoreB)
  {
      System.out.println("Team A won");
  }//if

  else if(scoreA < scoreB)
  {
     System.out.println("Team B won");
  }//else if

  else
  {
      System.out.println("The game was a draw");
  }//else 

The preceding code may also be written as:

 int scoreA = 25;
  int scoreB = 30;

  if(scoreA > scoreB)
      System.out.println("Team A won");
  else if(scoreA < scoreB)
     System.out.println("Team B won");
  else
      System.out.println("The game was a draw"); 
Previous Page: The If Statement Next Page: Conditions