473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Is switch statement is alternate of if else staement ?

2
Is switch statement is alternate of if else staement ?
Aug 6 '10 #1
10 2627
Maruf
2
ans me quickly plz
Aug 6 '10 #2
newb16
687 512MB
no, because in some cases switch can't be used instead of if/else.
Aug 6 '10 #3
johny10151981
1,059 1GB
I think every switch case can be replaced by every else if.

to newb16
Can you please give me an example that is not replaceable
Aug 6 '10 #4
newb16
687 512MB
if (somestring=="foo") ...
else if (somestring == "bar") ...
else if ...

switch(somestring) <- wrong, not an integral type
case "foo":

Yet the reverse is true (but if you use the switch's lacking of implicit break, it may be tricky)
Aug 6 '10 #5
johny10151981
1,059 1GB
Mr. newb16,
I agree with you :)
Aug 6 '10 #6
Dheeraj Joshi
1,123 Expert 1GB
You can have enums if you want and then use if else.

Regards
Dheeraj Joshi
Aug 6 '10 #7
donbock
2,426 Expert 2GB
The following if statements cannot be expressed by switch statements even if the arguments are integers.
Expand|Select|Wrap|Line Numbers
  1. if ((a==2) && (b<c))
  2.    ...
  3.  
  4. if (a==2)
  5.    ...
  6. else if (b < c)
  7.    ...
However, the reverse is true: any switch statement can be replaced by a single if-elseif-else cascade.
Aug 6 '10 #8
Banfa
9,065 Expert Mod 8TB
However one of the original purposes of the switch was to quickly and efficiently jump between multiple options. To this end switch statements often use to use jump tables where as switch statements processing the condition a single time where as if, else if ... else statements have to process multiple conditions.

In early compilers this had the effect of making switch more efficient than if, else if ... else, however I believe the advent of optimising compilers has tended to reduce this effect with the optimiser being able to choose the best way to implement both a switch and an if, else if ... else.
Aug 6 '10 #9
donbock
2,426 Expert 2GB
Here are a few more reasons to NOT replace a switch statement with an if-elseif-else cascade:
  1. It is less obvious to a maintainer that each leg of the if-elseif-else refers to the same variable than it is for a switch statement.
  2. You might mistakenly type the wrong variable name in one of the if-elseif expressions. This particular error is impossible with a switch statement.
  3. If the switch variable is an enumeration, some lint tools will warn you if any enumeration constants are not mentioned in the case statements.
Aug 6 '10 #10
yes , it is possible but in some cases if statement doesn't have option.
Dec 29 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Skip Montanaro | last post by:
Stephen> { Stephen> 'one': lambda x:x.blat(), Stephen> 'two': lambda x:x.blah(), Stephen> }.get(someValue, lambda x:0)(someOtherValue) One thing to remember is that function calls in Python...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
1
by: chis2k | last post by:
How can I compare objects in a switch statement? For instance: public void textbox1_Select(object sender, System.Eventargs e) { Textbox tx = (TextBox)sender; switch (tx) { case textbox2:...
14
by: Chris | last post by:
Hi, can you specify a range in a switch - statement ? switch (i) { case 100 - 999 : // do something break; case 1000 - 9999:
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
7
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.