Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch return wrong value

I have a switch statement to show a grade based on the points students get.

students with points >= 80 get A
students with points >=70 get B
students with points >= 50 get C
students with points >= 30 get D
students with points >= 0 get E

The problem is when the point is 0, it returns A instead of E. Here is the switch statement.

$point = 0;
switch ($point) {
        case $point >= 80:
            echo 'A';
            break;
        case $point >= 70:
            echo 'B';
            break;
        case $point >= 50:
            return 'C';
            break;
        case $point >= 30:
            echo 'D';
            break;
        case $point >= 0:
            echo 'E';
            break;
        default:
            echo 'F';
            break;
    }
like image 668
Abaij Avatar asked Oct 25 '17 06:10

Abaij


People also ask

Can switch return a value?

The switch statement can include constant or variable expression which can return a value of any data type. There can be any number of case statements within a switch. The case can include a constant or an expression.

Does return break a switch?

You do not need a break, the return stops execution of the function.

Can I write return in switch case?

No problem, you can return from a function ANYWHERE within that function. The middle of a loop, or a switch statement, makes no odds. return will return. The breaks used in switch/case prevent execution of the code from falling through to the next case but your returns will do the same.

Do you need break after return in switch?

Answer 533416ba631fe960060022a4 No. return jumps back directly to the function call returning the value after it and everything (in a function) that is after an executed return statement is ignored. So return itself can act as a break statement for functions and no further break is required.


1 Answers

You try to compare $point to a boolean value (like $point >= 80). It is matching the first case because $point = 0 is false and $point >= 80 is false too, so it is matching the first case and echoing A. If you want to use a comparison on the cases you have to use the following code:

$point = 0;

switch (true) {
    case $point >= 80:
        echo 'A';
        break;
    case $point >= 70:
        echo 'B';
        break;
    case $point >= 50:
        echo 'C';
        break;
    case $point >= 30:
        echo 'D';
        break;
    case $point >= 0:
        echo 'E';
        break;
    default:
        echo 'F';
        break;
}

demo: https://ideone.com/lyBUmA

Another solution using if and elseif instead of switch:

$point = 0;

if ($point >= 80) {
    echo 'A';
} elseif ($point >= 70) {
    echo 'B';
} elseif ($point >= 50) {
    echo 'C';
} elseif ($point >= 30) {
    echo 'D';
} elseif ($point >= 0) {
    echo 'E';
} else {
    echo 'F';
}

demo: https://ideone.com/FRlorS

like image 116
Sebastian Brosch Avatar answered Sep 28 '22 18:09

Sebastian Brosch