-1

In the program, no errors. PASS, CLEARED user inputs are working as desired. But when I enter NO as user input in the nested if, control picks "else statement" correctly, but also it is going inside, and executing the next S.O.P display like "Have you CLEARED second level of interview?" which is not required when the first level interview is not cleared. Hence, it displays both else & next if related statement. But I wanted to get displayed only else statement and get exit.

package controlStatements;

import java.util.Scanner;

class nested_if_else_Statement {

      static public void main(String[] args) {
             
            Scanner scan = new Scanner(System.in);
             
             System.out.println("Online Assesment, enter PASS or FAIL?");
             String oa = scan.nextLine();
             if (oa.equals("PASS")) {   
                 System.out.println("Please wait in the office lobby for further levels of interview.");
                 
                 System.out.println("Have you CLEARED first level of interview?");
                 String level1 = scan.nextLine();
                 if(level1.equals("CLEARED")) {
                    System.out.println("Please wait for second level of interview.");    
                 }else {
                    System.out.println("We're Sorry, better luck next time.");
                 }
                 
                 System.out.println("Have you CLEARED second level of interview?");
                 String level2 = scan.nextLine();
                 if(level2.equals("CLEARED")) {
                     System.out.println("Please wait for third level of interview.");
                 }else {
                     System.out.println("We're sorry, better luck next time.");
                 }
                 
                 System.out.println("Have you CLEARED third level of interview?");
                 String level3 = scan.nextLine();
                 if(level3.equals("CLEARED")) {
                    System.out.println("Please wait for fourth level of interview.");
                 }else {
                     System.out.println("We're sorry, better luck next time.");
                 }
                 
                 System.out.println("Have you CLEARED fourth level of interview?");
                 String level4 = scan.nextLine();
                 if(level4.equals("CLEARED")) {
                    System.out.println("Kindly wait for final level of HR Interview.");
                    System.out.println("Thank you for your patience.");
                 }else {
                    System.out.println("We're sorry, better luck next time.");
                 }
                 
                 System.out.println("Have you DONE with your HR Interview?");
                 String levelHR = scan.nextLine();
                 if(levelHR.equals("DONE")) {
                    System.out.println("Please wait in the office lobby, will get back shortly."); 
                 }
                 
             } else {
                 System.out.println("We're Sorry, better luck next time");
             }
     
             scan.close();
      }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

2

These statements are working as expected. It captures the else, but you never tell it to stop running, so it continues to the next step. An easy way to fix this is to add a return statement whenever you want to exit the code.

if (/* whatever */) {
   // do stuff
} else {
   // do stuff
   return;
}

// other stuff that won't be ran if `return` is reached
MrDiamond
  • 958
  • 3
  • 17
1

You need to direct control flow out of the function using return. This will prevent control flow from evaluating the next conditional.

class nested_if_else_Statement {
    static public void main(String[] args) {
        Scanner scan = new Scanner(System.in);
             
        System.out.println("Online Assesment, enter PASS or FAIL?");
        String oa = scan.nextLine();
        if (oa.equals("PASS")) {   
            System.out.println("Please wait in the office lobby for further levels of interview.");
                 
            System.out.println("Have you CLEARED first level of interview?");
            String level1 = scan.nextLine();
            if (level1.equals("CLEARED")) {
                System.out.println("Please wait for second level of interview.");    
            } else {
                System.out.println("We're Sorry, better luck next time.");
                return;
            }
                 
            System.out.println("Have you CLEARED second level of interview?");
            String level2 = scan.nextLine();
            if (level2.equals("CLEARED")) {
                System.out.println("Please wait for third level of interview.");
            } else {
                System.out.println("We're sorry, better luck next time.");
                return;
            }

            // ...

As an additional suggestion, you can remove a level of indentation by testing not oa.equals("PASS") but rather !oa.equals("PASS"). By exiting early on this condition, we can eliminate a level of indentation.

package controlStatements;

import java.util.Scanner;

class nested_if_else_Statement {
    static public void main(String[] args) {         
        Scanner scan = new Scanner(System.in);
             
        System.out.println("Online Assesment, enter PASS or FAIL?");
        String oa = scan.nextLine();
        if (!oa.equals("PASS")) { 
            System.out.println("We're Sorry, better luck next time");
            scan.close();
            return;
        }

        System.out.println("Please wait in the office lobby for further levels of interview.");
             
        System.out.println("Have you CLEARED first level of interview?");
        String level1 = scan.nextLine();
        if (level1.equals("CLEARED")) {
            System.out.println("Please wait for second level of interview.");    
        } else {
            System.out.println("We're Sorry, better luck next time.");
            return;
        }
             
        System.out.println("Have you CLEARED second level of interview?");
        String level2 = scan.nextLine();
        if (level2.equals("CLEARED")) {
            System.out.println("Please wait for third level of interview.");
        } else {
            System.out.println("We're sorry, better luck next time.");
            return;
        }
             
        System.out.println("Have you CLEARED third level of interview?");
        String level3 = scan.nextLine();
        if (level3.equals("CLEARED")) {
            System.out.println("Please wait for fourth level of interview.");
        } else {
            System.out.println("We're sorry, better luck next time.");
            return;
        }
             
        System.out.println("Have you CLEARED fourth level of interview?");
        String level4 = scan.nextLine();
        if (level4.equals("CLEARED")) {
            System.out.println("Kindly wait for final level of HR Interview.");
            System.out.println("Thank you for your patience.");
        } else {
            System.out.println("We're sorry, better luck next time.");
            return;
        }
             
        System.out.println("Have you DONE with your HR Interview?");
        String levelHR = scan.nextLine();
        if (levelHR.equals("DONE")) {
            System.out.println("Please wait in the office lobby, will get back shortly."); 
        }
     
        scan.close();
    }
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • Thank you so much, Chris It's working now as desired. – Murali7373 Sep 02 '23 at 01:57
  • While this code may serve the OP’s purpose, it serves as an example of things _not to do_ while writing Java code. 1) `Scanner` is `AutoCloseable`, so, should be used within try-with-resources 2) Class name should be Camel case 3) Q&A can be put into a map and the code cleaned up instead of repeating the same construct over and over. – Abhijit Sarkar Sep 02 '23 at 02:20
  • Those are definitely valid concerns and I'm glad you commented. I did not wish to address concerns other than control flow, which is at the heart of the question. – Chris Sep 02 '23 at 02:23
  • @Chris I’d not have pointed these out if OP weren’t a newbie in Java (their question leaves no doubt that they are inexperienced), since this forum is not meant for code reviews. But I am afraid that code that does the job but sets a poor precedent may send them down a wrong path to begin with. – Abhijit Sarkar Sep 02 '23 at 02:31
  • @AbhijitSarkar regarding point 1: on the other hand it is strongly recommended to **not** close `System.in` and so also not to close the `Scanner` built using `System.in` or have it done by try-with-resources. (it was not opened by the code, it should not be closed by it) || @Chris that is also valid for calling `close()` directly – user22471702 Sep 02 '23 at 06:30