ID:154091
 
This just popped into my mind, so this is more of just to forfill my curiousity...but is switch more efficient than numerous if/else statements nested together?
Yes. In a switch statement, the conditional is evaluated only once.
In response to Lesbian Assassin
Yes, I would think that a single switch would be better than a multitude of If statements. But I don't know how much better it is. I'll check it out with some timing tests some day.
CableMonkey wrote:
This just popped into my mind, so this is more of just to forfill my curiousity...but is switch more efficient than numerous if/else statements nested together?

It is more efficient in the underlying code and easier to read, so it's all around better.

It drives me crazy that Perl (and possibly Python) don't have switch() statements. Some very smart people defend that ("Just use nested if()s!"), and they are just wrong.
In response to Deadron
LOL! To be quite honest, I have never used a switch statement before. I guess that's just because BASIC was my first language learned.
In response to CableMonkey
I never used to use switch statements, then I learned about them and started using them because I thought they were probably more efficient that all the if/else statements, alot easier to type (Not so many if(D == 2450), just if(2450)), and alot easier on the eyes ;P
In response to Kunark
Okie @.@ Im lost in what these switch()'s are.. Im still a n00b around the DM side, and have no real c++ exp right now

What I DO have is exp with many different versions of Basic @.@

Just from what's been said tho, Im guessing its along the same lines as a Select Case in VB?

El
In response to Elorien
You are correct.

Example in VB:

select case value
case 1
debug.print "One"
case 2
debug.print "Two"
case 3
debug.print "Three"
case default
debug.print "Its not one, two, or three."
end select


Equilavent in DM:

switch (value)
if(1)
usr << "One"
if(2)
usr << "Two"
if(3)
usr << "Three"
else
usr << "Its not one, two, or three."

In response to CableMonkey
Ah, whoot~ Its great to see that Im kinda understanding stuff like this even tho I have no idea of the code~ ^________^

Yea, this way is way better. The problem with Ifs is that each if its own computation, whereas with Switch() or Case Select, it only computes it once, and makes the decision from there ^_^


El
In response to Elorien
yeah...logically speaking, you are correct...but I just wanted to make sure this was the case.