For this post I would like to discuss Conditionals in Java
A “Conditional Statement” allows us to execute different blocks of code based on some condition. With this we will begin to be able to make more complex “control flows” with our code.
If and Else Statements
The most straightforward of these is something called an “If Statement.” If some condition is true, then execute some block of code
if (condition) {
// Then execute this block of code
}
We can also add in an “Else Statement,” which will be executed if the condition of prior if statement is false
if (condition) {
// Execute this code if the condition is true
} else {
// Execute this code if the condition is false
}
There is also something called an “Else If Statement” which allows us to specify further additional conditions. It is possible to have multiple “Else If Statements” within an “If block.”
if (condition1) {
// Execute this code if condition1 is true
} else if (condition2) {
// Execute this code if condition1 is false and condition2 is true
} else {
// Execute this code if condition1 and condition2 are false
}
Ternary Operator
We talked a lot about Operators in our last section but there one more we can talk about here.
The ternary operator can be thought of as shorthand for an if else statement to assign variables.
variable = condition
? // Set to this value if the condition is true
: // Set to this value if the condition is false
Or to provide a more concrete example, lets say we had an if statement such as:
String helloWorld = "Hello World!";
String fooBar = "Foo Bar";
String longerWord;
if (helloWorld.length() > fooBar.length()) {
longerWord = helloWorld;
} else {
longerWord = fooBar;
}
We could use the ternary operator in this case to write this more concisely:
String helloWorld = "Hello World!";
String fooBar = "Foo Bar";
String longerWord = helloWorld.length() > fooBar.length()
? helloWorld
: fooBar;
Multiple If Blocks
We can also use multiple if blocks sequentially.
if (condition1) {
// Execute this code if condition1 is true
}
if (condition2) {
// Execute this code if condition2 is true
} else {
// Execute this code if condition2 is false
}
Nested If
We can also put if statements inside other if statements to create a “nested” if
if (condition1) {
if (condition2) {
// Execute this code if condition1 and condition2 are true
} else {
// Execute this code if condition1 is true and condition2 is false
}
} else {
// Execute this code if condition1 is false
}
Switch Statement
Switch Statements provide another option for conditional logic. To be perfectly honest, I find myself using these very rarely but I am including them here because there are instances where they make sense. For example, if you had high number of cases to check then a Switch statement would perform faster than a large if – else if chain.
A if block such as this
String romanNumeral = "X";
if (romanNumeral.equals("I")) {
System.out.println("I is a 1 in roman numeral");
} else if (romanNumeral.equals("V")) {
System.out.println("V is a 5 in roman numeral");
} else if (romanNumeral.equals("X")) {
System.out.println("X is a 10 in roman numeral");
} else if (romanNumeral.equals("L")) {
System.out.println("L is a 50 in roman numeral");
} // and so on...
We could potentially rewrite this as a switch statement to look something like this
String romanNumeral = "X";
switch (romanNumeral) {
case ("I"):
System.out.println("I is a 1 in roman numeral");
break;
case ("V"):
System.out.println("V is a 5 in roman numeral");
break;
case ("X"):
System.out.println("X is a 10 in roman numeral");
break;
case ("L"):
System.out.println("L is a 50 in roman numeral");
break;
// and so on...
}
In this case we are also using the break
keyword so that we do not process the remaining case statements after we matched one. If we did not include this then the switch statement would “fall through” and continue to process the other cases.
Now you have a good idea of how to use conditional statements in Java!