Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding Switch statement

Reply
Thread Tools

Regarding Switch statement

 
 
nrk
Guest
Posts: n/a
 
      02-20-2004
CBFalconer wrote:

> nrk wrote:
>>

> ... snip ...
>>
>> However, for stylistic reasons, when checking more than 3 if-else
>> if-else if style conditions, I would pick switch.

>
> Please explain how you handle the following with switch:
>
> if (v > 10000) i = 0;
> else if (v > 1000 ) i = 3;
> else if (v > 100 ) i = 5;
> else if (v > 10 ) i = 2;
> else if (v > 5 ) i = 1;
> else i = 0;
>
> and be prepared to reorganize either column of values at any time,
> including the number of cases to be handled.
>
> The above table is closely related to the Bush regimes tax
> policies in the US.
>


Try to engage your brain please I was obviously talking about
situations where a switch would be appropriate. If that wasn't obvious to
you, that's your lookout, not mine.

-nrk.

--
Remove devnull for email
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      02-20-2004
nrk wrote:
> CBFalconer wrote:
> > nrk wrote:
> >>

> > ... snip ...
> >>
> >> However, for stylistic reasons, when checking more than 3 if-else
> >> if-else if style conditions, I would pick switch.

> >
> > Please explain how you handle the following with switch:
> >
> > if (v > 10000) i = 0;
> > else if (v > 1000 ) i = 3;
> > else if (v > 100 ) i = 5;
> > else if (v > 10 ) i = 2;
> > else if (v > 5 ) i = 1;
> > else i = 0;
> >
> > and be prepared to reorganize either column of values at any time,
> > including the number of cases to be handled.
> >
> > The above table is closely related to the Bush regimes tax
> > policies in the US.

>
> Try to engage your brain please I was obviously talking about
> situations where a switch would be appropriate. If that wasn't
> obvious to you, that's your lookout, not mine.


Now now, let's not get testy Looking up I distinctly see the
prequisite "more than 3 if else if" clauses. We do tend to
require precision around here, and I have been bitten by it often
enough. Acting like Bushmen is not appropriate.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
 
Alan Balmer
Guest
Posts: n/a
 
      02-20-2004
On Fri, 20 Feb 2004 15:10:00 GMT, nrk
<(E-Mail Removed)> wrote:

>> policies in the US.
>>

>
>Try to engage your brain please I was obviously talking about
>situations where a switch would be appropriate. If that wasn't obvious to
>you, that's your lookout, not mine.


It's harder to code political statements with switch.

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      02-20-2004
CBFalconer wrote:

> nrk wrote:
>>

> ... snip ...
>>
>> However, for stylistic reasons, when checking more than 3 if-else
>> if-else if style conditions, I would pick switch.

>
> Please explain how you handle the following with switch:
>
> if (v > 10000) i = 0;
> else if (v > 1000 ) i = 3;
> else if (v > 100 ) i = 5;
> else if (v > 10 ) i = 2;
> else if (v > 5 ) i = 1;
> else i = 0;
>
> and be prepared to reorganize either column of values at any time,
> including the number of cases to be handled.
>
> The above table is closely related to the Bush regimes tax
> policies in the US.
>


If you must insist on a switch solution to this particular case... then:

static const int vtable[] = { 10000, 1000, 100, 10, 5, };
static const int itable[] = { 0, 3, 5, 2, 1, };
int index = 0, test = 0;

while ( index < sizeof vtable/sizeof vtable[0] ) {
switch ( test ) {
case 1:
i = itable[index-1];
goto end;
case 0:
test = index < sizeof vtable/sizeof vtable[0] &&
(v > vtable[index++]);
break;
}
}
i = 0;
end:
/* use i here */

This just re-inforces the idea that it is probably better to engage the
muscle between the ears than to take statements on style out of context and
try to apply them willy-nilly At least, you didn't ask me for C&V for
that stylistic statment!

-nrk.
--
Remove devnull for email
 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      02-20-2004
nrk wrote:

> CBFalconer wrote:
>
>> nrk wrote:
>>>

>> ... snip ...
>>>
>>> However, for stylistic reasons, when checking more than 3 if-else
>>> if-else if style conditions, I would pick switch.

>>
>> Please explain how you handle the following with switch:
>>
>> if (v > 10000) i = 0;
>> else if (v > 1000 ) i = 3;
>> else if (v > 100 ) i = 5;
>> else if (v > 10 ) i = 2;
>> else if (v > 5 ) i = 1;
>> else i = 0;
>>
>> and be prepared to reorganize either column of values at any time,
>> including the number of cases to be handled.
>>
>> The above table is closely related to the Bush regimes tax
>> policies in the US.
>>

>
> If you must insist on a switch solution to this particular case... then:
>
> static const int vtable[] = { 10000, 1000, 100, 10, 5, };
> static const int itable[] = { 0, 3, 5, 2, 1, };
> int index = 0, test = 0;
>
> while ( index < sizeof vtable/sizeof vtable[0] ) {


Dang it! That's not going to work, is it?

> switch ( test ) {
> case 1:
> i = itable[index-1];
> goto end;
> case 0:
> test = index < sizeof vtable/sizeof vtable[0] &&
> (v > vtable[index++]);
> break;
> }
> }
> i = 0;
> end:
> /* use i here */


Let's try again:

static const int vtable[] = { 10000, 1000, 100, 10, 5, };
static const int itable[] = { 0, 3, 5, 2, 1, };
int index = 0, test = 0;

while ( index < sizeof vtable/sizeof vtable[0] ) {
switch ( test ) {
case 1:
i = itable[index];
goto end;
case 0:
test = (v > vtable[index] || (++index, 0));
break;
}
}
i = 0;
end:
/* use i here */

I think there's a lesson somewhere in this for me, but I refuse to learn

-nrk.

>
> This just re-inforces the idea that it is probably better to engage the
> muscle between the ears than to take statements on style out of context
> and
> try to apply them willy-nilly At least, you didn't ask me for C&V for
> that stylistic statment!
>
> -nrk.


--
Remove devnull for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2004
CBFalconer <(E-Mail Removed)> writes:
[...]
> Now now, let's not get testy Looking up I distinctly see the
> prequisite "more than 3 if else if" clauses. We do tend to
> require precision around here, and I have been bitten by it often
> enough. Acting like Bushmen is not appropriate.


<OT>
I'm sure you didn't intend that as an ethnic slur. The Bushmen are an
actual ethnic group in southern Africa. (There's been some debate, I
think, about whether "Bushmen" is an appropriate term, but the brief
Googling I just did indicates that it's now generally accepted.)
</OT>

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-21-2004
On Fri, 20 Feb 2004 11:42:44 -0700, in comp.lang.c , Alan Balmer
<(E-Mail Removed)> wrote:

>On Fri, 20 Feb 2004 15:10:00 GMT, nrk
><(E-Mail Removed)> wrote:
>
>>> policies in the US.
>>>

>>
>>Try to engage your brain please I was obviously talking about
>>situations where a switch would be appropriate. If that wasn't obvious to
>>you, that's your lookout, not mine.

>
>It's harder to code political statements with switch.


Wasn't it an american president who said
"walk softly, but carry a big switch?"



--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-21-2004
Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:
> [...]
> > Now now, let's not get testy Looking up I distinctly see the
> > prequisite "more than 3 if else if" clauses. We do tend to
> > require precision around here, and I have been bitten by it often
> > enough. Acting like Bushmen is not appropriate.

>
> <OT>
> I'm sure you didn't intend that as an ethnic slur. The Bushmen are an
> actual ethnic group in southern Africa. (There's been some debate, I
> think, about whether "Bushmen" is an appropriate term, but the brief
> Googling I just did indicates that it's now generally accepted.)


I didn't, but I did intend it to connote the present US regime in
temporary (we trust) power, who have been known to play fast and
loose with accuracy and precision.
</OT>

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments