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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:25:13+00:00 2026-06-09T02:25:13+00:00

I was solving the C++ Multiple choice questions. I am not able to understand

  • 0

I was solving the C++ Multiple choice questions.
I am not able to understand the output for the following code::

#include <iostream>
using namespace std;
int main()
{
    int x,y,z;
    x=y=z=1;
    z=++x || ++y && ++z;
    cout<<x<<" "<<y<<" "<<z<<endl;
    system("pause");
    return 0;
}

I am solving this question in following way::
Precedence order ::

Precedence "++" greaterthan Precedence "&&" greaterthan  Precedence "||"

Also , the Associativity of unary++ is “Right to left”
.
So

z=(++x)||(++y) && (2)
z=(++x)||(2)&& (2)
z=(2)||(2)&&(2)
z=(2)|| 1  //As 2 && 2 is 1(true)
z=1       // As 2 || 1 is 1(true)

So as per me ,the correct output should be x=2,y=2 and z=1.

But When i ran this code in my compiler,the compiler output is x=2,y=1,z=1.

Why i am getting such output and where i am making mistake?

Thanks!

  • 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-09T02:25:14+00:00Added an answer on June 9, 2026 at 2:25 am

    Operator precedence tells you how to group expressions; it doesn’t tell you in which order they are executed.

    || and && are special in that the first operand is always evaluated first and the second operand (including all sub-expressions) is only evaluated if it is required to determine the value of the expression.

    For ||, if the first operand evaluates to true the second operand is not evaluated because the result of the logical-or will always be true.

    Similarly, the second operand of && will not be evaluated if the first operand evaluates to false as the logical-and must be false in this case.

    In the expression z=++x || ++y && ++z, the grammar rules specify a grouping:

    z = ((++x) || ((++y) && (++z)));
    

    In the sub-expression (++x) || ((++y) && (++z)), as (++x) evaluates to true (as 2 is non-zero), the second operator ((++y) && (++z)) is never evaluted. x becomes 2, y is unchanged and z is assigned 1 (true converted to an integer).

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

Sidebar

Related Questions

I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code
Does anyone have any workable strategy for solving casting/upcasting questions? I understand when upcasting
I need some help solving some compile errors. I was following a tutorial (at
Working on solving a simple problem in C++, want to understand how I could
I have multiple Django models that reference a model using foreign keys as such:
I have tried solving this problem by posting other related questions here that focused
I really need an immediate with Javascript. I created a multiple choice question quiz
I am currently building a multiple pages form using $_SESSION , everything has been
I am having a hard time solving the following with an MVC view. My
hey guys, someone already helped me solving the following problem: i wanted to iterate

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.