473,698 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Noobie question about Switch

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 ???

// execute if myfloat >0

break;

case 0:

// execute if myfloat ==0

break;

default:

// you got the idea
}

TIA
Aristotelis
Jul 22 '05 #1
6 1941
Hello,

"Aristoteli s E. Charalampakis"
<ar************ ***********@REM OVEMEhotmail.co m> wrote in message
news:bs******** ***@ulysses.noc .ntua.gr...
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 ???

// execute if myfloat >0

break;

case 0:

// execute if myfloat ==0

break;

default:

// you got the idea
}

TIA
Aristotelis


You can use if/else if/else combos or some sort of control table of your
design:
float range[3*2] = { 0.1, 0.9, 1, // if greater than 0.1 and below 0.9 then
integer i = 1
1.1, 2.1, 2}; // if 1.1 and 2.1 then 2, etc..

--
Elias
Jul 22 '05 #2
So it is not possible to write something like
"case >0" ?

Can it be written in the form

switch ( TRUE )
case myFloat>0:
etc?

In VB I can write something like this:

select case myFloat
case 2 , 3
'Do something
case Is > 4
'do something else
case else
'default
end select

Or

select case True
case myFloat>4
'Do something
case myFloat<0
'do something else
case else
'default
end select

Is there a way to implement the above with the switch statement?

TIA,
Aristotelis

"lallous" <la*****@lgwm.o rg> wrote in message
news:bs******** ****@ID-161723.news.uni-berlin.de...
Hello,

"Aristoteli s E. Charalampakis"
<ar************ ***********@REM OVEMEhotmail.co m> wrote in message
news:bs******** ***@ulysses.noc .ntua.gr...
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 ???

// execute if myfloat >0

break;

case 0:

// execute if myfloat ==0

break;

default:

// you got the idea
}

TIA
Aristotelis
You can use if/else if/else combos or some sort of control table of your
design:
float range[3*2] = { 0.1, 0.9, 1, // if greater than 0.1 and below 0.9

then integer i = 1
1.1, 2.1, 2}; // if 1.1 and 2.1 then 2, etc..
--
Elias

Jul 22 '05 #3

"Aristoteli s E. Charalampakis" <ar************ ***********@REM OVEMEhotmail.co m> wrote in message
In VB I can write something like this:
We're not in VB anymore.
select case myFloat
case 2 , 3

In C/C++:
case 2: case 3:

There's no way to do the relationals directly. You could do something like:

switch(i) {
case 2:
case 3:
...

default:
if(i > 10) { ...

You can't do switch with floats at all. The arg has to be an integer or
enum type.
Jul 22 '05 #4
Aristotelis E. Charalampakis wrote:
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 )
You cannot use switch/case on floating point values, only integral
values.
{

case >0: // ??? how do i write this ???

// execute if myfloat >0

break;

case 0:

// execute if myfloat ==0

break;

default:

// you got the idea
}


There is no way to do that. Just use if:

if (myFloat == 0)

// execut if myFloat == 0

else if (myFloat > 0)

// esecute if myFloat > 0

else

// you got the idea

Btw: testing floating point values for equality is most often a bad idea
Jul 22 '05 #5
Thank you all. I will stick to the if / else combos.
Btw: testing floating point values for equality is most often a bad idea


Yes, it is a bad idea. I am not noobie in general, just in C++.

Yet, I have persuaded myself to write some dlls (mostly math stuff) in C++,
so that I get to know it. Some other dlls are in Visual Fortran 6.5, the gui
in VB.

Aristotelis.
Jul 22 '05 #6
"Aristoteli s E. Charalampakis" <ar************ ***********@REM OVEMEhotmail.co m> wrote in message news:<bs******* ****@ulysses.no c.ntua.gr>...
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.:


A way of doing it (just as tiring as if-else though) is:

switch (myFloat > 0) {
case true: blah blah; break; //myfloat > 0
case false: switch (myFloat == 0) {
case true: blah blah; break; //myfloat == 0
case false: blah blah; break; //myfloat < 0
}
break;
}
Jul 22 '05 #7

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

Similar topics

10
10905
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
15
7313
by: YitzHandel | last post by:
The following is an extract from my program, which I seperated out and compiled as its own program. The problem is that as soon as a case tests true, all the cases below it execute as well. So if the user inputs, 4, the statements from cases 4-9 will execute. (I replaced function calls with cout for testing.) I am using Win2k, Open Watcom IDE v 1.3.
18
3453
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
10
3704
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
3
1212
by: QQ | last post by:
Hi here is my short program void main() { while(1) { int command; scanf("%d", &command); switch(command) {
10
12319
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a program to demonstrate how a switch without breaks behaves vs. how I expected it to behave. The code includes: (1) a switch statement with breaks (2) the if/else statements that have the same results as (1) (3) a switch statement without breaks...
2
1732
by: jalqadir | last post by:
In my program I have the following statements void Person::setTitle(const Glib::ustring& str) { switch ( str ) { case "None" : { title = title_t::None; break; } case "Dr" : { title = title_t::Dr;
13
11813
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.
1
2071
by: Josh Ibrahim | last post by:
Hello all :D, I am currently learning Java. I have a question about switch statements, in this program I am trying to use a switch statement instead of an If/else sequence to display a letter grades numerical value; however I would also like to include/exclude the '+' and '-' characters. example: enter a letter grade with capital letters(example a = A or b+ = = B+): input: A output:the numeric value is: 4.0 // switch sequence starts to...
0
8597
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
9148
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...
1
8884
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
8855
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
7708
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...
1
6515
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
5857
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
4358
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...
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.