Java Conditional statements
Java has the following conditional statements:
Use
if
to specify a block of code to be executed if a specified condition is true.Use
else
to specify a block of code to be executed, if the same condition is false.Use
else if
to specify a new condition to test, if the first condition is false.Use
switch
to specify many alternative blocks of code to be executed.
The if Statement
Use the if
statement to specify a block of Java code to be executed if a condition is true
.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example:
if (20 > 18) {
System.out.println("20 is greater than 18");
}
The If-else Statement
Use the else
statement to specify a block of code to be executed if the condition is false
.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example:
int age = 20;
if (age < 18) {
System.out.println("Eligible to vote");
} else {
System.out.println("Not eligibile to vote");
}
The else-if Statement
Use the else if
statement to specify a new condition if the first condition is false
.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example:
int age = 13;
if (age < 13) {
System.out.println("Child");
} else if (19 <= age) && (age >= 13) {
System.out.println("Teenager");
} else {
System.out.println("Adult");
}
// Outputs "Teenager"
Short-Hand If..Else
There is also a short-hand if-else, which is known as the ternary operator because it consists of three operands.
It can be used to replace multiple lines of code with a single line, and is most often used to replace simple if-else statements:
Syntax:
variable = (condition) ? expressionTrue : expressionFalse;
Example:
int age = 20;
String result = (age > 18) ? "Eligible to vote" : "Not eligible to vote";
System.out.println(result);
// Outputs "Eligible to vote"
The Switch Statement
Instead of writing many if..else
statements, you can use the switch
statement.
The switch
statement selects one of many code blocks to be executed:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
The
switch
expression is evaluated once.The value of the expression is compared with the values of each
case
.If there is a match, the associated block of code is executed.
The
break
anddefault
keywords are optional, and will be described later in this chapter
The example below uses the weekday number to calculate the weekday name:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example:
int day = 4;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday"
The break keyword
When Java reaches a break
keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
Note: A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.
The default keyword
The default
keyword specifies some code to run if there is no case match:
Example
int day = 4;
switch (day) {
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"
Subscribe to my newsletter
Read articles from Naman Garg directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by