1

I have written this switch case program in Java. However, the while loop is not breaking out. Here's the code:

import java.util.Scanner;
public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        while(true) {
            System.out.println("This is a gade checker program");
            System.out.println("Enter the marks from 0 to 100: ");
            System.out.println("Enter the marks: ");
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
                System.out.println("Please enter the marks between the limit assigned");
            }
            else {
                switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    break;
                    
                default:
                    System.out.println("Your grade is E");
                    break;
                }
            }
        }
    }
}

I dont know, why the break function is not working in this loop.. Please help me..

3
  • 2
    You have got break inside switch, so it inly breaks out of the switch, not out of the while. You may put one more break statement after the end of the switch statement. Or a label may solve it. Commented May 9, 2021 at 8:06
  • 3
    Your breaks are all inside the switch, so they just break out of the switch. Add a break after the switch to break out of the loop. Or you could just return from the method. Commented May 9, 2021 at 8:07
  • 1
    Not really a duplicate but this other question may help as well for the general case of "nested constructs from which you can break". Commented May 9, 2021 at 8:24

5 Answers 5

2

Use labels to break out of the while instead of the switch:

loop:
while(true) {
    // ...
    // later:
    switch (..) {
        case ..:
            break loop;
    } 
}

See this tutorial for details: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Sign up to request clarification or add additional context in comments.

2 Comments

I think it would be worth pointing out that the switch would be better outside the loop anyway. The only purpose of the loop (assuming all the switch breaks are intended to break the loop) is to make sure a valid score is entered.
@AndyTurner Simple Q, simple A
1

It's never going to break out of it because the break is referring to the switch statement

Alternatively you could ask the user whether to break out of the loop or not

public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        boolean again = true;
        while(again) {
            ...
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
               ...
            } else {
                switch(grade) {
                   ...
                }
            }

            System.out.println("Do you want to insert another grade (y/n): ");
            String answer;
            do {
               String answer = scanner.nextLine();
            } while (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n")); 
            again = answer.equalsIgnoreCase("y");
            
        }
    }
}

Comments

0

Your break statement works inside the switch statement only so to break outside of the loop Try this:

import java.util.Scanner;
public class exam001 {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int marks;
    while(true) {
        Boolean shouldBreak = false;
        System.out.println("This is a gade checker program");
        System.out.println("Enter the marks from 0 to 100: ");
        System.out.println("Enter the marks: ");
        marks = scanner.nextInt();
        int grade = marks / 10;
        if (marks > 100) {
            System.out.println("Please enter the marks between the limit assigned");
        }
        else {
            switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    shouldBreak = true;
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    shouldBreak = true;
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    shouldBreak = true;
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    shouldBreak = true;
                    break;
                
                default:
                    System.out.println("Your grade is E");
                    shouldBreak = true;
                    break;
                }
                if (shouldBreak){
                    break;
                }
            }
        }
    }
}

1 Comment

Since you set shouldBreak to true in all cases, the variable doesn’t really seem necessary in this code.
0
boolean shouldBreak = false;
while (!shouldBreak) {
    System.out.println("This is a gade checker program");
    System.out.println("Enter the marks from 0 to 100: ");
    System.out.println("Enter the marks: ");
    marks = scanner.nextInt();
    int grade = marks / 10;
    if (marks > 100) {
        System.out.println("Please enter the marks between the limit assigned");
    } else {
       switch(grade) {
          case 10:
          case 9:
             System.out.println("Your grade is A");
             shouldBreak = true;
             break;
          case 8:
          case 7:
             System.out.println("Your grade is B");
             shouldBreak = true;
             break;
          case 6:
             System.out.println("Your grade is C");
             shouldBreak = true;
             break;
          case 5:
          case 4:
             System.out.println("Your grade is D");
             shouldBreak = true;
             break;
          default:
             System.out.println("Your grade is E");
             shouldBreak = true;
      }
   }
}

Suggestion: you should set a varaiable inside the while statement so whenever you want to breakout just change the variable, it's just cleaner way instead of breaking out the switch statement and then checking using an if statement.. the while is using an if anyway(so why not use it?)

as @Ole V.V comment in your question. the problem is that you're breaking out the switch statement each statement has it own level. it's like variables and global variables, a variable inside a function is overriding the global variable. so your switch statement is overriding the break of the while loop. you can't use "break" to break the while loop unless you'll use it directly in the while level outside the switch method.

Comments

0

import java.util.Scanner;
public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        while(true) {
            System.out.println("This is a gade checker program");
            System.out.println("Enter the marks from 0 to 100: ");
            System.out.println("Enter the marks: ");
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
                System.out.println("Please enter the marks between the limit assigned");
            }
            else {
                switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    break;
                    
                default:
                    System.out.println("Your grade is E");
                    break;
                }
            }
            break;
        }
    }
}

You can just add a break; statement just after all the operation is done inside the while loop block, see the above code block, this will end the loop when all your operations are done inside the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.