Cags Posted April 15, 2004 Posted April 15, 2004 I was wondering in what situations the If/ElseIf, and Switch case statements are best used. By this I mean what are the advantages and disadvantages of each one. From what I can tell they both essentially do the same thing. Quote Anybody looking for a graduate programmer (Midlands, England)?
Arch4ngel Posted April 15, 2004 Posted April 15, 2004 If/ElseIf are more used if there is 2,3 or 4 case that you can use (at 4 it begin to be big and difficult to understand). They are used to verify Condition. The switch is used to compare the value of something (enumeration, string, int, etc...) to assure which way to take if the variable is equal to something. I use switch when I have 3 or more state in a variable. Like a enumeration: DataRowState.Added DataRowState.Deleted DataRowState.Modified etc... Another advice ? Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Leaders Iceplug Posted April 15, 2004 Leaders Posted April 15, 2004 Switches should only be used to compare one variable to different outcomes (all other ways look tacky)... very useful for enumerations and just for checking variables: If you want to do something different for Text = "A", "AA", and "AAA", then use a Select / Switch Case. Otherwise, use Ifs and ElseIfs... regardless of how many entries are in it. You can use If/End If for checking 1 or 2 entries (at 3, I opt for a switch). If you are checking something like A = 123, B = 247, C = 625, D = 777 use an If (even A = 12, B = 12, C = 12, D = 12, unless they do the same thing, then you can use OrElse / || ). Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Liqdfire Posted April 16, 2004 Posted April 16, 2004 I was wondering in what situations the If/ElseIf' date=' and Switch case statements are best used. By this I mean what are the advantages and disadvantages of each one. From what I can tell they both essentially do the same thing.[/quote'] if your doing all the processing on one variable you might look into select case Quote
spatuel Posted April 20, 2004 Posted April 20, 2004 I was wondering in what situations the If/ElseIf' date=' and Switch case statements are best used. By this I mean what are the advantages and disadvantages of each one. From what I can tell they both essentially do the same thing.[/quote'] Hi, Quick performance response: ELSEIF needs to consider each option, so there is a HUGE performance impact. SWITCH jumps to the specific address If you have to consider many options use the SWITCH statement when its possible Quote
Leaders snarfblam Posted November 16, 2005 Leaders Posted November 16, 2005 Hi, Quick performance response: ELSEIF needs to consider each option, so there is a HUGE performance impact. [/Quote] Until something evaluates to true. Then the rest of the ElseIfs can be ignored. If you have a few very common cases, and a handful of uncommon cases this isn't a big deal, since you usually branch off near the beginning or the elseif chain. SWITCH jumps to the specific address [/Quote] A switch will create a jump table for close together integer values. For values far apart, the branching and determining and what not is more complex and not necessarily as efficient. I'm pretty sure that switches on strings involve hashing, comparing hashes, and confirming matches, making switches easier to write, but for smaller numbers of options, less efficient. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.