Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Logic / Precedence Question...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    Both must be true in your example.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      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

      Comment


        #4
        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
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #5
          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?

          Comment


            #6
            Yes you are correct (at least I believe so) and I was speaking of the 'true' conditions.
            eDanny
            NinjaTrader Ecosystem Vendor - Integrity Traders

            Comment


              #7
              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...

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by kujista, Today, 05:44 AM
              2 responses
              11 views
              0 likes
              Last Post kujista
              by kujista
               
              Started by trilliantrader, Today, 08:16 AM
              0 responses
              3 views
              0 likes
              Last Post trilliantrader  
              Started by AttiM, 02-14-2024, 05:20 PM
              9 responses
              174 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by funk10101, Today, 08:14 AM
              0 responses
              2 views
              0 likes
              Last Post funk10101  
              Started by adeelshahzad, Today, 03:54 AM
              1 response
              13 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X