Java conditional logic
The conditions in a program are not always the same. For example, when different users enter different data, how would you react to it accordingly? You need to be able to perform different actions based on certain conditions in a program. You can do this in Java using conditional logic.
This tutorial focuses on:
- The if statement
- The else statement
- The else-if statement
- Using if, else, and else if together
- The switch statement
- The ternary operator
The if statement
The if statement will execute some code if a condition is true.
Syntax:if(condition){
perform this action;
}
Example:int aNumber = 5;
//check if aNumber equals 5 and if it is print a message
if (aNumber == 5){
System.out.print("aNumber is equal to 5");
}
Output:aNumber is equal to 5
NOTE: Use two equal signs (==) in the condition when comparing values. One equal sign (=) is used to assign values, while two equal signs (==) are used to compare values.
The else statement
The if statement executes some code if a condition is true, but what if the condition is false? The else statement will execute some code if the condition in the if statement is false.
Syntax:if(condition){
perform this action;
}
else{
perform this action if the above condition is false;
}
Example:int aNumber = 10;
//check if aNumber equals 5 and if it is print a message
if (aNumber == 5){
System.out.print("aNumber is equal to 5");
}
//otherwise print a different message
else{
System.out.println("aNumber equals a number other than 5");
}
Output:aNumber equals a number other than 5
The else-if statement
Any condition can be only true or false, but what if you needed to test a variable for more than one value? This is where the else-if statement comes in. The else-if statement performs an action if the variable in the if statement is another value specified in the else- if statement itself.
Syntax:if(variable equals one value){
perform this action;
}
else if(variable equals another value){
perform this action;
}
Example:int aNumber = 7;
if (aNumber == 5){
System.out.print("aNumber is equal to 5");
}
else if (aNumber == 7){
System.out.print("aNumber is equal to 7");
}
Output:aNumber is equal to 7
Using if, else, and else if together
You can use the if, else, and else if statements together when you want to check a variable for a certain value many times. If it is not any of the checked values then the code specified by the else statement will be executed.
Syntax:if(variable equals one value){
perform this action;
}
else if(variable equals another value){
perform this action;
}
else if(variable equals another value){
perform this action;
}
else{
if the variable was not equal to any of the values tested in
the if statement and all the else if statements, then perform
this action;
}
Example:int aNumber = 5;
if (aNumber == 9){
System.out.print("aNumber is equal to 9");
}
else if (aNumber == 7){
System.out.print("aNumber is equal to 7");
}
else if (aNumber == 3){
System.out.print("aNumber is equal to 3");
}
else if (aNumber == 15){
System.out.print("aNumber is equal to 15");
}
else {
System.out.print("aNumber is not equal to 9, 7, 3,
or 15. aNumber is equal to " + aNumber);
}
Output:aNumber is not equal to 9, 7, 3, or 15. aNumber is equal to 5
The switch statement
The switch statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, else-if, else structure. There is an important keyword used within the switch structure, and that keyword is the break keyword. The break keyword is used to make sure that the switch structure will not fall through to the next possible value.
Syntax:switch(variable){
case possible value:
perform this action;
break;
case possible value:
perform this action;
break;
case possible value:
perform this action;
break;
default:
perform this action if none of the values match;
}
Example:int aNumber = 7;
switch(aNumber){
//test if aNumber equals one of a set of values
//and if it is print a message
case 1:
System.out.print("aNumber is equal to 1");
break;
case 2:
System.out.print("aNumber is equal to 2");
break;
case 3:
System.out.print("aNumber is equal to 3");
break;
case 7:
System.out.print("aNumber is equal to 7");
break;
//test if aNumber equals an unspecified value
//and if it is print a message
default:
System.out.print("aNumber is not equal to any
of the values specified");
}
Output:aNumber is equal to 7
The ternary operator
The ternary operator is the question mark symbol (?), it works the same way as the if-else structure, the coding is just different.
Syntax:variable = (condition) ? value the variable will take if condition is true : value the
variable will take if condition is false
Example:int aNumber;
int anotherNumber = 7;
//check if anotherNumber equals 10 and if it is
//aNumber takes the value of 5
//otherwise aNumber takes the value of 15
aNumber = (anotherNumber==10) ? 5: 15;
System.out.print("aNumber = " + aNumber);