Control Statements (If-Else and Switch Statement) in Java

While the world is going through Corona (COVID-19) pandemics; stay safe at your home and keep your learning spirit high. We discussed Java variables, Data types, and Operators in our previous article. Today we are going to discuss If-else and Switch statements in Java. These are called Control statements. They are the basic building blocks of your program, without them, you can’t write the advanced version of your code.

The series of articles on Java would be exclusively for automation testers or for those manual testers who aspire to become an automation tester.

Let’s begin the learning journey.

Control statements If-else switch in java

If-Else Statement in Java

If-Else statement mainly used to test the condition whether it is true or false, hence, it checks the Boolean condition. Let’s discuss different types of if-else conditions.

If Condition only

It contains only if blocks and it executes when the condition is true.

if(condition){  
//Statement
}

If-Else Condition

When the condition is true then if block gets executed, otherwise, else block will be executed.

if(condition){  
//true condition  
}else{  
//false condition
}

If-Else-If Condition

This statement gets executed when we have to tests multiple Boolean conditions.

if(condition1){  
//statement
}else if(condition2){  
//statement
}  
else if(condition3){  
//statement
}  

Nested If Condition

When there are multiple if blocks within if block then we call it nested If condition.

if(condition){    
     //true condition 1
          if(condition){  
              //true condition 2   
    }    
}

Ternary Operator

The ternary operator is the shortest way to execute the if-else statement. It is represented as (? : ). When if the condition is true then it returns the result of (?), otherwise, it returns the result of (:).

public static void main(String args[])
    {
        String s = "This is inviul";
        String output=(s.contains("inviul"))?"Yes":"No";
    }

Switch Statement in Java

It is used to tests multiple conditions, which act like an If-Else-If ladder. Switch statement is used with byte, short, int, enum, long, String and their corresponding wrapper classes. Thus, it tests the condition against multiple cases.

 switch(expression){
        case value1:
           //statement  
            break;  //optional  
        case value2:
            //statement  
            break;  //optional  


        default:
            //This is executed when above cases are not matched.
    }

The switch statement has one condition against different cases. Here are some points that you should know about Switch statements.

  • The case statement must be always constant. It does not allow variables.
  • There must be a unique case statement; otherwise, it throws a compile-time error on duplicity.
  • Each and every case statement should have a completely optional break statement. If we don’t put a break statement then condition gets compared with all the cases. When we use a break, then it jumps to the next case statement when the condition doesn’t match.
  • We can put an optional default label. The default statement gets executed when there are no matches in the available cases.

Let’s have a look at different examples of switch statements below. It will help you understand the above discussed theory.

Program# 1

public class ControlStatements {
public static void main(String args[]) {
    int num=5;
    switch(num){
        case 5: System.out.println("5");
            break;
        case 20: System.out.println("20");
            break;
        case 30: System.out.println("30");
            break;
        default:System.out.println("Not Found");
    }
}

Program# 2

public class ControlStatements {
public static void main(String args[]) {
    char ch='a';
    switch(ch){
        case 'a': System.out.println("a found");
            break;
        case 'b': System.out.println("b found");
            break;
        case 'c': System.out.println("c found");
            break;
        default:System.out.println("Not Found");
    }
}

Program# 3

public class ControlStatements {
public static void main(String args[]) {
    char ch='a';
    switch(ch){
        case 'a': System.out.println("a found");
        case 'b': System.out.println("b found");
        case 'c': System.out.println("c found");
        default:System.out.println("Not Found");
    }
}

Program# 4

public class ControlStatements {

public static void main(String args[]) {
    String standard="Expert";
    int level=0;
    switch(standard){
        case "Beginner": level=1;
            break;
        case "Intermediate": level=2;
            break;
        case "Expert": level=3;
            break;
        default: level=0;
            break;
    }
}

Program# 5

public class ControlStatements {
    public enum Month {  Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec  }
    public static void main(String args[])
    {
        Month[] month = Month.values();
        for (Month mon : month)
        {
            switch (mon)
            {
                case Jan:
                    System.out.println(1);
                    break;
                case Feb:
                    System.out.println(2);
                    break;
                case Mar:
                    System.out.println(3);
                    break;
                case Apr:
                    System.out.println(4);
                    break;
                case May:
                    System.out.println(5);
                    break;
                case Jun:
                    System.out.println(6);
                    break;
                case Jul:
                    System.out.println(7);
                    break;
                case Aug:
                    System.out.println(8);
                    break;
                case Sep:
                    System.out.println(9);
                    break;
                case Oct:
                    System.out.println(10);
                    break;
                case Nov:
                    System.out.println(11);
                    break;
                case Dec:
                    System.out.println(12);
                    break;
            }
        }
    }
}

This was all about Control statements in Java. Hope you enjoyed the article. Stay tuned for more.

Join Inviul fb group

Leave a Reply