By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

switch statements with expressions...

P: n/a
I'm trying to do the following switch statement

switch i {
case '> 1':
break;
case '=1':
break;
case '<1':
break;
}

Are expressions such as '>1' allowable in a case ?

Thanks
Mike
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Mike Berger" <mb***************@skypoint.com> writes:
I'm trying to do the following switch statement

switch i {
case '> 1':
break;
case '=1':
break;
case '<1':
break;
}

Are expressions such as '>1' allowable in a case ?


Sure. It just doesn't do what you think it does. :)

You write '>1', which is a string literal. Used in a case, it will
match if "i" contains the string ">1".

With switch/case, you only tests for *equality*. Use if statements instead:
---
if (i<1) {
...
} else if (i>1) {
...
} else { // i==1
...
}
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.