![]() |
|
#1
|
|||
|
|||
Need a bit of help with switch()Hello, nice to meet you all (first post here).
I'm trying to code something for work that will calculate a deduction based on age values in a switch statement, but im getting an error and I have no idea why. I'm wondering if these are valid case values, it's a parser error before the > on the first case, but I don't understand how that could be unless the case statements are not valid. But then why would only the first one have the error? Anyway, if anyone could help me out at all I'd really appriciate it, I'm kinda at a loss here as to what to do. Thanks for any reply, here is the code sample: (The EE_cost variable is already declared earlier in the code, but I don't want to paste the entire code because there are no other errors to worry about and its a lot of code to sift through) CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
|
case statements can only take one integer... but you can have multiple cases...
CPP / C++ / C Code:
Without the 'breaks', all code would be executed, break exits the switch after a successful case. in your case, with ranges, you are probably better off with 'if' compares. Last edited by LuciWiz : 13-Apr-2005 at 01:18.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#3
|
|||
|
|||
|
Thanks a lot, I was hoping I wouldn't have to recode with if statements but I guess it's inevitable unless I want to code a rediculous amount of cases. I guess I'll just have to think about which would be more efficient.
I appriciate the quick reply, thanks again. |
|
#4
|
||||
|
||||
|
Hello,
Welcome to GIDForums™. The first question I have for you is: Must you use a switch statement? If not, I believe this task would be best served with an if-else statement. Let me explain why. The switch statement The switch statement is a multi-way decision that tests whether an expression matches one of a number or constant integer values, and branches accordingly: CPP / C++ / C Code:
Each case is labeled by one or more integer-valued constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order. The break statement causes an immediate exit from the switch. Because cases serve just a labels, after the code or one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch. A break statement can also be used to force an immediate exit from while, for, and do loops. The if statement The sequence of if statments is the most general way of writing a multi-way decision. The expressions are evaluated in order; if any expression is true, the statment associated with it is executed, and this terminates the whole chain. As always, the code for each statment is either a single statement, or a group in braces. The If-Else statement is used to express decisions. Formally, the syntax is: CPP / C++ / C Code:
The Else-If statement has a syntax of: CPP / C++ / C Code:
Conclusion To say the least, the switch statement tests only one of a number or constant integer values, where-as the if-else statement isn't as limited. You could use the if statement as the following: CPP / C++ / C Code:
If you have to use a switch statement, you can implement a fall-through procedure shown previously by rdrast. If you have further questions, please feel free to ask. - Stack Overflow __________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility. |
|
#5
|
|||
|
|||
|
Quote:
First, worry about correct. Efficient is often very very deceptive. And before you even begin to worry about optimizing your code, you should profile it to see if what you THINK is a bottleneck really is. If you are calling this in response to a user's input, or even reading in a file from a disk, the I/O time will far exceed the time for a dozen compares or so. |
|
#6
|
|||
|
|||
|
Thanks to both of you, the program works flawlessly now, and the boss is pleased.
Really appriciate the helpful advice, cant thank you 2 enough. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| switch statement | kai85 | C Programming Language | 4 | 02-Apr-2005 05:02 |
| secound level php switch &id= | nophoto | MySQL / PHP Forum | 4 | 31-Mar-2005 15:29 |
| Problems with switch statement | dontcare | C++ Forum | 4 | 29-Nov-2004 19:28 |
| Re: Command Line Arguments, Part 1 | WaltP | C Programming Language | 0 | 11-Jul-2004 00:34 |
| Help a C++ Idiot: I am trying to fill an array based on a switch statement. | Psycop | C Programming Language | 2 | 14-Apr-2004 04:12 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The