简体   繁体   English

在IF语句中检查多个OR运算符

[英]Check multiple OR operators in IF statement

I have the following C++ code: 我有以下C ++代码:

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". 如果x等于y或m等于n,程序将打印“您的消息”。 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? 有没有办法使用单个if语句根据每个条件独立打印每个"Your message"

The output would be identical to the below using multiple if statements. 使用多个if语句,输出将与下面的输出相同。

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. 1)可维护性代码。 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. 一对if语句不会。

2) Distinct input should produce distinct output. 2)不同的输入应产生不同的输出。 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 . 旁白:当你的意思是\\n时,永远不要使用endl They produce semantically identical code, but endl can accidentally make your program go slower. 它们产生语义相同的代码,但是endl可能会意外地使你的程序变慢。

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. 你可以做的是带有break语句的do / while循环。 But I don't think that's the way to go. 但我不认为这是要走的路。 Why do you want to avoid two if statements? 为什么要避免两个if语句?

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. 当两者都为真时,编译器仅检查一个条件,因为您已将条件与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. 如果OR链中的一个条件为真,则不需要检查其他条件已经为真,如果其中一个条件为假,则为假。 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. 很快你就不应该期望编译器在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. 在比较布尔值(true / false)值时,查看布尔运算符以查看所有可能的选项。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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