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 8462167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:05:37+00:00 2026-06-10T14:05:37+00:00

During work over a simple project I have found situation that I don’t fully

  • 0

During work over a simple project I have found situation that I don’t fully understand. Consider following code:

#include <iostream>

using namespace std;

bool test(int k)
{
    cout << "start " << k << endl;

    bool result; // it is important that result's value is opposite to initial value of recheck in main()
    result = false;

    return result;
}

int main()
{
    bool recheck;
    recheck = true;
    for (int i = 2; i > -1; i--)
    {
      recheck = (recheck || test(i));   // (1)
      cout << i << " ???" <<endl;
    }
    cout << "----------------------------" << endl;
    cout << endl;

    recheck = true;
    for (int i = 2; i > -1; i--)
    {
        recheck = (test(i) || recheck);  //different order that in (1)
        cout << i << "???" <<endl;
    }

    return 0;
}

It returns completely different results from for loops:

2 ???
1 ???
0 ???
----------------------------

start 2
2???
start 1
1???
start 0
0???

It seems that it first one test(int k) is not even invoked. I suspect it has something to do with || operator. Could anybody explain such a behavior?

  • 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-10T14:05:38+00:00Added an answer on June 10, 2026 at 2:05 pm

    The built-in || short-circuits: if the left operand is true, the right operand is not evaluated (it doesn’t matter what the value of the right operand is, because the value of the || expression is guaranteed to be true in this case).

    For completeness, but not particularly relevant to the question: In c++, the || operator is overloadable, just as many other operators are. If an overload is used, short circuiting does not take place.

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

Sidebar

Related Questions

I have a new client that I am converting over to Drupal from Wordpress.
I have built a simple map application with a single marker over my office.
During the work on my iPhone application the icon changed from time to time.
During my work on this database application, I've apparently managed to corrupt a form
During my routine work, i happened to write the chained javascript function which is
I like to work between my laptop and desktop during different parts of the
I have a seemingly simple requirement, but i can't figure out how to write
I have a method that does a time consuming operation, say something like ten
I work for a fulfillment company and we have to pack and ship many
We have over 10,000 registered users on our site and want to associate each

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.