Know more about if(x=y)

Know more about if(x=y)

After a very long I'm writing a blog. This is a short informative blog about if conditional statement. This is about a property of if which I learnt from Flavio's C-language handbook. I found this working in C,Cpp and JavaScript.

Let's start

You all know about if statement that it is a conditional statement. In if we give some argument in it which is the condition, if that condition is true then our if block executes, otherwise else or else if executes if used.

Let's have a look at it's syntax

if(x == y)
    {
        // statement
    }
else
    {
        // statement
    }

As you can see in above syntax we have used comparison operator == , as we can only use comparison operators in if. In if we do not use assignment operator =. But beginners often do this mistake, so let's see what happens if you use assignment operator

Let's have a look at it's syntax

if(x = y)
    {
        // statement
    }
else
   {
        // statement
   }

If you follow the above syntax then the above if condition will always be true, unless the argument is number zero 0. Yes if the argument is 0 then it will always be false and the if block will never execute in that condition. This will be more clear from the syntax

Let's have a look at it's syntax

if(x = 0)
    {
        // statement
    }

This happens because the conditional check will look for a boolean result and the number 0 always equates to a false value in boolean. Everything else is true including negative numbers.

Here are examples to make it more clear

Example in C language

#include <stdio.h>

void main()
{
    int a = 4;
    if(a = 0) 
        {
            printf("condition is true");
        }
    else
        {
            printf("condition is false");
        }
}

Copy above code and run it here online If you run it you have found that its prints condition is false as the passed argument is 0.

Another example in C language

#include <stdio.h>

void main()
{
    int a = 4;
    if(a = -4) 
        {
            printf("condition is true");
        }
    else
        {
            printf("condition is false");
        }
}

Copy above code and run it here online If you run it you have found that it prints condition is true as I earlier said that if the argument is anything else than 0 it will be true.

Example in Cpp

#include <iostream>
using namespace std;

int main() 
{
    int a = 20;
    if (a = 0) 
        {
            cout << "condition is true";
        } 
    else 
        {
            cout << "condition is false";
        }
}

Copy above code and run it here online You will find that in this case also it prints condition is false.

This is also works same way in JavaScript

Example in JavaScript

var a = 22; 
if (a = 0) 
    {
       console.log("condition is true");
    } 
else 
    {
       console.log("condition is false");
    }

Copy above code and run it here online You will find that in this case too it prints condition is false.

Another example in JavaScript

var a = 22; 
if (a = -1) 
    {
       console.log("condition is true");
    } 
else 
    {
       console.log("condition is false");
    }

Copy above code and run it here online You will find that in this case it prints condition is true as it worked in C.

Thank you for reading.

Please share your thoughts about it.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com