Solved

C++

Posted on 2001-07-24
13
154 Views
Last Modified: 2006-11-17
C++ operators are evaluated from left to right, true or false? explain your answer
0
Comment
Question by:yuki
  • 3
  • 2
  • 2
  • +6
13 Comments
 

Author Comment

by:yuki
ID: 6313521
C++ operators are evaluated from left to right, true or false? explain your answer
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6313541
sounds like homework.  Look up "operator precedence" in your textbook.
0
 
LVL 1

Expert Comment

by:Hoegje
ID: 6313588
It depends on which operators you are talking about offcourse. As in math * binds stronger than + so

a+b*c means a+(b*c) rather than (a+b)*c

And then there is also a difference between unary and binary operators : unary operators and assignment operators are right-associative, while all others are left associative, eg. :

a=b=c means a=(b=c)  (assignment)
a+b+c means (a+b)+c  (binary operator +)
*p++ means *(p++) and not (*p)++  (unary dereference operator *)

and so on...

And then there are still some expressions that can't be expressed in terms of precedence or associativity, but are pure grammar rules. The most important one of those is :
"if the statement could possibly be interpreted as a declaration, it is a declaration !!"

eg:
a=b<c?d=e:f=g means a = ((b < c) ? (d = e) : (f = g ))

So actually, you could say that the majority of the C++ operators are evaluated from the left to the right, but this isn't always the case at all.
0
 
LVL 4

Accepted Solution

by:
IainHere earned 50 total points
ID: 6313600
>explain your answer

EE policy is that homework tasks are not to be completed.  That is, specific questions will receive specific answers (rather like asking a tutor), but general "how do I write this program?" questions will not be answered.  The reason for this is obvious - pointing you in the direction of an operator precedence table (and hopefully an explanation of it in the surrounding pages) will allow you to understand what is going on.  Copying and pasting someone elses words from this site will teach you nothing.  (Didn't mean that to sound as pompous as it did :)
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6314514
C++ operators are not evaluated at all. Expressions are evaluated.
The evaluation order of expressions depends on which type of expression.
Which expression are you referring to?

If in doubt,see the C++ standard, 5 - 'Expressions'

0
 
LVL 9

Expert Comment

by:msdixon
ID: 6314535
IainHere,

i guess the other 2 guys are ignoring you and will do his homework anyway!!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 6314615
read again
0
 
LVL 9

Expert Comment

by:msdixon
ID: 6314642
yes, KangaRoo, i stand corrected... you just gave the little Joey a hint. my apologies.
0
 
LVL 2

Expert Comment

by:smitty1276
ID: 6314667
I think the other response was intended to overwhelm rather than answer.
0
 
LVL 9

Expert Comment

by:msdixon
ID: 6314719
and when read that way it's rather funny.
0
 

Expert Comment

by:CTrip
ID: 6346686
it depend in the operator:
1) a = b + c

1. the compiler check what is "b + c"
2. the compiler add "b + c" to "a"

2) a * b + c

1. the compiler check what is "a * b"
2. the compiler add "a * b" to "c"

however : the "()" operator can change it.

Exept A++ and ++A !!!

A++ - is always last
++A - is alwase first

example:
A = 1
cout << A++ //OUTPUT : 1
cout << ++A //OUTPUT : 2


0
 
LVL 11

Expert Comment

by:griessh
ID: 6833164
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points.

The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6853155
Folks,

I have gone against the recommendation and accepted IainHere's comment because he issued the warning regarding doing students' homework.

This subject is currently under review by the Moderators, Admin and the site owners; it is certainly academic dishonesty to offer coding as a solution to a problem posed by a teacher, but answering a basic question a student might not understand -- such as this one -- is a little less clear. It's not as if one is answering a question on a test.

Please be very careful if you think you see something that smacks of homework; it is a violation of the Membership Agreement to ask such a question, and is also one to answer it.

Thanks,

Netminder
Community Support Moderator
Experts Exchange

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now