4

I'm trying to replace my if and else if statement with a switch statement.

The Ball class comes from an external action script file witch has a variable where I pass in the balls radius(radius = 30).

How would I convert the if and else if statements into a switch statement?

The code:

private var ball:Ball;

private var left:Number = 0;
private var right:Number = stage.stageWidth;
private var top:Number = 0;
private var bottom:Number = stage.stageHeight;    

    if(ball.x >= right + ball.radius)
    {
        ball.x = left - ball.radius;
    }

    else if(ball.x <= left - ball.radius)
    {
        ball.x = right + ball.radius;
    }

    if(ball.y >= bottom + ball.radius)
    {
        ball.y = top - ball.radius;
    }

    else if(ball.y <= top - ball.radius)
    {
        ball.y = bottom + ball.radius;
    } 

Thanks you

2
  • I think, that you shouldn't use "switch" here. "If" statements looks much better in your case. Commented Dec 6, 2012 at 17:21
  • good question. switch are much cleaner looking than lots of else if's. They happen to be faster too. Commented Dec 6, 2012 at 17:39

2 Answers 2

3

There is a little trick with this - you do the evaluation of the inequality at the case not the switch:

 switch(true) {
     case ball.x >= right + ball.radius:
         ball.x = left - ball.radius;
         break;
     case ball.x <= left - ball.radius:
         ball.x = right + ball.radius;
         break;
 }

switch(true){
     case (ball.y >= bottom + ball.radius):
         ball.y = top - ball.radius;
         break;
     case (ball.y <= top - ball.radius):
         ball.y = bottom + ball.radius;
         break;
} 
Sign up to request clarification or add additional context in comments.

Comments

1

Think of switch statements as glorified IFs.
Basically, you are evaluating the switch statement to the case statement.
Switch statements evaluate in a top down order so once there is a match found it will break out of the switch after the code in that case is run.
Also, in your case, you want to keep X and Y separate

switch(true){
  case (ball.x >= right + ball.radius):
    ball.x = left - ball.radius;
    break;
  case (ball.x <= left - ball.radius):
    ball.x = right + ball.radius;
    break;
  default:
    // no match
}

switch(true){
  case (ball.y >= bottom + ball.radius):
    ball.y = top - ball.radius;
    break;
  case (ball.y <= top - ball.radius):
    ball.y = bottom + ball.radius;
    break;
  default:
    // no match
} 

5 Comments

OK so let me try to understand this. the switch(true) is the same as saying if(ball.x >= right + ball.radius). Because all the if statement is checking whether or not the condition is true or false, and that why you right true in the switch(true)?
A switch statement goes through each case statement in order, and if the case statement matches the switch condition (true in this case) then it will execute the code in that case and move on to the next one (unless it reaches a break; then it will exit the switch), if it doesn't reach a break, it will execute whatever is in the default: block.
So switch statements are not really glorified if conditions, because you can have multiple case statements run - you have to manually use the break keyword if you want to exit the switch and keep any of the others from evaluating.
No - switch(true) just gets you into the switch statement, it always happens (true) and then you have the evaluation of the positions done at each case statement. If you're planning on expanding the number of comparisons, the switch works well. If you put switch(false), it works as well - basically it needs a Boolean value.
No - it's not the same as saying if(ball.x >= right + ball.radius) - there is no evaluation of that statement at the switch. switch(true) means the case condition stands as true. switch(false) means that the case condition is false. If condition case (ball.x >= right + ball.radius) == true ---> then execute that code else check the next one. My concern was that Scope was thinking things are evaluated at the switch when it's actually just stating what the evaluation needs to be compared against. Did I just make it more confusing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.