User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 360,995 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,431 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

Logical operations and data-checks

Join Date: Feb 2006
Location: UK
Posts: 461
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 39
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Logical operations and data-checks

  #2  
Dec 28th, 2006
Correct, this sort of thing is known as short circuit evaluation.

it also happens under logical OR evaluation when the left-hand-side of the expression yields 'true', ie
  1. if ( true || false )
The program knows that there is no need to evaluate the right-hand-side, since the result of the boolean expression is already known for certain.

As you see in the example you've found, this is sometimes used in subtle ways to perform checks before executing a statement - Which is not always a good idea IMHO, because it runs the chance of being accidentally changed to introduce weird bugs! For example
  1. int n=0;
  2. if ( n!=0 && 10/n == 2 )
if this check was improperly altered in this block of code, the program could crash
  1. int n=0;
  2. if ( 10/n == 2 && n!=0 )
  3. //Bad: Looming catastrophe!
Last edited by Bench : Dec 28th, 2006 at 2:35 pm.
¿umop apisdn upside down?
Reply With Quote  
All times are GMT -4. The time now is 6:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC