![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
Does C# evaluate the second half of a logic equation if the first half is false in a question like:
if ( A==B) && (C==D) ) { } If A !=B , would C==D even be evaluated? I'm guessing that this would be like other languages, but thought I'd ask just to be sure... Thanks Matt |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
|
Both must be true in your example.
|
|
|
|
|
|
#3 |
|
Senior Member
|
yes
![]() but will they both be *evaluated* better example... bool isfalse() { return false;} bool istrue() { return true; } if ( isfalse() && istrue() ) { .... } The question is - will istrue() ever get called/evaluated since isfalse() kills the whole logic set.... Matt |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
|
To not possibly evaluate the second half you would need to use this instead:
if ( A==B) || (C==D) ) If you are using &&, both sides need to be evaluated to determine if both are true. Dan |
|
|
|
|
|
#5 | |
|
Senior Member
|
Quote:
Now you have me second guessing myself, so let me talk outloud for a minute... Because && is a logical AND it means that both sides must be true for the whole thing to be true... if one side is false, then the whole thing is false, so only one half has to be false for the entire equation to be false, right? And for || it is a logical OR (not to be confused with an exclusive OR) that needs only one (or both) of the sides to evaluate to true to have a true expression So I think my logic is right that it should be an AND and not an OR -- don't confuse the english and math operators in that But what I really didn't know was if C# was smart enough to know when to stop evaluation.... or if it would compute both halves and figure it out later. BTW - I just re-read your statement, and realized that you were speaking of the TRUE condition, and I was speaking of the FALSE I think we're on the same page - but in the FALSE condition, will it stop when the first argument is FALSE in an AND statement? |
|
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
|
Yes you are correct (at least I believe so) and I was speaking of the 'true' conditions.
|
|
|
|
|
|
#7 |
|
Senior Member
|
I think you have the idea, but the syntax is a little off...
if((A==B) || (C==D)) { //do action 1 } So if A=B or C=D then you will //do action 1. You could do the same thing if you did this: if(A==B) { //do action 1 } if(C==D) { //do action 1 } Of course the first one is better coding...just showing you this to illustrate. However, if the code at the very top had && instead of || then it would be the same as: if(A==B) { if(C==D) { //do action 1 } } Hope that helps... |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Logic Problem? | DaveS | General Programming | 5 | 09-28-2009 01:21 PM |
| OCO logic question | shooter | Strategy Development | 7 | 03-27-2009 02:36 PM |
| Logic Question bars with same low | Laserdan | General Programming | 3 | 11-08-2008 08:50 AM |
| Reverse Logic (short) question | ericadam | Strategy Development | 3 | 10-13-2008 08:40 AM |
| MRO logic question | Burga1 | Strategy Development | 4 | 04-09-2008 08:56 PM |