Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If-else working, switch not [duplicate]

I am making an app that has a grid of images with text and each one opens a different activity. It works fine but just for design purposes I want to replace my if-else statements with switch statements (which I assume I can do) however it doesn't work. Right now my working code to set the label on each image is:

if(position == 0)
        textView.setText(R.string.zero);
    else if(position == 1)
        textView.setText(R.string.one);
    else if(position == 2)
        textView.setText(R.string.two);
    else if(position == 3)
        textView.setText(R.string.three);
    else if(position == 4)
        textView.setText(R.string.four);
    else if(position == 5)
        textView.setText(R.string.five);
ect....

I want to use:

switch(position)
case 0:
   textView.setText(R.string.zero);    
case 1:
   textView.setText(R.string.one);
case 2:
   textView.setText(R.string.two);    
case 3:
   textView.setText(R.string.three);
case 4:
   textView.setText(R.string.four);    

but when I did that ever label was the last one that I defined (in my example it would be "four"). I also have a similar code for each object to start a different intent with the position variable however that does the opposite and makes every intent equal to the first one. Is my syntax wrong or will this not work for my situation?

like image 435
Ryan Sayles Avatar asked Jun 27 '13 14:06

Ryan Sayles


People also ask

Which is more efficient switch or if-else?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

Do switch and if-else have differences?

The if-else statement estimates integers, characters, pointers, floating points, and boolean types. The switch statement estimates integers and character expressions. One statement will be executed. It can be if or else.

Are switch statements faster than if-else C#?

The results show that the switch statement is faster to execute than the if-else-if ladder. This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer.

What is the advantage of switch statement over ELSE IF construct?

The switch statement has a fixed depth. It allows the best-optimized implementation for faster code execution than the “if-else if” statement. It is easy to debug and maintain the programs using switch statements. The switch statement has faster execution power.


2 Answers

You need to break; after each statement in a case, otherwise execution flows down (all cases below the one you want will also get called), so you'll always get the last case.

switch(position) {
case 0:
    textView.setText(R.string.zero); 
    break; 
case 1:
    textView.setText(R.string.one);
    break; 
case 2:
    textView.setText(R.string.two);   
    break;  
case 3:
    textView.setText(R.string.three);
    break; 
case 4:
    textView.setText(R.string.four); 
    break; 
}

Here's the official tutorial explaining when to and when not to use break;.

like image 96
Sotirios Delimanolis Avatar answered Sep 24 '22 13:09

Sotirios Delimanolis


You need to break; after each branch:

switch (position) {
    case 0:
        textView.setText(R.string.zero);
        break; // <-- here
    // etc
}

Legitimate uses of switch when you don't break exist, those are called fall throughs; or because you return or throw.:

switch (someNumber) {
    case 0:
        return 0; 
        // no need for break here
    case 1:
        throw new IllegalArgumentException();
        // no need to break here
    case 2:
        System.out.println("Oh, I got two!");
        // fall through
    case 3:
        return 3;
    default:
        System.out.println("Meh")
        // No need to break: last possible branch
}

return -1;

will return 3 even if you enter 2.

But otherwise you need to break.

like image 35
fge Avatar answered Sep 23 '22 13:09

fge