Announcement

Collapse
No announcement yet.

question proper ues of if/else if

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

  • question proper ues of if/else if

    Hi,

    Am I using the if and else if properly?

    PHP Code:

    setColorPriceBars
    (true)
    setDefaultPriceBarColor(Color.white);

    if (
    a>b)
    {
    setPriceBarColor(Color.green);
    }
    else if (
    e>f)  //should this be else if or if??
    {
    setPriceBarColor(Color.red); 
    //if neither condition is met the bars default to white
    }

    if (
    s>y)
    {
    drawShape(Shape.UPARROW,.................);
    }
    else if (
    x>P//again, should this be if or else if??
    {
    drawShape(Shape.DOWNARROW,...............);
    }
    //if neither condition is met no arrows are drawn 
    Thank you for your help,
    Davo

  • #2
    Davo,

    You're using the correct syntax. Whether the logic is correct or not depends on what you want to happen.

    Say a>b and e>f. What color do you want the bars to be?

    With your current code they will be green. If you take out the else and just make another if, the bars will be set to green in the first if statement and then set to red in the second if statement. So they will be red on the chart.

    A similar condition will exist with your arrow drawing code. If both are true, only the first if statement will run with your given code. If you change the code to just use if statements, then the last true condition will be what is shown.

    Steve

    Comment


    • #3
      Hi Steve,

      a>b and e>f cannot happen at the same time.

      If a>b bars are green if not bars are white.

      If e>f bars are red if not bars are white,
      unles a>b.

      What my questin is: Is this the best and most efficient way to write the code. Look at the following exampe, perhaps this is the most efficient to write the code?? or perhaps if doesn't matter (I posted here to have Jason or Alex let me know which is best.

      New example......this may be the correct way.....

      PHP Code:
      setColorPriceBars(true

      //setDefaultPriceBarColor(Color.white);


      if (a>b)

      {

      setPriceBarColor(Color.green);

      }

      if (
      e>f
      {

      setPriceBarColor(Color.red); 
      }
      else
      {
      setPriceBarColor(Color.white);
      }

      if (
      s>y)
      {
      drawShape(Shape.UPARROW,......);
      }

      else if (
      x>P)
      {
      drawShape(Shape.DOWNARROW,......);
      }

      //if neither condition is met no arrows are drawn 

      Comment


      • #4
        Davo,

        If the conditions are exclusive, then either coding method will work and be just as efficient.

        It would come down to which one is easier for you to understand 3 months from now when you go back to make code changes .

        Steve

        Comment

        Working...
        X