Solved

C++

Posted on 2001-07-24
13
157 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 112
c++ syntax question 9 52
c++, dynamic object by json 1 53
Android development question 2 54
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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