switch statement

D

D

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


Thanks
 
R

Rob Williscroft

D wrote in 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.
 
D

Default User

D said:
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
 
D

Default User

Rob said:
D wrote in in
comp.lang.c++:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top