From: "Art S. Kagel" Newsgroups: comp.os.msdos.djgpp Subject: Re: Newbie Question: Index lookup coord. checking... Date: Fri, 1 Aug 1997 09:55:53 -0400 Lines: 37 Message-ID: References: <5rif6n$5v1 AT news DOT velocity DOT net> NNTP-Posting-Host: 160.43.8.60 Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <5rif6n$5v1@news.velocity.net> To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Mon, 28 Jul 1997, Badman wrote: > Hello, I have a simple question: Can you check coordinates for an index > lookup (so you don't get an error) and check that same index in the same > if() statement? > > For example: > if (x > 0 && x < 640 && y > 0 && y < 480 && *(screen+(y * 640)+x) > 3){ > > /* do stuff */ > }; > > or.... > > > if (x > 0 && x < 640 && y > 0 && y < 480){ > if (*(screen+(y * 640)+x)>3){ > }; > }; > > which is better? Any help would be appreciated... Both are good in the sense that both will work exactly the same way. However, the latter (separate if's) is MUCH easier to read an maintain and no less efficient at runtime. BTW: *(screen+(y * 640)+x) and screen[y][x] are not only equivalent, but the GCC optimizer can actually generate better runtime code from the array form in some situations. Try the timings yourself. It is rarely slower and also MUCH easier to read and maintain. Art S. Kagel, kagel AT bloomberg DOT com