Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java if else if else Control Statement

A computer program is a list of instructions for the computer to follow. Some instructions tell the computer to test a variable and to follow a group of instructions based on the result of that test. This type of instruction is called a conditional test. The if else statement tells the computer to run one set of instructions based on one result of a test, and a different set of instructions based on a different result of the test.

import javax.swing.*;

public class ifelseCont 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Java ifelse Test");

      int height = 74;
      String tall = "";

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);

      if(height < 72)
      {
          tall = "Your height is too short.";         
      }
      else
      {
          tall = "Your height is tall enough.";         
      }

      myText.append(tall);
      frame.add(myText);

      frame.setVisible(true);
   }
}

In the code shown above, if height is less-than 72, the code within the brackets just below the line if(height < 72) are executed, else (otherwise), if the height is 72 or greater, the code within the brackets just below the line else are executed.

But what if you want to test for more posibilities than just two? In that case you can use a if else if else statement as shown below.

import javax.swing.*;

public class ifelseCont2 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Java ifelse Test");

      int height = 74;
      String tall = "";

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);

      if(height < 72)
      {
          tall = "Your height is too short.";        
      }
      else if(height > 62 && height < 82)
      {
          tall = "Your height is just right.";         
      }
      else
      {
	  tall = "Your height is too tall.";
      }

      myText.append(tall);
      frame.add(myText);

      frame.setVisible(true);
   }
}

In this code if height is less-than 72, the code within the brackets just below the line if(height < 72) are executed (the variable tall is set to "Your height is too short.").

If height is greater than 62, and less than 82, the the code within the brackets just below the line else if(height > 62 || height < 82) are executed, (tall is set to "Your height is just right.").

If height is equal-to or greater than 82, the the code within the brackets just below the line else are executed (tall is set to "Your height is too tall.").

The nice thing about an if else if else statement is that you can use as many else if sections as you require. You could have an else if section for every possible height value.

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2018 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268