May 25, 2016 11:00 AM
(This post was last modified: Aug 11, 2016 05:48 PM by SureDawgg.)
Delete this thread please, thank you
|
C
Why isn't my switch statement currency convertor working? |
|
May 25, 2016 11:00 AM
(This post was last modified: Aug 11, 2016 05:48 PM by SureDawgg.)
Delete this thread please, thank you
look at this example:
int caseSwitch = 1; switch (caseSwitch) { case 1: Console.WriteLine("Case 1"); break; case 2: Console.WriteLine("Case 2"); break; default: Console.WriteLine("Default case"); break; } You need the default case, this handles cases where the caseSwitch isn't one of the listed cases..
May 25, 2016 11:42 AM
(This post was last modified: May 25, 2016 11:42 AM by Das Kartoffel.)
You forgot the 'break' keywords, as follow:
PHP Code
May 25, 2016 11:42 AM
(This post was last modified: May 25, 2016 11:43 AM by ๖ۣۜOpulence.)
OH I GOT IT. You forgot to put 'break;' after each case, refer to my example I found.
May 25, 2016 04:09 PM
(This post was last modified: May 25, 2016 04:16 PM by 0xDEAD10CC.)
(May 25, 2016 01:54 PM)6577657534735 Wrote: Missing a break is annoying. Compiler should give a warning. NO - Definitely not! ❓ Think about it... Would you want the compiler to warn you that you missed typing 'else if' instead of 'if' the second time here: Code if (conditionA)^ Same thing basically. There's some cases where you want a case statement to fall through... Having compiler warnings in cases like that which clutters the view of more important warnings would be annoying. Have you ever tried enabling the most verbose warning output and included <Windows.h>?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer] (May 25, 2016 04:09 PM)0xDEAD10CC Wrote: NO - Definitely not! ❓ Yeah what was I thinking. Very dumb on my part
May 27, 2016 02:36 PM
(This post was last modified: May 27, 2016 02:43 PM by Corios.)
(May 26, 2016 11:43 PM)6577657534735 Wrote: Yeah what was I thinking. Very dumb on my part No, I agree with you. Falling through from one case labels code to another is pretty evil, I would prefer it give the warning. That shouldn't be intended functionality, unlike two ifs Say you have: Code Switch numIf you are working with students and not fees, it will appear that it's case 3 or 4. You may not realize you've been deleting all fees the entire time. Obviously not a good example as those should usually never be in the same switch, its just for demonstration. You should not need to check above your cases every time to make sure nothing else is happening.
May 27, 2016 03:05 PM
(This post was last modified: May 27, 2016 03:17 PM by 0xDEAD10CC.)
(May 27, 2016 02:36 PM)Corios Wrote: No, I agree with you. Falling through from one case labels code to another is pretty evil, I would prefer it give the warning. That shouldn't be intended functionality, unlike two ifs So you're willing to adopt a compiler warning that will end up being redundant in lots of optimized code for the purpose of programmer stupidity? It should be pretty obvious in your case that things are getting deleted, and if the programmer was competent enough to breakpoint and step through the code it would be a very easy fix. It would be silly.. How is it to be considered "evil"? It can lead to more clear code opposed to if statements that do the same thing, and end up being more optimized also. This is like the people who keep saying that goto is evil. It's only as evil as you make it but it has a place in the language and its usages still as you can see in lots of Linux kernel code even for cleanup and cleanly exiting nested scopes without introducing more locals. And how is that semantically not like 2 if's? Code if (x == 1 || x == 2)In more complex situations a switch statement with fall-throughs is much more readable. If code is properly commented too it can help with re-assuring the intended logic if someone is concerned. In C#'s case it was removed due to programmer incompetence, and people later accepted that the use of a goto was an acceptable workaround: Code switch (/*...*/) {Lastly, here is a great example: https://en.wikipedia.org/wiki/Duff%27s_device
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer] (May 27, 2016 02:36 PM)Corios Wrote: No, I agree with you. Falling through from one case labels code to another is pretty evil, I would prefer it give the warning. That shouldn't be intended functionality, unlike two ifs What if you wanted a fall through? (May 27, 2016 03:43 PM)Remix Wrote: What if you wanted a fall through? Then you shouldn't be using a switch, a switch is meant for a multi-branch flow, if your case sled is going through multiple branches, that doesn't sound right, now does it? I don't know of another language that allows it (albeit I only really use about 5 languages so there are probably a few more that support break structures and allow this)
May 27, 2016 06:57 PM
(This post was last modified: May 27, 2016 07:18 PM by 0xDEAD10CC.)
(May 27, 2016 06:24 PM)Corios Wrote: Then you shouldn't be using a switch, a switch is meant for a multi-branch flow, if your case sled is going through multiple branches, that doesn't sound right, now does it? Explain how it's not right. There's absolutely nothing that says it isn't. Quote:... a switch is meant for a multi-branch flow This does not mean that a branch can't lead to the same code path, and if you know how a switch works internally as a jump table there's no reason as to why it should be avoided, but you didn't read my previous post either: https://en.wikipedia.org/wiki/Duff%27s_device Lots of programmers still use it, and the Eclipse Foundation uses code that does too: https://github.com/eclipse/ptp/blob/mast...ggregate.c It's not uncommon and there's no reason to not use it where it makes sense. If statements would be a horrible alternative, less optimized, and more ugly to read... Again, it comes down to programmer stupidity. If one has the mindset that you shouldn't be able to do it because C# by default disallows this, then stick to C# or whatever other language it is that you're used to. Languages are not supposed to conform to standards from another language and using that as a reason for why it should be disallowed or avoided is ridiculous. What you are suggesting with the warning about break statements in switch statements -- is taking something that is completely capable of doing something that may be desirable (with the added benefit of being more optimized), and putting training wheels on it so to speak to provide an obstacle that doesn't need to be there in the first place. Nobody goes mountain biking with training wheels.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
May 27, 2016 11:03 PM
(This post was last modified: May 27, 2016 11:04 PM by Remix.)
(May 27, 2016 06:24 PM)Corios Wrote: Then you shouldn't be using a switch, a switch is meant for a multi-branch flow, if your case sled is going through multiple branches, that doesn't sound right, now does it? That means you are not allowed to execute the same stuff for multiple cases? Imagine you had 15 cases which should execute the same code Would you write it 15 times?
Jun 1, 2016 10:16 AM
(This post was last modified: Jun 1, 2016 10:24 AM by Kantos.)
All the C switch statement does is simply jump to the case label that matches the control expression value if it exists and continues execution from there. Some languages give each case its own block and exit the switch once the instructions in that block have completed, and require an explicit instruction for any fallthrough.
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img] |
|
|
| Possibly Related Threads... | |||
| Thread / Author | Replies | Views | Last Post |
| [Java] by OBAMASELFApr 23, 2019 12:42 AM | 4 | 245 |
Apr 23, 2019 12:42 AM Last Post: Joseph Stalin |
| [C] by Quality_k1nGSep 27, 2018 07:05 PM | 6 | 292 |
Sep 27, 2018 07:05 PM Last Post: Quality_k1nG |
| [C] by F.A.S.TJul 22, 2018 10:57 AM | 1 | 118 |
Jul 22, 2018 10:57 AM Last Post: aeiro |