Conditional statements are used to break the flow of execution of the code from top to bottom and allows the program to conditionally execute a particular piece of code.
Java has the following conditional statements
If statement
It executes a block of code only if the condition is true
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Program:
public class Usecase {
public static void main(String[] args) {
int x = 100;
int y = 50;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
if-else Statement
If else statement specifies a set of code to execute if the if 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
}
Program:
public class Usecase {
public static void main(String[] args) {
int x = 50;
int y = 500;
if (x > y) {
System.out.println("x is greater than y");
} else {
System.out.println("y is greater than x");
}
}
}
if-else-if statement
if-else-if statement is used 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
}
Program:
public class Usecase {
public static void main(String[] args) {
int x = 500;
int y = 500;
if (x > y) {
System.out.println("x is greater than y");
} else if (y < x) {
System.out.println("y is greater than x");
} else {
System.out.println("both are equal");
}
}
}
Switch statement
- Switch is used to specify many alternative blocks of code to be executed.
- Break keyword is used to break the switch block and avoid execution of rest of the code
- Using a string-based switch is an improvement over using the equivalent sequence of if/else statements
- The comparison of String objects in switch statements is case sensitive.
- It is best to switch on strings only in cases where the controlling data is already in string form and it is not null
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Program:
public class Usecase {
public static void main(String[] args) {
int x = 2;
switch (x) {
case 1:
System.out.println("value of x is 1");
break;
case 2:
System.out.println("value of x is 2");
break;
default:
System.out.println("value of x is 0");
}
}
}
In the above code, the value of x is 2 so the second case is executed. If all the statement fails in the switch statement the default block is executed
The below use case demonstrates the string-based switch case
public class Sample {
public static void main(String[] args) {
String str = "two";
switch (str) {
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
}
}
}
Output:
two