www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/11/00:33:47

Date: Tue, 10 Feb 1998 21:32:31 -0800 (PST)
Message-Id: <199802110532.VAA05804@adit.ap.net>
Mime-Version: 1.0
To: nguyen2 AT tidalwave DOT net_REMOVE (Linh Nguyen), djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Beginner, switch statement in DJGPP

At 08:34  2/10/1998 GMT, Linh Nguyen wrote:
>Hi.  I use Borland C++ 5 at school, and coded the switch statement as
>follows...
>
>switch(Num)
>{
>case 1, 10, 6: cout << "Test"; break;
>....... and so on and so forth
>}
That is a nonstandard extension made by Borland. It is not part of Standard
ANSI C, so if you use it your program is not portable.
>
>but in DJGPP, it won't let me do that, it only lets me have one
>possibility for each case statement, so it would be like this..
>
>case 1:  ...
>case 10:  ...
>case 6:  ...
That's correct. However, since by default in C a `switch' case falls
through, you can do this:
switch(foo)
   {
        case 1:
        case 10:
        case 6:
                cout << "Test"; 
                break;
   /* ... */
   }

GCC does provide an extension to let you use ranges in `case' expressions,
so that

        case 1 ... 4: /* stuff */

is equivalent to:

        case 1:
        case 2:
        case 3:
        case 4: /* stuff */

See the info page "gcc" "C Extensions" "Case Ranges" for more info and
caveats. Note that this also is not standard.
>
>Is there any way around this?  Thanks for any help!
>
>
>

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019