www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Switch Statement case '0': .. case '9'

reply "Paul" <phshaffer gmail.com> writes:
The book in section 3.5 gives these valid case examples:

case ’0’: .. case ’9’:
case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’:

In my code:

case "STEP01": ...compiles and works correct
case "STEP01", "STEP02": ...compiles and works correct
case "STEP01": .. case "STEP50": ...compiler reports:

iccstats.d(27): Error: Integer constant expression expected 
instead of "STEP01"
iccstats.d(27): Error: Integer constant expression expected 
instead of "STEP50"
iccstats.d(27): Error: integral constant must be scalar type, not 
const(char[])

Help please.
Jun 14 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 06/14/2012 05:59 PM, Paul wrote:
 The book in section 3.5 gives these valid case examples:

 case ’0’: .. case ’9’:
 case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
 case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’:

 In my code:

 case "STEP01": ...compiles and works correct
 case "STEP01", "STEP02": ...compiles and works correct
 case "STEP01": .. case "STEP50": ...compiler reports:

 iccstats.d(27): Error: Integer constant expression expected instead of
 "STEP01"
 iccstats.d(27): Error: Integer constant expression expected instead of
 "STEP50"
 iccstats.d(27): Error: integral constant must be scalar type, not
 const(char[])

 Help please.
case-range expressions don't work with strings. (because there is no general way to meaningfully fill in the gap.) You could automatically generate the case statements via a string mixin (untested): mixin({ string r; foreach(i;1..51) r~=`case "STEP`~(i<10?"0":"")~to!string(i)~`":`; return r; }()); But that is probably overkill, why not just parse the string with an if statement? If you need fallthrough, you can use a goto statement if(...) goto Lstepn; switch(...){ case ...: ... break; Lstepn: case ...: ... break; }
Jun 14 2012
parent reply "Paul" <phshaffer gmail.com> writes:
On Thursday, 14 June 2012 at 16:16:19 UTC, Timon Gehr wrote:
 On 06/14/2012 05:59 PM, Paul wrote:
 The book in section 3.5 gives these valid case examples:

 case ’0’: .. case ’9’:
 case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
 case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’:

 In my code:

 case "STEP01": ...compiles and works correct
 case "STEP01", "STEP02": ...compiles and works correct
 case "STEP01": .. case "STEP50": ...compiler reports:

 iccstats.d(27): Error: Integer constant expression expected 
 instead of
 "STEP01"
 iccstats.d(27): Error: Integer constant expression expected 
 instead of
 "STEP50"
 iccstats.d(27): Error: integral constant must be scalar type, 
 not
 const(char[])

 Help please.
case-range expressions don't work with strings. (because there is no general way to meaningfully fill in the gap.) You could automatically generate the case statements via a string mixin (untested): mixin({ string r; foreach(i;1..51) r~=`case "STEP`~(i<10?"0":"")~to!string(i)~`":`; return r; }()); But that is probably overkill, why not just parse the string with an if statement? If you need fallthrough, you can use a goto statement if(...) goto Lstepn; switch(...){ case ...: ... break; Lstepn: case ...: ... break; }
What are those examples I quoted from the book? Aren't they string ranges?
 case ’0’: .. case ’9’:
 case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Jun 14 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Paul:

 What are those examples I quoted from the book?  Aren't they 
 string ranges?

 case ’0’: .. case ’9’:
 case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Those are char ranges. Bye, bearophile
Jun 14 2012
parent "Paul" <phshaffer gmail.com> writes:
On Thursday, 14 June 2012 at 16:49:16 UTC, bearophile wrote:
 Paul:

 What are those examples I quoted from the book?  Aren't they 
 string ranges?

 case ’0’: .. case ’9’:
 case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Those are char ranges. Bye, bearophile
Thanks.
Jun 14 2012