Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:23:31+00:00 2026-06-11T18:23:31+00:00

I heard some programmers use if(1 == var) instead of if(var == 1) to

  • 0

I heard some programmers use if(1 == var) instead of if(var == 1) to avoid unintended assignment. Why or in what cases does it cause unintended assignment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:23:32+00:00Added an answer on June 11, 2026 at 6:23 pm

    The problem is if you mistype the statement:

    if (var = 1)
    

    vs

    if (1 = var)
    

    In the first case, the code after the if is executed unconditionally (with no more than a warning from the compiler, which isn’t obliged to produce a warning for you — but the good ones do; if you aren’t using a good compiler, get one!). In the second case, you get a syntax error at compile time, so the problem has to be fixed before the code can compile.

    The problem isn’t always as blatant:

    if (var = 0)
    

    never executes the code after the if, of course. Often though, you’ll have:

    if (var = function(arg1, arg2))
    

    and it won’t be clear whether you intended to assign or compare. You can make it clear to the compiler and code readers (humans) by writing:

    if ((var = function(arg1, arg2)) != 0)
    

    or

    if (var == function(arg1, arg2))
    

    I don’t use the ‘back-to-front’ comparison technique. I dislike the inverted conditions because they almost invariably read ‘wrong’ to me. I’m not comparing 1 with my variable; I’m comparing my variable with 1. So, even though logically the == operator is commutative, I don’t think commutatively and prefer that ‘riskier’ way. I have not found myself making the assignment vs equality mistake often enough for the issue to be a problem. The compiler warns me if I do make a mistake (and I pay attention to the warning and fix the code so that there isn’t a problem).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard that some custom component authors use an RTL routine that checks to
I heard recently some advice to unimport an implicit conversion from Predef - I
I am looking for a 3D physics engine for XNA. I heard of some
I heard that in PHP there are some alternatives for the functions substr() and
I heard this term many times when I came across some protocol implementations, but
I've heard something and seen some examples of web application built using ASP.NET /
I heard an example at work of using serialization to serialise some values for
I have heard of types being referred to as boxed in some languages. In
I have heard the term connection pooling and looked for some references by googling
I heard this today during interview for java developer. I had to list some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.