473,805 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ 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 1381
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.goo gle.com...
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.n et>
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.******@blueyo nder.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.o rg> posted at Fri, 16
Jan 2004 17:05:59 :-
"Robert Scheer" <rb******@my-deja.com> wrote in message
news:cf******* *************** ****@posting.go ogle.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
7266
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 - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
1
3554
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 doesn't work. Please help! here is my class: *****************************************
17
8629
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 to refer to THAT form ? And having done this, how does one get data to and from that form ? I have read the MS tutorial on this but it is senselessly complex; there must be a simple, straightforward method, or I can only assume that MS has...
1
1226
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 want to know the command so please give me the replay
0
12090
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
38
2735
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 then the SEGMENTATION FAULT is occuring. Please tell me what are all the cases this problem may occur if we use strcpy().
8
1276
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 only when I run prog. Today I found another thing. I have music in background and use WindwosMediaPlayer. The code is: Dim WithEvents MusicPlayer As New WMPLib.WindowsMediaPlayer
3
2695
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 application with a GUI interface as well (let's call it BASE). I don't have access to the code of this application. This application has hooks through which other applications can be called. I have defined a hook in this application which bring up...
4
4805
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 error in Spanish (I don't get translation: Los controles creados en un subproceso no pueden tener controles primarios en un control en un subproceso diferente.)
0
9718
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
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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...
1
10369
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
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...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.