IF...Else Quick Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arcticool@hotmail.com

    IF...Else Quick Question

    I was just surprised to find that "Else if" is not required in the code bit below.
    Apparently "If" and "Else if" are used interchangeably . Please correct me if I'm wrong, but it
    appears "Else" is just a stylistic choice.

    for (int i = 1; i <= 100; i++)
    {
    if (i == 3)
    continue;
    if (i == 5)
    break;
    Console.WriteLi ne(i);
    }

    //Compiles just fine and I can tell no difference using this or "else if" in the second if
    statement.
    Thanks in advance,

    Jack


    --
    --------------------------------- --- -- -
    Posted with NewsLeecher v3.9 Beta 5
    Web @ http://www.newsleecher.com/?usenet
    ------------------- ----- ---- -- -

  • Paul E Collins

    #2
    Re: IF...Else Quick Question

    "Arcticool" <arcticool@hotm ail.comwrote:
    Apparently "If" and "Else if" are used interchangeably .
    It's the same in your example, because "continue;" will jump back to the top
    of the loop anyway, so the second "if" won't be processed.

    In general, however, they are not the same. Consider:

    int i = 5;
    if (i <= 5) Console.WriteLi ne("less than or equal to 5");
    if (i == 5) Console.WriteLi ne("equals 5");

    This will print both lines of text, since each "if" is processed
    independently.

    If you add "else" to the second line, then the second message will never be
    printed, because whenever the second condition is true, the first is true as
    well, *and* the second condition is only performed in the "else"-case, when
    the first was not.

    Eq.


    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: IF...Else Quick Question

      arcticool@hotma il.com wrote:
      I was just surprised to find that "Else if" is not required in the code bit below.
      Apparently "If" and "Else if" are used interchangeably . Please correct me if I'm wrong, but it
      appears "Else" is just a stylistic choice.
      >
      for (int i = 1; i <= 100; i++)
      {
      if (i == 3)
      continue;
      if (i == 5)
      break;
      Console.WriteLi ne(i);
      }
      >
      //Compiles just fine and I can tell no difference using this or "else if" in the second if
      statement.
      Thanks in advance,
      >
      Jack
      >
      It's only in situations where the second if is skipped or it's condition
      is exclusive of the first condition that there is no difference.

      As you are using continue to skip some of the code, it's equivalent to
      this code:

      for (int i = 1; i <= 100; i++) {
      if (i == 3) {
      // do nothing
      } else {
      if (i == 5) {
      break;
      }
      Console.WriteLi ne(i);
      }
      }

      Note that there doesn't exist any "else if" construct. It's just an else
      containing an if. When you write like this:

      if (something) {
      ...
      } else if (something) {
      ...
      }

      it's actually:

      if (something) {
      ...
      } else {
      if (something) {
      ...
      }
      }

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Little Mad Dog

        #4
        Re: IF...Else Quick Question

        arcticool@hotma il.com wrote:
        I was just surprised to find that "Else if" is not required in the code bit below.
        Apparently "If" and "Else if" are used interchangeably . Please correct me if I'm wrong, but it
        appears "Else" is just a stylistic choice.
        >
        for (int i = 1; i <= 100; i++)
        {
        if (i == 3)
        continue;
        if (i == 5)
        break;
        Console.WriteLi ne(i);
        }
        >
        //Compiles just fine and I can tell no difference using this or "else if" in the second if
        statement.
        You got two independent and complete if statements up there above there.


        It could be something like this too.


        if (i==3)
        {
        continue;
        }
        else
        {
        brake; 'anything else but 3
        }

        ---------


        if (i == 3)
        {
        continue;
        }
        elseif ( i 0 && i < 2)
        {
        brake;
        }
        elseif ( something else )
        {
        do something;
        }

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: IF...Else Quick Question

          Paul E Collins wrote:
          "Arcticool" <arcticool@hotm ail.comwrote:
          >Apparently "If" and "Else if" are used interchangeably .
          >
          It's the same in your example, because "continue;" will jump back to the top
          of the loop anyway, so the second "if" won't be processed.
          >
          In general, however, they are not the same. Consider:
          >
          int i = 5;
          if (i <= 5) Console.WriteLi ne("less than or equal to 5");
          if (i == 5) Console.WriteLi ne("equals 5");
          >
          This will print both lines of text, since each "if" is processed
          independently.
          >
          If you add "else" to the second line, then the second message will never be
          printed, because whenever the second condition is true, the first is true as
          well, *and* the second condition is only performed in the "else"-case, when
          the first was not.
          Continue is a special case.

          In general and which was also the case in the original posters
          code, then else has no functional impact only
          performance impact if the conditions are mutually exclusive.

          Arne

          Comment

          • Bela Istok

            #6
            Re: IF...Else Quick Question

            The IL code generated for the if, if case or the if, else if case is the
            same, in your example.

            Regards,

            Bela Istok
            "Arcticool" <arcticool@hotm ail.comwrote in message
            news:ZNCdnQgx_J xd1wPVnZ2dnUVZ_ qOdnZ2d@powerus enet.com...
            >>I was just surprised to find that "Else if" is not required in the code
            >>bit below.
            >Apparently "If" and "Else if" are used interchangeably . Please correct me
            >if I'm wrong, but it
            >appears "Else" is just a stylistic choice.
            >>
            > for (int i = 1; i <= 100; i++)
            > {
            > if (i == 3)
            > continue;
            > if (i == 5)
            > break;
            > Console.WriteLi ne(i);
            > }
            >>
            >//Compiles just fine and I can tell no difference using this or "else if"
            >in the second if
            >statement.
            >Thanks in advance,
            >>
            >Jack
            >>
            >>
            >--
            >--------------------------------- --- -- -
            >Posted with NewsLeecher v3.9 Beta 5
            >Web @ http://www.newsleecher.com/?usenet
            >------------------- ----- ---- -- -
            >>
            >

            Comment

            • arcticool@hotmail.com

              #7
              Re: IF...Else Quick Question

              Thanks to all for the responses.
              It seems that there are at least three considerations here:
              1. Is the second boolean condition exclusive of the first, if not a nested if may be the best
              choice.
              2. Else If is not a single construct (aha!)
              3. Simply using Else may be all that is needed.

              Interesting responses. Much appreciated :)

              -Jack


              --
              --------------------------------- --- -- -
              Posted with NewsLeecher v3.9 Beta 5
              Web @ http://www.newsleecher.com/?usenet
              ------------------- ----- ---- -- -

              Comment

              • J.B. Moreno

                #8
                Re: IF...Else Quick Question

                Arne Vajhøj <arne@vajhoej.d kwrote:
                Paul E Collins wrote:
                "Arcticool" <arcticool@hotm ail.comwrote:
                Apparently "If" and "Else if" are used interchangeably .
                It's the same in your example, because "continue;" will jump back to the
                top of the loop anyway, so the second "if" won't be processed.

                In general, however, they are not the same. Consider:

                int i = 5;
                if (i <= 5) Console.WriteLi ne("less than or equal to 5");
                if (i == 5) Console.WriteLi ne("equals 5");

                This will print both lines of text, since each "if" is processed
                independently.

                If you add "else" to the second line, then the second message will
                never be printed, because whenever the second condition is true,
                the first is true as well, *and* the second condition is only
                performed in the "else"-case, when the first was not.
                >
                Continue is a special case.
                >
                In general and which was also the case in the original posters code,
                then else has no functional impact only performance impact if the
                conditions are mutually exclusive.
                *IF* the conditions are mutually exclusive, but IME it's much more
                common to have if else if chains where the conditions are *not*
                mutually exclusive.

                Also mutually exclusive conditions are more likely to be done as switch
                statements instead.

                --
                J.B. Moreno

                Comment

                • MC

                  #9
                  Re: IF...Else Quick Question

                  >I was just surprised to find that "Else if" is not required in the code bit
                  >below.
                  Apparently "If" and "Else if" are used interchangeably . Please correct me
                  if I'm wrong, but it
                  appears "Else" is just a stylistic choice.
                  No, you just happen to have written a program in which they both do the same
                  thing. The "continue" statement changes the flow of control so that the
                  following "if" statement is not executed.

                  if (X) then A; else if (Y) then B;

                  is not at all equivalent to:

                  if (X) then A; if (Y) then B;

                  The second one will execute both A and B if both X and Y are true. The
                  first one only looks at Y if X was not true.



                  Comment

                  • J.B. Moreno

                    #10
                    Re: IF...Else Quick Question

                    In article <X5ednQCvNbaNwg PVnZ2dnUVZ_s-dnZ2d@powerusen et.com>, wrote:
                    Thanks to all for the responses.
                    It seems that there are at least three considerations here:
                    1. Is the second boolean condition exclusive of the first, if not a nested if
                    may be the best
                    choice.
                    An additional consideration is object references.

                    Consider....

                    Object x = null;

                    if (x == null) {Console.WriteL ine("x is null")}
                    else if (x.Value == 1) {Console.WriteL ine("x = 1")}


                    And side effects


                    if (x.IsSaved) {whatever}
                    else if (!x.Persist()) {whatever}

                    who knows what all could change because of the method call?

                    --
                    J.B. Moreno

                    Comment

                    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                      #11
                      Re: IF...Else Quick Question

                      J.B. Moreno wrote:
                      Arne Vajhøj <arne@vajhoej.d kwrote:
                      >Paul E Collins wrote:
                      >>"Arcticool" <arcticool@hotm ail.comwrote:
                      >>>Apparently "If" and "Else if" are used interchangeably .
                      >>It's the same in your example, because "continue;" will jump back to the
                      >>top of the loop anyway, so the second "if" won't be processed.
                      >>>
                      >>In general, however, they are not the same. Consider:
                      >>>
                      >>int i = 5;
                      >>if (i <= 5) Console.WriteLi ne("less than or equal to 5");
                      >>if (i == 5) Console.WriteLi ne("equals 5");
                      >>>
                      >>This will print both lines of text, since each "if" is processed
                      >>independently .
                      >>>
                      >>If you add "else" to the second line, then the second message will
                      >>never be printed, because whenever the second condition is true,
                      >>the first is true as well, *and* the second condition is only
                      >>performed in the "else"-case, when the first was not.
                      >Continue is a special case.
                      >>
                      >In general and which was also the case in the original posters code,
                      >then else has no functional impact only performance impact if the
                      >conditions are mutually exclusive.
                      >
                      *IF* the conditions are mutually exclusive, but IME it's much more
                      common to have if else if chains where the conditions are *not*
                      mutually exclusive.
                      Possible. I don't have any particular feeling for how common or
                      uncommon it is.
                      Also mutually exclusive conditions are more likely to be done as switch
                      statements instead.
                      For equality tests.

                      Arne

                      Comment

                      Working...