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 Philippe56140, Today, 02:35 PM
          0 responses
          2 views
          0 likes
          Last Post Philippe56140  
          Started by 00nevest, Today, 02:27 PM
          0 responses
          1 view
          0 likes
          Last Post 00nevest  
          Started by Jonafare, 12-06-2012, 03:48 PM
          5 responses
          3,986 views
          0 likes
          Last Post rene69851  
          Started by Fitspressorest, Today, 01:38 PM
          0 responses
          2 views
          0 likes
          Last Post Fitspressorest  
          Started by Jonker, Today, 01:19 PM
          0 responses
          2 views
          0 likes
          Last Post Jonker
          by Jonker
           
          Working...
          X