473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch..Case Statement Question.

Can someone tell me if the following Switch...Case construct is valid?
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.
So for example, will case 1-35: work? If this would work, will it
consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
me know if the following will work below:

switch(state){
case 1-35:
case 37:
do something_1;
break;
case 36:
do something_2;
break;
case 38-50:
do something_3;
break;
}
Thank you.
Andy.

Jul 23 '05 #1
21 7625
Andy wrote on 28 feb 2005 in comp.lang.javas cript:
Can someone tell me if the following Switch...Case construct is valid?
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.
So for example, will case 1-35: work? If this would work, will it
consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
me know if the following will work below:
did you test it yourself? If not why not?
switch(state){
case 1-35:
case 37:
do something_1;
break;


this part will execute if "state" is -34 or 37,
because 1-35 is 34, I think.

But please test it yourself.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
Lee
Andy said:

Can someone tell me if the following Switch...Case construct is valid?
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.
So for example, will case 1-35: work? If this would work, will it
consider all numbers between 1 and 35 inclusive of 1 and 35? Please let
me know if the following will work below:

switch(state ){
case 1-35:
case 37:
do something_1;
break;
case 36:
do something_2;
break;
case 38-50:
do something_3;
break;
}


With less effort and much less wait time, you could have just tested
your sample code yourself. Your first case will match any state that
equals your index value of -34 (1-35).

Jul 23 '05 #3
I apologize for not having tested myself and wasted your time. Do you
or anyone else knows of a way to check for multiple values without
stating each one like if I'd like to check all values from 1 through 35
how would i do it?

Any help will be appreciated.

Jul 23 '05 #4
Andy wrote:
Can someone tell me if the following Switch...Case construct is valid?
No.
I'm wanting to check for multiple values in the Case statement without
explicitly listing each values.


You cannot. The expression after the case keyword is compared for
strict equality (===) with the expression in the switch statement. In
other words,

switch(2) {
case '2': /* This statement list will not be evaluated. */
}

the statement list for case '2' will not be evaluated because the
expressions do not evaluate to the same type (one is a number, the
other is a string).

You have little choice but to use an if statement.

if(((1 <= state) && (35 >= state)) || (37 == state)) {
/* Between 1 and 35 (inclusive) or is 37 */
something_1();
} else if(36 == state) {
/* Is 36 */
something_2();
} else if((38 <= state) && (50 >= state)) {
/* Between 38 and 50 (inclusive) */
something_3();
}

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
Andy wrote on 28 feb 2005 in comp.lang.javas cript:
I apologize for not having tested myself and wasted your time. Do you
or anyone else knows of a way to check for multiple values without
stating each one like if I'd like to check all values from 1 through 35
how would i do it?

Please do not use usenet as a form of email. you are replying to many
here, so quote relevant part of the mail you answer on. Remember
netiquette.

Now "I" have to quote from your old posting:switch(state ){
case 1-35:
case 37:
do something_1;
break;
case 36:
do something_2;
break;
case 38-50:
do something_3;
break;

You could do:

state = 20;
switch(true){
case state>=1&&state <=35:
case state==37:
do something_1;
break;
case state==36:
do something_2;
break;
case state>=38&&stat e<=50:
do something_3;
break;
case default:
alert('default' )
}

This however in my view defies usefulness of switch().
anyway if-else is much more robust:

state = 20;
if ((state>=1&&sta te<=35)||state= =37){
do something_1
else if (state==36)
do something_2
else if (state>=38&&sta te<=50)
do something_3
else
alert('default' );

[not tested]

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #6
Andy wrote:
I apologize for not having tested myself and wasted your time. Do you
or anyone else knows of a way to check for multiple values without
stating each one like if I'd like to check all values from 1 through 35
how would i do it?

Any help will be appreciated.

This is where the "greater than or equal to" and "less than or equal to"
operators come in handy. For instance, ">=1 && <=35" is between one and
thirty-five.
Jul 23 '05 #7
You can also try

switch(true){

case(val>1 && val<35): do something;break ;
case(val>=35 && val <53):do something;break ;

Jul 23 '05 #8
bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
You can also try

switch(true){

case(val>1 && val<35): do something;break ;
case(val>=35 && val <53):do something;break ;


[please quote, usenet is not email]

is the above really more useful than:

if (val>1 && val<35) do_something;
if (val>=35 && val<53) do_something;

?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #9
rh
Evertjan. wrote:
bumbleguppy wrote on 05 mrt 2005 in comp.lang.javas cript:
You can also try

switch(true){

case(val>1 && val<35): do something;break ;
case(val>=35 && val <53):do something;break ;


[please quote, usenet is not email]

is the above really more useful than:

if (val>1 && val<35) do_something;
if (val>=35 && val<53) do_something;

?


I'd have to say "No", because the former is syntactically (block)
deficient. ;-)

However, the "switch/case" idiom can be more useful, or at least more
appropriate to use, in some circumstances because of the nature of the
structural encapsulation it provides for a specific filtering task.

The choice shouldn't have any effect on the result of the execution,
but it can have a marked effect on the readability and maintainability
of the code.

../rh

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1937
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch ( myFloat ) { case >0: // ??? how do i write this ???
35
8325
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 for '5'. So in his switch statement, he omits a case for '5':
18
3057
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
3
19733
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the...
2
1691
by: Cathleen C via DotNetMonster.com | last post by:
I'm a semi-beginner with c# and am having a problem effectively implementing a switch statement. I've created an asp.net app that runs a report depending on which item was selected from a drop down box from a previous page. I then have an Export button to convert the resultant report into pdf. I have it working but wanted to stream line the code. The oStream variable is set with the switch statement. After the switch I attempt to...
7
22124
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 else statements but it looked pretty ugly and the string values to be tested is still increasing. The switch statement seems to be a better choice then the if else statement. But it seems that the switch statement accepts integer values as the test...
13
11802
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
8015
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.