Matrix
Matrix

Reputation: 25

While loop not breaking out in Java

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

Upvotes: 1

Views: 160

Answers (5)

Dr. Zola
Dr. Zola

Reputation: 1


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.

Upvotes: 0

Tyler Perry
Tyler Perry

Reputation: 46

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.

Upvotes: 0

Lukas Eder
Lukas Eder

Reputation: 220877

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

Upvotes: 2

Orion
Orion

Reputation: 245

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;
                }
            }
        }
    }
}

Upvotes: 0

L_Cleo
L_Cleo

Reputation: 1527

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");
            
        }
    }
}

Upvotes: 1

Related Questions