Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch statement

Reply
Thread Tools

switch statement

 
 
D
Guest
Posts: n/a
 
      12-01-2004
Can a switch statement have an arithmetic attribute in it. For example
switch (x / 10)
{
case a: place your statements here
}


Thanks
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      12-01-2004
D wrote in news:(E-Mail Removed) m in
comp.lang.c++:

> Can a switch statement have an arithmetic attribute in it. For example
> switch (x / 10)
> {
> case a: place your statements here
> }
>
>


No idea what you mean by "arithmetic attribute in it", if you mean
a non-compiletime-constant, then no.

int const ten = 10;

int main()
{
int x = 100, a = 10;
switch ( x / 10 )
{
case a: // illegal must be a compile time constant
case 10: // ok
case ten; // also ok.
}
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2004
D wrote:
> Can a switch statement have an arithmetic attribute in it. For example
> switch (x / 10)
> {
> case a: place your statements here
> }


Yes.

V
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-01-2004
D wrote:

> Can a switch statement have an arithmetic attribute in it. For
> example switch (x / 10)
> {
> case a: place your statements here
> }



You mean expression, not attribute. The answer is yes, as long as the
expression yields an integer type. Assuming x is an integer type, then
your example above will have integer division and all will be well.



Brian
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-01-2004
Rob Williscroft wrote:

> D wrote in news:(E-Mail Removed) m in
> comp.lang.c++:
>
> > Can a switch statement have an arithmetic attribute in it. For
> > example switch (x / 10)
> > {
> > case a: place your statements here
> > }
> >
> >

>
> No idea what you mean by "arithmetic attribute in it", if you mean
> a non-compiletime-constant, then no.



Ah, you read it as asking about the case labels. I confess that now I'm
not clear what was meant. I took it to mean the controlling expression.
You're right, of course, about case labels.




Brian
 
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