Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i write nested if statement?

So i want to make a simple if statement like this :

if(a=1)
{
    if(variable1=1)
    {
        cout<<"a1=1";
    }
    else
    {
        cout<<"error";
    }
}

if(a=2)
{
    if(variable1=1)
    {
        cout<<"a2=1"
    }
    else
    {
        cout<<"error";
    }
}

Problem :
When i set a to 2 and variable1 to 1 it will just print a1=1 not a2=1. How do i fix this?
Sorry if there is a miss-writing, or bad english in this post. I hope someone answers…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using the assignment operator(=) instead of the comparison operator (==). The assignment operator will always return true if the assignment is successful, hence it is going in the first if statement.

To solve the issue, change = to == in your code.

if (a == 1){

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading