Android Question If versus Case

An Schi

Well-Known Member
Licensed User
I cannot see a real difference between If and Case.

When should i use if and when case?

Thx :)
 
My 50cts (others would have better replies) :
  • it is by design quicker and clearer to use a "SELECT CASE" when you have many value's cases to test
  • it is much simpler to use a "IF" when you have many values to test in the same condition
  • if I remember correctly, "IF" should be able to make short-circuit (with logical operators)
 
Upvote 0
SELECT-CASE = IF-ELSEIF-...-(FALSE)EXIT

Code: Copy
Number=Rnd(1,3)

Select case Number
Case 1 Msgbox "Hit 1"
Case 2 Msgbox "Hit 2"
Case 3 Msgbox "Hit 3"
case else Msgbox "No way!"
end select

if number = 1 then
    msgbox "hit 1"
else if number=2 then
    msgbox "hit 2"
else if number=3 then
     msgbox "hit 3"
else
    msgbox "no way!"
end if

Same Results... 2 ways... IF alone (Without ElseIf) is more like a 1-0 or a OFF-ON Situation... Those 3 are "conditional statements" IF for 2 Values, ELSEIF or CASE for more.

But i think that ELSE IF is more process consuming right?
 
Upvote 0
If I remember correctly, IF/THEN blocks take more time to execute than a Select/Case, However, the order in wich the cases are presented does matter and may influence the flow in an unexpected way.
 
Upvote 0
Also, for clarity of code, if there are many values to be checked, the code for CASE is often much clearer to read.
 
Upvote 0
Back
Top