Conditional statements are decision making structures. They allow your execution control to follow a certain path if a condition is met and an alternate path if not. The alternate path specified when condition is not met may further branch based on additional conditions. Java has if-then, if-then-else and switch conditional statements. These statements can be nested among each other. Meaning, there can be conditional statements embedded inside other conditional statements.
General Form a Conditional Statement
Conditional statement, be it if-else or switch follow the following general structure –
if-then Statement
- Is the most basic of all conditional statements
- Allows you to execute a code block if a condition is true
- In its simplest form the if-then statement does not say what to do when condition is false
Consider the code below Java
1234567891011121314 | public class Politician { // should not be part of Politician class // but this information is available to // politician, thanks to corruption boolean copsChasing = false; public void rob() { // vanilla if-then statement if (copsChasing) { evadeJustice(); }… |
The if statement introduces a code block which is delimited by braces – { and }.
if-then-else Statement
if-then-else is a if-then statement where you also specify what happens if a condition is false. Consider the code below –Java
12345678910111213141516 | public class Politician { // should not be part of Politician class // but this information is available to // politician, thanks to corruption boolean copsChasing = false; public void rob() { // if-then-else statement if (copsChasing) { evadeJustice(); } else { // Java convention is to place else on same line as the brace robHarder(); }… |
Here we are able to specify code to execute when the condition of cops Chasing is false.
switch Statement
The if-then statement specifies one alternate path. The if-then-else path specifies two alternate paths. A switch statement allows you to specify multiple paths.
A switch statement is a way of specifying a code block to be executed when a variable has a particular value. The variable in question can be of type integer (i.e. int, short, byte, long) or String or Number.
Consider the code below. We have a String variable called person. We are able to execute different pieces code depending on the value of the variable person.
1234567891011 | // in some driver class String aPerson = "Paul" ; String anotherPerson = "Peter" ; aPolitician.rob(aPerson); aPolitician.rob(anotherPerson); aPolitician.rob( "someone" ); |
And in the Politician class
12345678910111213141516171819202122 | public void rob(String person) { switch (person) { case "Paul" : System.out.println( "Robbing Paul to pay myself" ); break ; case "Peter" : System.out.println( "Robbing Peter to pay myself" ); break ; case default : System.out.println( "Robbing you to pay myself" ); break ; } |
In the code above we specify many alternate paths for code execution to follow based on the value of variable person. Note that you need to use break statement religiously for each case. If you omit the break statement the code below the case (presumably of other cases) keeps also gets executed.
The ‘default’ case in the code is a catch-all clause. If none of the other cases get executed, the default case does. This allows you to specify the behavior when the switch variable does not have one of the expected values.