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 to use & operator in C++?

While I was coding, I came to a place where I need to use if and the & operator. For any reason, it did not work and I was frustrated.

The example code is here:

#include <iostream>

using namespace std;

int main()
{
int a=0, b=0,c=0;
if(a && b && c==0)
{
    cout<<"Sound!";
}

return 0;
}

My goal is: if a and b and c is 0, I will print Sound!

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

The output that I got did not print anything but the compiler did not raised any errors.

But I thought that the output should be like this:

Sound!

I have tried to replace the && with only 1 & but it did not work. I had the & replaced with and but still did not work.

Does anyone know the answer?

>Solution :

You can write the if statement in several ways. For example

if (a == 0 && b == 0 && c==0)

or

if ( !a && !b && !c )

or

if ( not ( a != 0 || b != 0 || c != 0 ) )
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