Mail Archives: djgpp/1997/05/05/02:12:54
On 5 May 1997 DBerry1 AT dca DOT gov DOT au wrote:
> I've slowly been working my way through learning C and DJGPP, and I was
> wondering if it's possible to use a Case statement to check for a range of
> numbers - not just one....
>
> eg..
>
> Switch (bullet_x)
>
> Case (alien1_x to alien1_x + 16)
> then alien_hit = 1
> Case (alien2_x to alien2_x + 16)
> then alien_hit = 2
> Default
> alien_hit = 0
First, why not use "if...else if...else"? Are you sure you need
switch?
If you do, then there's a GCC-specific extension (which means you
won't be able to compile the code with anything but GCC), that goes
like this:
switch (bullet_x)
{
case alien1_x ... alien1_x + 16:
alien_hit = 1;
case alien2_x ... alien2_x + 16:
alien_hit = 2;
etc. Note that this will only work if alien1_x, alien2_x and
everything else between "case" and the colon ':' are all COMPILE-TIME
constants! That is the limitation of `switch'. For more about this
GCC extension, type this from the DOS prompt:
info gcc "C Extensions" "Case Ranges"
(the quotes are important).
- Raw text -