Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:08+00:00 2026-06-17T07:41:08+00:00

I have the following C++ code: if(x==y||m==n){ cout<<Your message<<endl; } If x is equal

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:41:09+00:00Added an answer on June 17, 2026 at 7:41 am

    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 x-y message\n";
    }
    if(m==n) {
      std::cout << "Your m-n message\n";
    }
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code int main() { cout << Please enter your name...
If you have the following code: cout << hex << 10; The output is
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
I have the following C++ code and when I run the program sometimes it
I have the following code: map<StatTypesEnum, ValueHandler*>::const_iterator itr; for(itr=player1->Stats.begin(); itr!=player1->Stats.end(); itr++) { cout <<
I have following code for double comparision. Why I am getting not equal when
I have the following code (simplified) int main() { ifstream myFile(input.txt); if(myFile.is_open()) cout<<test; }
I have the following code : const int k=1; int *p=const_cast<int *>( &k); cout<<k

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.