7 Replies - 233 Views - Last Post: 19 November 2012 - 04:24 AM Rate Topic: -----

#1 Sodai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 12

Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 12:47 AM

Hello! I have some confusion about the execution of this code:
          void main()


   




Is This A Good Question/Topic? 0
  • +

Replies To: Does the compiler ignore the C++ precedence rules here?

#2 Sodai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 12

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 01:00 AM

Hello! I've some confusion here:

          void main()
          {
             int n=5;
             if((5>4) || (n=0))
             {
               cout<< "n = " << n ;
             }    
          }



Its output is : n = 5
Precessor checks the first condition 5>4 which is true; and ignores whatsoever follows, i.e. "n" is not assigned the value "0".
Now my question is that parentheses () have higher precedence than Logical OR || operator. So, the assignment inside the parentheses should be evaluated before the Logical OR operator; and thus output should be: n = 0 .
Why does this not happen?
Was This Post Helpful? 0
  • +
  • -

#3 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 02:04 AM

View PostSodai, on 19 November 2012 - 12:47 AM, said:

Hello! I have some confusion about the execution of this code:
          void main()


   




The main() is the entry point of a C++ Programm.
Below a short example of main():
int main()
{
return 0;
}


or (little bit more advanced)
#include <iostream>  //input/output stream
using namespace std;

int main()
{
	system("pause");
	return 0;
}


This post has been edited by LarcaCode: 19 November 2012 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 02:14 AM

The priority of () is higher than ||. Your code behaves absolutly correct.
To accomplish the wished behaviour I would just make some little changes (red):

int main() //return an integer
{
int n=5;
if((5>4) || (n==0)) // '=' assignment, '==' compare
{
cout<< "n = " << n ;
}
return 0;
}

Hope that is helpful for you.
Was This Post Helpful? 1
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 03:06 AM

It's always int main.

And this isn't at all about operator precedence, it's about short-circuit evaluation.

Let's analyse it step by step. We start with:
if((5>4) || (n=0)) { /* body */ }

We know that if to be able to decide if to execute it's body needs to evaluate the expression:
(5>4) || (n=0)
Now, Logical OR operator has special rules, let me quote C++11 standard:

Quote

The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.


So you see, it is guaranteed that left side of || is going to be evaluated first, with all it's side effects complete before right side is even considered to be evaluated.
This means that this:
(5>4)
is always going to be evaluated first. This of course evaluates to true since 5 is larger than 4.
So we have:
 true || (n=0)

At this point one of the operands is true, and we already know that this whole expression is going to result in true no matter what result the right side of || yields. Since || is short-circuit operator, the right side won't even be evaluated, and true will be returned.

This means that in C++ you can actually use || and && in place of simple if:
a || b; // only evaluates b when a is false
a && b; // only evaluates b when a is true
but for readability sake, you probably shouldn't.

This post has been edited by Xupicor: 19 November 2012 - 03:52 AM

Was This Post Helpful? 2
  • +
  • -

#6 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 03:40 AM

This was answered in your other topic.

@LarcaCode - system("pause") is not portable, possibly dangerous, assumes the program is being run with a human being in front of the PC, and is ugly. :P Why recommend that?

Just run your program from the terminal, or set your IDE to keep the terminal window open after the execution. If your IDE doesn't support that (huh?) then you still can run your app trough debugger with breakpoint set at your last line.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 03:48 AM

Xupicor already answered this question well, but I just want to stress that precedence has nothing to do with the order in which the subexpressions are executed - it just determines which how the expression is split into subexpressions. So when I do f1() + f2() * f3(), the precedence rules tell us whether we have

     +
f1()     *
     f2() f3()



or

           *
    +        f3()
f1()  f2()



They do not tell us in which order f1, f2 and f3 are executed.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Does the compiler ignore the C++ precedence rules here?

Posted 19 November 2012 - 04:24 AM

Merged duplicate topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1