In Java (and indeed all programming) a key feature is to be able to control the order in which the statements you write in your code are executed. This is achieved through conditional statements.
Before looking at conditional statements, it's worth visiting some more common operators. These operators are those that operate on boolean values, and produce a boolean result. The three most common are:
&&is the "and" operator||is the "or" operator!is the "not" operator
The "and" operator returns true only if both of its inputs were true:
boolean firstAnd = (1 == 1) && (2 == 2); // This is true
boolean secondAnd = (1 == 1) && (2 == 3); // This is falseThe "or" operator returns true if either of its inputs is true.
boolean firstOr = (1 == 1) || (2 == 3); // This is true
boolean secondOr = (1 == 2) || (3 == 4); //This is falseThe "not" operator turns true into false and false into true:
boolean firstNot = !(1 == 1); // This is false
boolean secondNot = !(1 == 2); // This is trueThe first and most important way you control the flow of your program is through conditional statements. Conditional statements execute different pieces of code depending on the result of a conditional expression.
The most common conditional statement is the "if" statement. In Java, it consists of three parts:
- The
ifkeyword - A boolean expression, contained in parentheses
- A block of code, inside curly braces, that is executed if the boolean expression is true.
if (9 > 2) {
System.out.println("This will be displayed");
}
if (3 > 5) {
System.out.println("This will not be displyed");
}Sometimes, if the condition of your "if" statement is not true, you want to do something else instead. You can achieve this using an augmentation of the "if" statement called "else".
-
If the boolean expression of the "if" statement evaluates to
true, then the block of code after the "if" is executed. -
Otherwise, if the boolean expression evaluates to
false, then the block of code after the "else" is executed.
if (1 == 2) {
System.out.println("1 is equal to 2");
} else {
System.out.println("1 is not equal to 2");
}In some cases, we need to execute a separate block of code depending on multiple different boolean expressions. In that case, you can use the "if-elseif-else" statement.
-
If the expression after the "if" evaluates to
true, then it will run the block of code that immediately follows. -
Otherwise, if the expression after the "else if" evaluates to
true, then it will run the block of code directly after that expression. -
Finally, if none of the previous boolean expressions evaulate to
true, then the block of code in the "else" block will run.
int myScore = 2;
int yourScore = 3;
if (myScore > yourScore) {
System.out.println("I beat you!");
} else if (myScore == yourScore) {
System.out.println("We drew");
} else {
System.out.println("You beat me!");
}Java contains a special syntax that can be shorter than an "if" statement in certain cases. it is called the "ternary" conditional. It consists of three parts:
- A boolean expression, followed by a question mark.
- A first alternative result that will be used if the condition is true.
- A second alternative result that will be used if the condition is false.
The two alternatives are separated by a colon (:);
int pointsScored = 19;
char gameResult = (pointsScore > 20) ? 'W' : 'L';In the above code block, the boolean expression evaulates to false, so the second of the two alternatives ('L') is used as the result of the expression.