NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-21-2010, 10:03 PM   #1
tazatek
Senior Member
 
Join Date: Jul 2008
Location: Grand Junction, CO
Posts: 148
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to tazatek
Default Logic / Precedence Question...

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
tazatek is offline  
Reply With Quote
Old 03-21-2010, 10:34 PM   #2
eDanny
Senior Member
 
eDanny's Avatar
 
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
Default

Both must be true in your example.
eDanny is offline  
Reply With Quote
Old 03-21-2010, 10:43 PM   #3
tazatek
Senior Member
 
Join Date: Jul 2008
Location: Grand Junction, CO
Posts: 148
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to tazatek
Default

Quote:
Originally Posted by eDanny View Post
Both must be true in your example.
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
tazatek is offline  
Reply With Quote
Old 03-21-2010, 10:52 PM   #4
eDanny
Senior Member
 
eDanny's Avatar
 
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
Default

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
eDanny is offline  
Reply With Quote
Old 03-21-2010, 11:03 PM   #5
tazatek
Senior Member
 
Join Date: Jul 2008
Location: Grand Junction, CO
Posts: 148
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to tazatek
Default

Quote:
Originally Posted by eDanny View Post
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

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?
tazatek is offline  
Reply With Quote
Old 03-22-2010, 08:01 AM   #6
eDanny
Senior Member
 
eDanny's Avatar
 
Join Date: Jul 2008
Location: East Rochester, NY
Posts: 899
Thanks: 0
Thanked 19 times in 17 posts
Default

Yes you are correct (at least I believe so) and I was speaking of the 'true' conditions.
eDanny is offline  
Reply With Quote
Old 03-22-2010, 01:54 PM   #7
mountainclimber
Senior Member
 
Join Date: Jun 2008
Posts: 112
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to mountainclimber
Default

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...
mountainclimber is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -6. The time now is 01:16 AM.