1. Technology

Conditional Statements


Hands using computer keyboard
John Lamb/Photographer's Choice RF/Getty Images

Conditional statements allow a program to execute a certain piece of code based on a decision. For example, say you want to convert some text entered in by a user to lowercase. You only want to execute the code to convert the text to lowercase if has entered in some text, if he hasn't it's better not to try and execute the code because it will just lead to a runtime error.

There are two main conditional statements used in Java:

  • the if..then..else statement - the if..then statement is good for simple decision. Essentially the statement is saying if a condition is true then do this. It can also be extended to do something else if the condition is false. For example, if you normally feed your cat after 5.00pm you could have a reminder that follows the logic - if the time is before 5.00pm do nothing else show an alert saying "feed the cat!". Read the if-then and if-then-else statements to find out more.
  • the switch statement - the switch statement is used if a number of options are possible. For example, say you want to display the lyric of the song Twelve Days of Christmas that goes with the number you're singing (e.g., two turtle doves). There are 12 options that lead to twelve different lyrics. Read Using the Switch Statement for Multiple Choices for more information on switch statements.


The following example shows how an if..then.else statement can be used to check to see if a user's input is null. If it is then the program shows a dialog telling the user off for not picking a number otherwise the switch statement will be executed. The switch statement just matches the number with the correct lyric for the song.

//Options for Numbers between 1 and 12 input dialog
//Note: The reason this is an array of Integer objects rather than
//just an int array is because JOptionPane's .showInputDialog method
//expects an object array for seletion values. 
Integer[] oneToTen = {1,2,3,4,5,6,7,8,9,10,11,12};
//Input dialog box allowing the user to pick a day of the week.
Integer numberPicked = (Integer)JOptionPane.showInputDialog(null, "Pick a Number:"
                , "Numbers between 1 and 12", JOptionPane.QUESTION_MESSAGE
                , null, oneToTen, oneToTen[0]);
//Use an if then else statement to see if the user picks a number.
//If numberPicked has a null value it means the input dialog box was closed 
//either by pressing cancel or by using the close 'x' button.
if (numberPicked == null)
   JOptionPane.showMessageDialog(null, "You didn't pick a number!");
else //executes because numberPicked has a value.
  String lyric = "";
  String day = "";
  //The int chosen is used as a conparison against the case 
  //statements. When a match is found the code in that case block will
  //be executed. If none of the cases match the default case statement
  //is triggered.
  switch (numberPicked)
     case 1:
        day = "first";
        lyric = "A partridge in a pear tree.";
     case 2:
        day = "second";
        lyric = "2 Turtle Doves";
     case 3:
        day = "third";
        lyric = "3 French Hens";
     case 4:
        day = "fourth";
        lyric = "4 Calling birds";
     case 5:
        day = "fifth";
        lyric = "5 Gold Rings";
     case 6:
        day = "sixth";
        lyric = "6 Geese-a-laying";
     case 7:
        day = "seventh";
        lyric = "7 Swans-a-Swimming";
     case 8:
         day = "eigth";
         lyric = "8 Maids-a-Milking";
     case 9:
         day = "nineth";
         lyric = "9 Ladies Dancing";
     case 10:
         day = "tenth";
         lyric = "10 Lords-a-Leaping";
     case 11:
         day = "eleventh";
         lyric = "11 Pipers Piping"; 
     case 12:
         day = "twelveth";
         lyric = "12 Drummers Drumming";
         day = "special day";
         lyric = "ferrari";
   JOptionPane.showMessageDialog(null, "On the " + day + " day of "
                    + "Christmas, my true love gave to me..\n" + lyric);


# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

  1. About.com
  2. Technology
  3. Java
  4. Java Glossary
  5. C
  6. Definition for the Java Term: Conditional Statements

©2014 About.com. All rights reserved.