473,480 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Another doubt when using switch

Hi.

I am trying to use switch this way, without success:

//thedata is a numeric variable
switch (thedata) {
case < 10:
...
case < 20:
...
case <= 30:
...
}

It seems that switch can only test for single string values. Can't I
make this kind of test using switch?

Thanks,
Robert Scheer
Jul 20 '05 #1
4 1364
Hello,

You can't use 'switch' like that...you can check for exact values only, as:

switch(value)
{
case 6:
case 7:
case 8:
// do something
break;
case 100:
// do something else...
break;
default:
// default value...
break;
}

If you need to compare for ranges rather than exact values then the 'if/else
if' is what you need:

if (x < 10) { ...... }
else if (x > 30) { .....}
else { .......}

--
Elias
"Robert Scheer" <rb******@my-deja.com> wrote in message
news:cf**************************@posting.google.c om...
Hi.

I am trying to use switch this way, without success:

//thedata is a numeric variable
switch (thedata) {
case < 10:
...
case < 20:
...
case <= 30:
...
}

It seems that switch can only test for single string values. Can't I
make this kind of test using switch?

Thanks,
Robert Scheer

Jul 20 '05 #2
> I am trying to use switch this way, without success:

//thedata is a numeric variable
switch (thedata) {
case < 10:
...
case < 20:
...
case <= 30:
...
}

It seems that switch can only test for single string values. Can't I
make this kind of test using switch?


Buy a book. You can't expect to program in total ignorance. Fortify yourself
with specific knowledge.

In this case, use an if statement.

if (thedata < 10) {
...
} else if (thedata < 20) {
...
} else if (thedata <= 30) {
....
}

http://www.crockford.com/javascript/javascript.html

Jul 20 '05 #3
On Fri, 16 Jan 2004 07:42:39 -0800, Douglas Crockford <no****@covad.net>
wrote:
Buy a book. You can't expect to program in total ignorance. Fortify
yourself with specific knowledge.


Even use of the JavaScript language reference would do...

Mike

http://devedge.netscape.com/central/javascript/

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
JRS: In article <bu************@ID-161723.news.uni-berlin.de>, seen in
news:comp.lang.javascript, lallous <la*****@lgwm.org> posted at Fri, 16
Jan 2004 17:05:59 :-
"Robert Scheer" <rb******@my-deja.com> wrote in message
news:cf**************************@posting.google. com...
I am trying to use switch this way, without success:

//thedata is a numeric variable
switch (thedata) {
case < 10:
...
case < 20:
...
case <= 30:
...
}

It seems that switch can only test for single string values. Can't I
make this kind of test using switch?


You can : see below previous response.
Responses should go after trimmed quotes, as per Wednesday FAQ.
You can't use 'switch' like that...you can check for exact values only, as:
Well, yes and no.

If you need to compare for ranges rather than exact values then the 'if/else
if' is what you need:

if (x < 10) { ...... }
else if (x > 30) { .....}
else { .......}


No. It can be used, but it is not needed.

var X = 265 // the variable
switch(true) {
case X<10 : alert('<10') ; break
case X<20 : alert('<20') ; break
case X<30 : alert('<30') ; break
default : alert('big') }
A post here or elsewhere a few days/weeks ago provided a clue from which
the above is derived. The code appears to be about as long as that with
nested IF; appears legal; can easily be laid out readably; works (at
least in eval in MSIE4); and ISTM that it should be of similar speed.
Advantage: case & break fulfil the role of brackets; more typing, but
easier to pair them up correctly.

To confuse readers : change true to false and reverse the relational
operators ( < to >= ).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5

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

Similar topics

188
7035
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
1
3534
by: DjordjeD | last post by:
I need to call(reference) class member "PrintDestination.PrintDestinationPrinter" from another file. How do I do it, I tried "ReportRenderer.PrintDestination.PrintDestinationPrinter" but it...
17
8581
by: Barret Bonden | last post by:
As an old programmer just now looking at VB.net I have a question: How does one simply open one form from another ? I don't mean how does one create a new instance of that form , but rather how...
1
1200
by: maridu h.s.n.v.prasad | last post by:
subscribe end hai sir i am using the postgresql 7.2 my doubt is what is the command of to see the list of databases i create a database named "jntuoes" database created it's displayed but i...
0
12046
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
38
2663
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
8
1264
by: ZikO | last post by:
Hi. I appologise for noob problems but I don't know what Im doing wrong. Yesterday I descibed a strange behavoiur of VB, as transparency doesn't work for all the time the program work. It work...
3
2668
by: asadikhan | last post by:
Hi, I have written a windows application with a GUI (let's call it MENU). I own the code for this application and have access to it. We have another application that is a third-part windows...
4
4783
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have winForm that needs update Panel (add controls to Panel of SplitContainer dynamically). I have a Backgroundworker, and DoWork event I want to add controls but I get this...
0
7051
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,...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5353
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,...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
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 ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
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...

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.