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. if (23 == x) or if (x == 23)

if (23 == x) or if (x == 23)

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • A Offline
    A Offline
    Anorexic Tribble
    wrote on last edited by
    #1

    Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!

    J G 2 Replies Last reply
    0
    • A Anorexic Tribble

      Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!

      J Offline
      J Offline
      Justin Hallet
      wrote on last edited by
      #2

      It is to prevent typo errors, if (x=23) will compile and do undesired things, assign x the value of 23 and always be true if (23=x) will not compile its good practice to put the constant on the left and variable on the right

      1 Reply Last reply
      0
      • A Anorexic Tribble

        Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!

        G Offline
        G Offline
        grigsoft
        wrote on last edited by
        #3

        I have thought myself about it and could only think of one reason - to avoid mistakes with 'if (x = 23)'. Most compilers today warns you about such syntax, but in past that could be a common hard-to-find mistype. By having accustomed to write constant first, you ensure compiler would not handle this: 'if (23 = x)' Igor Green http://www.grigsoft.com/ Compare It! + Synchronize It! - files and folders comparison never was easier!

        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