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. how to optimize IF condition

how to optimize IF condition

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestioncode-review
3 Posts 3 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.
  • X Offline
    X Offline
    xcavin
    wrote on last edited by
    #1

    I have following line of code: if(a==Z|| b==Z|| c==Z|| d==Z|| e==Z|| f==Z|| g==Z) { . . . }can this be written in a better way ? p.s all the variables are of type enums

    D Z 2 Replies Last reply
    0
    • X xcavin

      I have following line of code: if(a==Z|| b==Z|| c==Z|| d==Z|| e==Z|| f==Z|| g==Z) { . . . }can this be written in a better way ? p.s all the variables are of type enums

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

      How about:

      if (a <= Z && Z <= g)
      ...

      Of course, trying to one-up the optimizer is usually a lost cause.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      • X xcavin

        I have following line of code: if(a==Z|| b==Z|| c==Z|| d==Z|| e==Z|| f==Z|| g==Z) { . . . }can this be written in a better way ? p.s all the variables are of type enums

        Z Offline
        Z Offline
        Zac Howland
        wrote on last edited by
        #3

        Is Z a enumeration value or a variable? If they are all variables, you really can't do much without making assumptions about ordering for the values.

        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

        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