Dev Bingo
Dev Bingo

Reputation: 67

Use switch case as conditional statement in Java

I am trying to use switch case for selecting out conditions based on variable, but I'm getting error. What would be the correct process to do so? Is it possible using switch case, or should i use nested iffs?

public class Grader {
   // no attributes required
    public Grader() {
// no code required
}


public String grade(int mark) {
    String grade = null;
// code to determine grade goes here
    switch (mark){
        //For marks 85-100, Grade is HD
        case ((100>=mark>=85)):
            grade="HD";
            break;
        //For marks 75-84, Grade is D
        case (mark>=75 && mark<=84):
            grade="D";
            break;
        //For marks 65-74, Grade is C
        case (mark>=65 && mark<=74):
            grade="C";
            break;
        //For marks 50-64, Grade is P
        case (mark>=50 && mark<=64):
            grade="P";
            break;
        //For marks 49-0, Grade is HD
        case (mark<=49 && mark>=0):
            grade="F";
            break;






    }
    return grade;
}
public boolean pass(int mark) {
    boolean pass = false;
// code to determine pass goes here

    return pass;
    } 
}

I

Upvotes: 3

Views: 11420

Answers (6)

kimo
kimo

Reputation: 1

if you really wanna use the switch you can divide mark by 10 and make case 10 & case 9 = A then you can put if conditions inside the cases from 8 to 5 and default for f

Upvotes: 0

Anonymous
Anonymous

Reputation: 1

For this situation, definitely if-else statement. Your switch statement is wrong btw.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Switch only takes constant values in it's cases. You cannot add expressions in cases which evaluate run time.

The best here is to go with traditional if-else-if.

 public String grade(int mark) {
        String grade = null;
        // code to determine grade goes here
        if (mark >= 100 && mark <= 85) {
            grade = "HD";
        } else if (mark >= 75 && mark <= 84) {
            grade = "D";
        } else if (mark >= 65 && mark <= 74) {
            grade = "C";
        } else if (mark >= 50 && mark <= 64) {
            grade = "P";
        } else if (mark <= 49 && mark >= 0) {
            grade = "F";
        } else {
            grade = "Not found";
        }
        return grade;
    }

Upvotes: 2

Berkay
Berkay

Reputation: 1

The problem is that you are trying to generate boolean value while checking if the statement is less or greater than some values.In switch, you cannot control it if you are iterating through integer value type.

if (100 >= mark && 85 <= mark) {
        // grade ...
    } else if (84 >= mark && 75 <= mark) {
        // grade ...
    } else if (74 >= mark && 65 <= mark) {
        // grade ...
    } else if (64 >= mark && 50 <= mark) {
        // grade ...
    } else if (49 >= mark && 0 <= mark) {
        // grade ...
    } else if (-1 >= mark || 101 <= mark) {
        // Error,mark cannot be lower than 0 and higher than 100 
    }

This should solve your problem.But there are many approaches that spare you from multiple check statements.Check enums and mapping technique

Upvotes: 0

CellX
CellX

Reputation: 11

you can use the if-else statements for this as switch cannot have expressions. You can use this.

public class Grader {
   // no attributes required
    public Grader() {
// no code required
}


public String grade(int mark) {
    String grade = null;
// code to determine grade goes here
    if(mark>=85 && mark<=100)
    {
     grade = "HD";
    }
  else if(mark>=75 && mark<=84)
  {
    grade = "D";
  }
  else if(mark>=65 && mark<=74)
  {
    grade = "C";
  }
  else if(mark>=50 && mark<=64)
  {
    grade = "P";
  }
  else if(mark>=0 && mark<=49)
  {
    grade = "F";
  }
  return grade;

}
public boolean pass(int mark) {
    boolean pass = false;
// code to determine pass goes here

    return pass;
    } 
}

Upvotes: 0

Chris Gong
Chris Gong

Reputation: 8229

Unfortunately, Java doesn't have switch-ranges like Javascript has, so one way to get you're desired result would be to divide the mark by 10 and rounding up if necessary (e.g. 8.5 becomes 9 and etc.). Then, for each number from 0 to 10, have a case for it like so,

double markDividedByTen = mark/10.0;
int mark = (int) (markDividedByTen + 0.5); //round to whole number
switch (mark){
        case 10:
            grade="HD";
            break;
        case 9:
            grade="HD";
            break;
        case 8:
            grade="D";
            break;
        case 7:
            grade="C";
            break;
        case 6:
            grade="P";
            break;
        case 5:
            grade="P";
            break;
        case 4:
            grade="F";
            break;
        case 3:
            grade="F";
            break;
        case 2:
            grade="F";
            break;
        case 1:
            grade="F";
            break;
        case 0:
            grade="F";
            break;
    }

The only issue with this approach is that, a mark from 45 to 49 would technically receive a "P" grade, but I just want to show you an alternative approach to if/else if statements that still utilized switch statements.

Upvotes: 0

Related Questions