Tutorials Hut

Java Conditions - If Else and Switch Case

Java if else and switch cases are features provided by java to make decisions and branch our code or logic based on requirement, for example if based on certain decisions there are two different tasks that can be executed in that case we can use java conditions.
They evaluate the statement if it holds true to execute the code which belongs to true else move to else or next condition.

Java if else diagram:

java if else
Java if else

 

 

Types of java conditions

  • If else based java conditions
  •  Switch case based java conditions

Java If Else Based Conditions

By using if and else key words we can do branching in our code or logic, simple example of if else statement-

Example:

public class Conditions {

public static void main(String[] args) {

//This is an example of if else where if part will execute
if(5 < 10) {
System.out.println("we are in if block, condition 5 < 10 is true");
}
else {
System.out.println("we are in else block, condition is false");
}

//This is an example of if else where else part will execute
if(15 < 10) {
System.out.println("we are in if block, condition is true");
}
else {
System.out.println("we are in else block, condition is 15 < 10 is false");
}
}
}

Output:

we are in if block, condition 5 < 10 is true
we are in else block, condition is 15 < 10 is false

There are 3 different variations of if else

  • If statement – We can simply use it if certain condition is true to do something
  • Else statement – We use this with if in case when if is not true and hence else block gets executed.
  • Else If statement – We can do this with an if condition and when false code will come to this statement to check another condition and if this condition is true will execute the code.

If Statement

If statement evaluates the condition and if true will execute the block belongs to if condition.

Syntax:

if(condition which returns boolean) {
 //Code to be execute if condition is true
}

Example:

 

public class IfElseConditions {

public static void main(String[] args) {
if(1<2) {
System.out.println("if condition is true 1<2 ");
}
if(12.3 < 100.9) {
System.out.println("if condition is true 12.3 < 100.9");
}
}
}

Output:

if condition is true 1<2
if condition is true 12.3 < 100.9

Else Statement

Else statement is always used with if or else if and if, if condition is false this will be executed.

Syntax:

if(condition which returns false) {
//Code to be execute if condition is true
}
else {
 //Code will execute if condition is false
}

Example:

public class IfElseConditions {
public static void main(String[] args) {
  if(10<2) {
        System.out.println("if condition is true in if ");
    }
 else {
        System.out.println("if was false, now in else for condition 10<2");
     }
}
}

Output:

if was false, now in else for condition 10<2

Else If

Else if statement is always  used with any if or else if condition and when if condition is false this block will execute.

Syntax:

if(condition which returns false) {
//Code to be execute if condition is true
}
else if (condition which returns false) {
 //Code will execute if condition is false
}

Example:

public class IfElseConditions {
public static void main(String[] args) {
    int a = 10;
    if(a > 20) {
        System.out.println("if condition is true, in if ");
    }
    else if(a > 5 ){
        System.out.println("else if is true so in else if condition");
    }
}
}

Output:

 else if is true so in else if condition

Switch Case 

In java we have switch case conditions which are similar to multiple if else blocks, in case based conditions we try to match output with certain values and if it matches then that block will be executed else it will go to the next case to match and so on, if nothing matches it will end.

We use break keyword with if case block to avoid execution of next cases in case of match, also we use default case in situations where there is no match.

Syntax of switch case:

switch(Expression) {
case 1:
//some code
case 2:
//some code
.
.
case n :
//some code
case default:
//some code
}

Examples of switch case:

public class Conditions {

public static void main(String[] args) {
    int caseNumber = 2;

//case with break
    switch(caseNumber) {
         case 1:
            System.out.println("in case 1, with break");
            break;
        case 2:
            System.out.println("in case 2, with break");
            break;
        default: System.out.println("in default, with break");
     }

    //Case without break
    switch(caseNumber) {
        case 1:
            System.out.println("in case 1, no break used");
        case 2:
            System.out.println("in case 2, no break used");
        default: System.out.println("in default, no break used");
    }
    //Case without default, this will give no output
    int newCaseNum = 30;
    switch(newCaseNum) {
        case 1:
            System.out.println("in case 1, no break used");
        case 2:
            System.out.println("in case 2, no break used");
    }
}
}

Output of above switch case:

in case 2, with break
in case 2, no break used
in default, no break used

Java If Else Using Ternary Operator

There is a short way to use if else which is very handy for simple condition checks.

Syntax:

Result variable to hold o/p = (Condition) ? if true do this : else do this;

Example:

public class IfElseConditions {

public static void main(String[] args) {
   String result = (10 < 20)? "ternary condition true" : "ternary condition false";
    System.out.println("result: "+result);
}
}

Output:

result: ternary condition true

References

Switch case in Java from Oracle: Switch Case

Next Article To Read

You may be interested to read our next article : Java Numbers