Solved

C++

Posted on 2001-07-24
13
152 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

19 Experts available now in Live!

Get 1:1 Help Now