Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...
Printable View
Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...
It's just less code and simpler to follow, that's all.
Thanks Rodik, an honest answer I feel...
Actually, yes, it is a less code and less headache thing. But you need to know when to use what. Whenever you want to choose, if you have two things to choose from, use 'if'. If you have more than 3 things to choose from, use 'case'. If you have 3 things, (take one and pass on the remaining to me!) you can use either 'if' or 'case', whatever you like.
The important point is 'two is a company, three is a crowd' applies to the 'if' statement. Whenever the number of choices increases, 'if' will become complicated to follow.
In Fact VB take less time to translate a CASE than IF
Just note that large case statement will be faster than an if statement. If you use multiple if statements, it has to check each one before continuing on to the next. If you use one large case statement, it automatically matches a case and goes right to it, doesn't have to check all the other cases.
So what about ElseIf? I made some performance tests and the Ifs were always faster than Select Case...
How many case/elseif statements did you use?
All I know is I had a function once, it was all If, and then I changed it to a Case, and it went much faster. I would like you to perform some tests, and have the results posted on my desk by twenty hundred hours.
Maybe you could get Paul - the aussie in Japan do some performance tests, he seems sharp and knows the workings of getTickCount.
:D
What d'ya mean - workings? It's dead simple!
lElapsed has the time taken for ... to execute (in ms).Code:lBefore = GetTickCount
...
lElapsed = GetTickCount - lBefore
I didnt say it was difficult, I am just too dam lazy to do it, but I figure ya throw Paul a nice line, he might do it.
I am interested in knowing which is faster, but have no drive to do it myself.
Yeah, and to work out which one's faster you could use a Case or an If statement. ;)
By the way, is IIf slower then an If...ElseIf statement?
IIf is definitely slower than If...ElseIf
Also done tests and IF, elseif, elseif...End if is faster than select case.
For performance testing, if you use an loop with for next you will get missleading results, and that's what you need to do with gettickcount. For more exact results you could use querryperformace counter, which is accurate to about 1 microsecond, also dependent on hardware...
Hmmmmmmmmmmmm,Code:If InDoubt Then
Select Case of InDoubt
Case Something
Dummy="Hmmmmmmmmmmmm"
Case Else
DoEvents()
End Select
Else
DoEvents()
End If
Makes one stop and think...
DocZaf
{;->
Hi!, Spotted my name and thought I'd drop in.
I think we went through this a while ago and were all dissapointed to find out that heaps of elseif's were faster than a case statement, we were hoping to find that the case was faster because it looks cleaner.
In C the case statement is losts faster because it can skip to the bottom of the statement after it executes the True code, but C doesn't have an elseif statement, just else if (a new if statement after the else) which means losts of extra evaluation.
Personally I try never to use more than 1 or 2 elseifs in an if statement, but that's for readablilty, mostly the impact of using a case statement is so small that there are almost always better perfomance tunings that can be done.
All talking about whether IF is faster or CASE is faster, where the time wasted (if any) in any method has to be measured in micro/mili/nano seconds (whichever is the smallest unit) ?????
Let me ask you all a simple question: You have to use either IF or CASE in your project. You have to make a choice from two options, what would you choose? And if you were to choose one from 15 options, what would you choose?
I feel you should also consider the debugging strain on the programmer. It should not be that you saved 16 miliseconds by using IF instead of CASE and lost 16 hours in debugging the nested IFs.
Hm 16 Millicsecond in the main loop would quite nice!
Told ya Paul would know...
Thx Paul
The speed difference is minimal, as in unless you need to loop it over 100,000 time, you'll not notice. Use which ever makes you're code more readable and understandable.
Paul, regarding using if's in C++. You don't necessarily have to use else whenever you are using a lot of if's.
This would work fine as well.
But, yes, I agree wih you on using a switch statement instead because you easily insert a break into it.Code:if( Choice == '*' )
cout << "Multiplication";
if( Choice == '/' )
cout << "Division";
if( Choice == '+' )
cout << "Addition";
Although in many cases this wouldn't work because you need to avoid the possibility of satisfying more than one if statement.
Paul is dead right, in any version of basic l have used vb/Universe/Pick etc etc. Continuos If statements are fractionally faster than Case statements, strange but true.
Even though each one of the IF statements has to be tested, the CASE statement does it in a sneaker way too
E.g
Now suppose iValue = 1, the first If condition is executed with each subsequent If being tested. With the Case statement, the first Case is executed followed by each line of code being tested for the End Select statement. Hence the apparent paradox nature of the speed test.Code:Select Case iValue
Case 1
Case 2
Case 3
Case 4
End Select
If iValue = 1
If iValue = 2
If iValue = 3
If iValue = 4
Of course with the If construction you would throw in an Exit Sub or whatever to by-pass the redundant testing.
Its up to you and your style of coding. Cause a number of people will end up modifying my code over time, l use case for large numbers of tests to allow for greater readability. Phantomd uses nested If statements, because he prefers them....don't trust a unix guy to do anything easy...
Just sit back and think...gee in a couple of years l might have to modify this code, would l be able to read it easily to make a change.
Not only code is easest for read, Computer only test var onceQuote:
Originally posted by coox
Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...