Know more about if(x=y)
Tags: javascript, cpp, webdev, beginners
Published: 2020-12-31 13:27:34

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](https://twitter.com/flaviocopes)** **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
```c
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
```c
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
```c
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
```c
#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](https://www.onlinegdb.com/online_c_compiler)**
If you run it you have found that its prints **condition is false** as the passed argument is 0.
#### Another example in C language
```c
#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](https://www.onlinegdb.com/online_c_compiler)**
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 
```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](https://www.onlinegdb.com/online_c_compiler)**
You will find that in this case also it prints **condition is false**.
### This is also works same way in JavaScript
#### Example in JavaScript
```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](https://playcode.io/new/)**
You will find that in this case too it prints **condition is false**.
#### Another example in JavaScript
```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](https://playcode.io/new/)**
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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*