Announcement

Collapse
No announcement yet.

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.
    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 NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          65 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X