delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/05/02:12:54

Date: Mon, 5 May 1997 08:45:39 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: DBerry1 AT dca DOT gov DOT au
cc: djgpp AT delorie DOT com
Subject: Re: Check a range of numbers with case
In-Reply-To: <0000jjzjjbzl.0000iswkaeyu@dca.gov.au>
Message-ID: <Pine.SUN.3.91.970505084450.11050B-100000@is>
MIME-Version: 1.0

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 -


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