简体   繁体   中英

Check multiple OR operators in IF statement

I have the following C++ code:

if(x==y||m==n){
cout<<"Your message"<<endl;
}

If x is equal to y or m is equal to n, the program prints "Your message". But if both conditions are true,the program tests only one of them and eventually prints one "Your Message".

Is there a way to print each "Your message" independently based on each condition using a single if statement?

The output would be identical to the below using multiple if statements.

if(x==y){
cout<<"Your message"<<endl;
}

if (m==n){
cout<<"Your message"<<endl;
}

Not that I'd ever do it this way, but ...

for(int i = 0; i < (x==y)+(m==n); ++i) {
  std::cout << "Your message\n";
}


Let me expand on this. I'd never do it this way because it violates two principles:

1) Code for maintainability. This loop is going to cause the maintainer to stop, think, and try to recover your original intent. A pair of if statements won't.

2) Distinct input should produce distinct output. This principle benefits the user and the programmer. Few things are more frustrating than running a test, getting valid output, and still not knowing which path the program took.

Given these two principles, here is how I would actually code it:

 if(x==y) { std::cout << "Your xy message\\n"; } if(m==n) { std::cout << "Your mn message\\n"; } 

Aside: Never use endl when you mean \\n . They produce semantically identical code, but endl can accidentally make your program go slower.

I don't think that's possible. What you have inside your bracket is a statement which is either true or false, there's no such thing like a true/true or true/false statement. What you could do is a do/while loop with a break statement. But I don't think that's the way to go. Why do you want to avoid two if statements?

single "|" or "&" gaurantees both side evaluation even if the result can be determined by left side operator alone.

You could do something like this, to build up the "message":

string msg = "Your Message\n";
string buildSt = x == y ? m == n ? msg + msg : msg : m == n ? msg : ""; 

Compiler checks only one condition when both are true because you've connected your conditions with OR. If even one condition in ORs chain is true there is no need to check others as a result already true and will be false if one of them is false. So if you think that your logic is right then there is no need to do multiple checks. Your code is asking that you will print a message if one of the conditions is true and program doing it. If you want something special for a case when both conditions are true then add it separately. Shortly you should never expect from the compiler to do all checks in the expressions connected by OR.

Regards,

Davit

Tested code:

#include <iostream>
#include <string>
using namespace std;

void main() {
    int x=1;
    int y=1;
    int m=1;
    int n=1;
    string mess1="Your message 1\n";
    string mess2="Your message 2\n";
    cout<<((x==y)?mess1:"")+((m==n)?mess2:"");
    getchar();
}

If you are trying to see if both statements are true an && is what you will want to use.

Take a look at Boolean Operators to see all of the possible options when comparing boolean (true/false) values.

To answer your question:

    if ((x==y) && (m==n))
    {
        cout<<"Your Message"<<endl<<"Your Message"<<endl;
    }
    else if((x==y) || (m==n))  
    {
        cout<<"Your Message"<<endl;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM