Solved

C++

Posted on 2001-07-24
13
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

630 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