To create a robust and error-free program we need to perform an action based on the certain conditions. In Java, this can be achieved using If and Switch statements. These statements are called conditional statements in JAVA.
If-else Statement
Let’s understand first If statement. An if statement is used to take a decision based on the boolean values true and false. If a syntax or combination of syntax returns a true value, only then all the statement inside the if block gets executed. Otherwise Else block get executed.
if(condition)
{
Statement(s)….
}
else
{
Statement(s)….
}

Code Example:
public class IfElse {
public static void main(String[] args)
{
int result=1;
if(result==1) // Executes when condition is True.
{
System.out.println("Value of result: "+result);
}
else // Executes when condition is False.
{
System.out.println("Value of result not equal to 1.");
}
}
}
Related Links:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
If-else if-else ladder
Sometimes we have to take a decision between multiple conditions. In that case, we have to use if-else if-else ladder.
Code Example:
public class Ifelse { public static void main(String[] args) { int result=1; if(result==1) { System.out.println("Value of result: "+result); } else if(result==2) { System.out.println("Value of result: "+result); } else if(result==3) { System.out.println("Value of result: "+result); } else { System.out.println("Invalid Value"); } } }
Switch-case
Alternate of if-else statement is Switch-case. The functionality will remain same, the key difference is only that if-else statement works on the boolean value. The condition can be simple or complex.
if(result > 0 && result <10)
On the other hand switch-case statement used to take a decision between multiple options based on the variable value only like a numeric value, character value or string value.
Code Example using numeric variables:
public class SwitchCase { public static void main(String[] args) { int result=3; switch(result) { case 1: // Executes if value of result is 1. { System.out.println("Value of result equal to 1."); } break; // Break statement used to exit from swtich loop once any condition satisfied. case 2: // Executes if value of result is 2. { System.out.println("Value of result equal to 2."); } break; case 3: // Executes if value of result is 3. { System.out.println("Value of result equal to 3."); } break; default: { System.out.println("If all the above conditions get failed."); } } } }
Code Example using character variable:
public class SwitchCase2 { public static void main(String[] args) { char result='b'; switch(result) { case 'a': { System.out.println("Value of result equal to a."); } break; case 'b': { System.out.println("Value of result equal to b."); } break; case 'c': { System.out.println("Value of result equal to c."); } break; default: { System.out.println("If all the above conditions get failed."); } } } }
Code Example using string variable:
public class SwitchCase3 { public static void main(String[] args) { String result="third"; switch(result) { case "first": { System.out.println("Value of result equal to 1."); } break; case "second": { System.out.println("Value of result equal to 2."); } break; case "third": { System.out.println("Value of result equal to 3."); } break; default: { System.out.println("If all the above conditions get failed."); } } } }