GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2005, 16:50
Caibre Caibre is offline
New Member
 
Join Date: Apr 2005
Posts: 3
Caibre is on a distinguished road

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:
                switch(EE_age)
                {
                     case > 17 && < 30: EE_cost = 98.73;
                     case > 29 && < 40: EE_cost = 102.38;
                     case > 39 && < 50: EE_cost = 144.43;
                     case > 49 && < 55: EE_cost = 192.89;
                     case > 54 && < 60: EE_cost = 245.92;
                     case > 59 && < 65: EE_cost = 293.43;
                     case > 64:         EE_cost = 358.35;
                }
  #2  
Old 12-Apr-2005, 17:01
rdrast rdrast is offline
New Member
 
Join Date: Apr 2005
Posts: 22
rdrast is on a distinguished road
case statements can only take one integer... but you can have multiple cases...
CPP / C++ / C Code:
 switch(EE_age)
{
   case 17 :
   case 18 :
   case 19 : 
     ...
   case 29 :
        EE_cost = 98.73;
        break;

   case 30 :
   case 31 :
     ...
   case 39 :
        EE_cost = 102.38;
        break;


   default:

} // end switch


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  
Old 12-Apr-2005, 17:05
Caibre Caibre is offline
New Member
 
Join Date: Apr 2005
Posts: 3
Caibre is on a distinguished road
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  
Old 12-Apr-2005, 17:09
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
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:
switch (expression) {
	case const-expr:	statements
	case const-expr:	statements
	default:	statements
}

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:
if (expression)
	statement1
else
	statment2
Code 1.1: Example of if-else statement

The Else-If statement has a syntax of:

CPP / C++ / C Code:
if (expression)
	statement
else if (expression)
	statement
else
	statement
Code 1.2: Example of else-if statement

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 (EE_age > 17 && EE_age < 30)
	/* Do something */
else if (/* ... */)
	/* Do something */
else	/* If all else fails... */
	/* Do something */

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  
Old 12-Apr-2005, 17:14
rdrast rdrast is offline
New Member
 
Join Date: Apr 2005
Posts: 22
rdrast is on a distinguished road
Quote:
Originally Posted by Caibre
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.

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  
Old 12-Apr-2005, 18:02
Caibre Caibre is offline
New Member
 
Join Date: Apr 2005
Posts: 3
Caibre is on a distinguished road
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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 07:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.