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..

Matrix
  • 25
  • 1
  • 6
  • 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. – Ole V.V. May 09 '21 at 08: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. – khelwood May 09 '21 at 08:07
  • 1
    Not really a duplicate but [this other question](https://stackoverflow.com/questions/886955/how-do-i-break-out-of-nested-loops-in-java) may help as well for the general case of "nested constructs from which you can `break`". – Federico klez Culloca May 09 '21 at 08:24

5 Answers5

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

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • 1
    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. – Andy Turner May 09 '21 at 09:15
  • @AndyTurner Simple Q, simple A – Lukas Eder May 09 '21 at 09:24
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");
            
        }
    }
}
L_Cleo
  • 1,073
  • 1
  • 10
  • 26
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;
                }
            }
        }
    }
}
Orion
  • 248
  • 3
  • 10
  • Since you set `shouldBreak` to true in *all* cases, the variable doesn’t really seem necessary in this code. – Ole V.V. May 09 '21 at 09:13
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.

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.