Conditional Statements in Java



Conditional Statements in Java

  • Conditional statements in a computer program support decisions based on a certain condition.
  • For example, perhaps you want to convert some user-entered text to lowercase.
  • You want to execute the code only if the user entered some text; if he hasn't, don't execute the code because it will just lead to a runtime error.
  • There are two main conditional statements used in Java the if-then and if-then-else statements and the switch statement.
 Conditional Statement in Java

Learn Java - Java tutorial - Conditional Statement in Java - Java examples - Java programs

If-Then and If-Then-Else Statements in Java

  • The basic flow control statement in Java is if-then: if [something] is true, do [something]. This statement is a good choice for simple decisions.
  • The basic structure of an if statement starts with the word "if", followed by the statement to test, followed by curly braces that wrap the action to take if the statement is true.
  • It looks very much like it seems it would:
if ( Statement ) {
   // do something here....
}
  • This statement can also be extended to do something else if the condition is false:
if ( statement ) {  
   // do something here...
}
else {    
   // do something else...
}

Example:

  • If you are determining whether someone is old enough to drive, you might have a statement that says "if your age is 16 or older, you can drive; else, you cannot drive."
int age = 17;
if age >= 16 {
    System.out.println("You can drive.");
}
else  {
    System.out.println("You are not old enough to drive.");
}
  • There is no limit to the number of else statements you can add.

Conditional Operators in Java

  • In the example above, we used a single operator: >= i.e. "great than or equal to." These are the standard operators you can use:
    • equal to: =
    • less than: <
    • more than: >
    • greater than or equal to: >=
    • less than or equal to: >=
  • In addition to these, there are four more used with conditional statements:
    • and: &&
    • not: !
    • or: ||
    • is equal to: ==
  • For example, perhaps driving age is considered to be from age 16 to age 85, in which case we could use the AND operator:
else if ( age > 16 && age < 85 )
  • This will return true only if both conditions are met. The operators NOT, OR, and IS EQUAL TO can be used similarly.

Switch Statement in Java

  • The switch statement provides an effective way to deal with a section of code that could branch in multiple directions based on a single variable.
  • It does not support the conditional operators that the if-then statement does, nor can it handle multiple variables.
  • It is, however, a preferable choice when the condition will be met by a single variable, because it can improve performance and is easier to maintain.

Here's an example:

switch ( single_variable ) {
   case value: 
      //code_here;
      break;
   case value: 
      //code_here;
      break;
   default:
      //set a default;
}
  • You start with switch, provide a single variable and then set out your choices using the term case. The keyword break completes each case of the switch statement. The default value is optional but good practice. For example, this switch prints the lyric of the song Twelve Days of Christmas given a provided day:
int day = 5;
String lyric = "";  // empty string to hold the lyric
switch (day) {
   case 1:
           lyric = "A partridge in a pear tree.";
           break;
   case 2:
           lyric = "2 Turtle Doves";
           break;
   case 3:
           lyric = "3 French Hens";
           break;
   case 4:
           lyric = "4 Calling birds";
           break;
   case 5:
           lyric = "5 Gold Rings";
           break;
   case 6:
           lyric = "6 Geese-a-laying";
           break;
   case 7:
           lyric = "7 Swans-a-Swimming";
           break;
   case 8: 
           lyric = "8 Maids-a-Milking";
           break;
   case 9: 
           lyric = "9 Ladies Dancing";
           break;
   case 10: 
           lyric = "10 Lords-a-Leaping";
           break;
   case 11:
           lyric = "11 Pipers Piping"; 
           break;
   case 12:
           lyric = "12 Drummers Drumming";
           break;
   default:
           lyric = "There are only 12 days.";
           break;
}
System.out.println(lyric);
  • In this example, the value to test is an integer. Java SE 7 and later support a String object in the expression.
String day = "second";
String lyric = "";  // empty string to hold the lyric
switch (day) {
   case "first":
           lyric = "A partridge in a pear tree.";
           break;
   case "second":
           lyric = "2 Turtle Doves";
           break;
   case "third":
           lyric = "3 French Hens";
           break;
   // etc.  

Related Searches to Conditional Statements in Java