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

Question About "If" and "else if"

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

    Question About "If" and "else if"

    Hello Staff,

    I am puzzled when trying to understand the concept of "If" and "else if".
    To do this correctly, do I need to place both the "If" and "else if" in the same bracket like this?

    {
    if (X=true)
    {
    //Do Something
    }
    else if (X=false)
    {
    //Do Something Else
    }
    }


    Or will the "else if" work with whichever "if" is placed above it no matter the brackets, and ignore the "if" that is placed below it?

    I'm looking forward to your reply.

    #2
    Hello GLFX005,

    Try the following:

    bool myBoolA = false;
    bool myBoolB = true;

    if (myBoolA == true)
    {
    Print("myBoolA is true");
    }
    else if (myBoolB == true)
    {
    Print("myBoolB is true");
    }

    The curly braces outside of the if statement (branching command) would be related to another object such as the method the if statement is in, or if this is nested in another if statement.
    In the code you have suggested, the outer brackets would nothing at all as there is no method declaration or if statement above it.

    This would fall under general C# education and is not specific to NinjaTrader.

    Below is a link to a 3rd party educational site on if and else if.
    Encode branching logic with if, else-if and else. Evaluate conditions to true or false.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea, thank you for your answer.

      Is the "else if" ignored when the "if" above it is true?

      Comment


        #4
        Hello GLFX005,

        That is correct. When the 'if' is true, the 'else' will not be evaluated. The 'else' means that the first 'if' didn't happen similar to using the english word otherwise.

        As an example if you were to say to someone if you have a red ball i would like to buy it, else if you have a green ball i'll buy that.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          Thank you for your help, I will let you know should any further questions arise.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          1 view
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          16 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by frslvr, 04-11-2024, 07:26 AM
          9 responses
          124 views
          1 like
          Last Post caryc123  
          Started by selu72, Today, 02:01 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by WHICKED, Today, 02:02 PM
          2 responses
          20 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Working...
          X