Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Evaluation order in an 'if' statement

Evaluation order in an 'if' statement

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    Hi all, In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after? I mean, if Comp1 changes a variable, will this variable be changed by the time Comp2 is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?

    if(Comp1)
    if(Comp2)

    Thank you!

    C T D M 4 Replies Last reply
    0
    • P piul

      Hi all, In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after? I mean, if Comp1 changes a variable, will this variable be changed by the time Comp2 is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?

      if(Comp1)
      if(Comp2)

      Thank you!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Yes, Comp1 is evaluated first. And also, if Comp1 is false, then Comp2 won't be evaluated. So you the variable will be modified once you check the second comparison.

      Cédric Moonen Software developer
      Charting control [v1.4]

      1 Reply Last reply
      0
      • P piul

        Hi all, In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after? I mean, if Comp1 changes a variable, will this variable be changed by the time Comp2 is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?

        if(Comp1)
        if(Comp2)

        Thank you!

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        yes, comp1 is avaluated before comp2. also, you have to know that depending on the comparison operator used, the other "comps" may not be evaluated. check this :

        if ((1==1) || (c == d)) ...

        here, the test (c == d) is never performed, because (1 == 1) is true, so true **OR** anything is actually true. same is this :

        if ((0==1) && (c == d)) ...

        same is (0 == 1) always returns false, so false **AND** anything is false... so if that even happens, when you have a function call of a variable modification in such a construction, it may not be performed... basically, a if/while conditional statement should be used only for testing a condition to avoid mistakes.

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        1 Reply Last reply
        0
        • P piul

          Hi all, In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after? I mean, if Comp1 changes a variable, will this variable be changed by the time Comp2 is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?

          if(Comp1)
          if(Comp2)

          Thank you!

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          piul wrote:

          In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after?

          It's called short-circuit evaluation.

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • P piul

            Hi all, In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after? I mean, if Comp1 changes a variable, will this variable be changed by the time Comp2 is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?

            if(Comp1)
            if(Comp2)

            Thank you!

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Also, if you're not entirely clear on operator precedence and ssociativity in C++, then maybe keep a link to this handy: Operator Precedence and Associativity[^] Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups