CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    Why do we have only costant value's or constant value expression in SWITCH statement

    Hi,
    Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?

    Is there any technical reason behind or its just compiler standard ?

    Thanks
    Kiran

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why do we have only costant value's or constant value expression in SWITCH statem

    Quote Originally Posted by rsodimbakam
    Hi,
    Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?
    Those are the rules of the language.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Why do we have only costant value's or constant value expression in SWITCH statem

    If the value can be change and no case will be match after changed.

    Think logically.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Because switch is a glorified goto and the targets are defined statically at compile time, not computed dynamically at run-time.

    Or, as Paul put it, "them's the rules".
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by rsodimbakam
    Can anyone provide me some information about why do we have only constant value's or constant value expression be in SWITCH case statement ?

    Is there any technical reason behind or its just compiler standard ?
    To my knowledge most (if not all) languages require the case labels to be compile-time constants. What differs is that in some languages the switch doesn't have to evaluate to an integer.
    Last edited by _uj; September 25th, 2008 at 06:30 AM.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by Graham
    Because switch is a glorified goto
    The switch offers a unique functionality namely that the case labels are evaluated without order. This cannot be mimiced by any combination of other control structures.

    So the switch is not just a "glorified goto". It has a unique role among the common control structures.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Please don't use apostrophes to make words plural. I can't understand why that simple little rule confuses so many people.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by _uj
    To my knowledge most (if not all) languages require the case labels to be compile-time constants. What differs is that in some languages the switch doesn't have to evaluate to an integer.
    PHP is an exception, e.g.,
    Code:
    switch ($x)
    {
    case $a:
        echo 'a';
        break;
    case $b:
        echo 'b';
        break;
    case $a + $b:
        echo 'a+b';
        break;
    default:
        echo 'none of the above';
    }
    where $x, $a and $b are variables whose values are only known at run time.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by GCDEF
    Please don't use apostrophes to make words plural. I can't understand why that simple little rule confuses so many people.
    Your picky :P
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by Hobson
    Your picky :P
    You're picky.

    Yes, I'm a grammar cop, I'll admit. Still, most people here will be out in the professional world and proper use of the written language is important.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by _uj
    The switch offers a unique functionality namely that the case labels are evaluated without order. This cannot be mimiced by any combination of other control structures.

    So the switch is not just a "glorified goto". It has a unique role among the common control structures.
    What?

    What does "case labels are evaluated without order" mean?

    The only difference between switch and FORTRAN's computed GOTO is syntactic sugar. I could do anything that switch can do in FORTRAN. The only complication is that I would need to map (the used values of) the switch variable on to a consecutive sequence of integer values starting at one. That the compiler in C++ does this for me is, as I said, syntactic sugar.

    E.g.
    Code:
    switch (i)
    {
    case 4:
       // do stuff
       break;
    
    case 73:
       // do other stuff
       break;
    
    default:
       // more stuff
       break;
    }
    in FORTRAN:
    Code:
       INTEGER J
        IF (I.EQ.4) THEN
    	  J = 1
        ELSEIF (I.EQ.73) THEN
    	  J = 2
       ELSE
    	  J = 3
       ENDIF
       GOTO(100, 200, 300) J
    100 do stuff
       GOTO 1000
    
    200 do other stuff
       GOTO 1000
    
    300 more stuff
       GOTO 1000
    
    1000 ...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by Graham
    What?

    What does "case labels are evaluated without order" mean?

    The only difference between switch and FORTRAN's computed GOTO is syntactic sugar. I could do anything that switch can do in FORTRAN. The only complication is that I would need to map (the used values of) the switch variable on to a consecutive sequence of integer values starting at one. That the compiler in C++ does this for me is, as I said, syntactic sugar.
    You can implement any control structure available in C++ in assembly and that would involve gotos. So if you would call all control structures "glorified gotos" I would agree, but if you single out the switch, I don't.

    As a control structure the switch has a unique property. The label cases are in no specific order. That's what characterizes a switch and that's for example why an if-chain isn't functionally equivalent with a switch. That's also why a switch can be implemented in O(1) using say a table of some kind.

    The fact that you (or a compiler) can accomplish the functional equivalent of a switch with gotos in some language is irrelevant. The switch has a unique property that no combination of other control structures can mimic. It's not just a "glorified goto".
    Last edited by _uj; September 25th, 2008 at 04:14 PM.

  13. #13
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by laserlight
    PHP is an exception, e.g.,
    Code:
    switch ($x)
    {
    case $a:
        echo 'a';
        break;
    case $b:
        echo 'b';
        break;
    case $a + $b:
        echo 'a+b';
        break;
    default:
        echo 'none of the above';
    }
    where $x, $a and $b are variables whose values are only known at run time.
    Isn't PHP an interpreted language (like PERL)?

    Viggy

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by MrViggy
    Isn't PHP an interpreted language (like PERL)?
    Yes, PHP code is typically interpreted at run time (after all, C++ is also interpreted, but at compile time, heh).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Aug 2007
    Posts
    858

    Re: Why do we have only costant value's or constant value expression in SWITCH statement

    Quote Originally Posted by laserlight
    Yes, PHP code is typically interpreted at run time (after all, C++ is also interpreted, but at compile time, heh).
    Heh, well, unless you're going to directly write in machine code every language has to be interpreted at some point.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured