![]() |
|
#1
|
||||
|
||||
The evaluation of if statement.Is the order of evaluation of expressions in if statement defined ?
Should we use braces to define the order of evaluation ? Best Regards, Kobi. |
||||
|
#3
|
|||
|
|||
|
Quote:
Quote:
Quote:
|
|
#4
|
||||
|
||||
whould the usage of parentheses change this fact ?Quote:
The header of this message referes to the above qoute. Quote:
I should have been more accurate... |
|
#5
|
||||
|
||||
|
Quote:
Quote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#6
|
||||
|
||||
I still don't get it.I am still trying to understand this issue.
I am using parentheses with sub-expressions, But I'm trying to understand this : How does this ( ISO) : Quote:
Live in peace with this : Quote:
What am I missing ? |
|
#7
|
|||
|
|||
|
>I am using parentheses with sub-expressions
Quote:
|
|
#8
|
|||
|
|||
|
Quote:
I think there has to be an understanding of the difference between the order of evaluation and the order of assignment. If you write a program that depends on order of evaluation for arithmetic operators (or for function calls within an expression, or arguments of a function call) then you have what is known as "undefined behavior". That is, you can't depend on a compiler to come up with any particular value for the expression. Now, I would say that, usually, expressions in if statements are separated by boolean operators (&&, ||), and the order of evaluation is defined (left to right). With most other operations (including arithmetic operators) the order of evaluation is not defined. In all cases, order of assignment is determined by the precedence rules of the language, and that is defined for all operators. So the expression CPP / C++ / C Code:
On the other hand, suppose we have functions with side effects (that is, they affect the values of global variables, or maybe they print something out). Then you could get into trouble with this expression: CPP / C++ / C Code:
Here's an example where order of evaluation affects the results: CPP / C++ / C Code:
Now, if you run my little example on your compiler, you may get the same answers as one of my compilers, or something completely different. I don't want to hear arguments about which is "right" and which is "wrong". None of them is "wrong", since the compiler behavior is undefined if the results depend on order of evaluation. As a further note, the following also gives undefined behavior: CPP / C++ / C Code:
Can you tell why? (I don't care what your compiler says, it may or may not give what you think is a reasonable answer, the result is undefined behavior. There is no "right" answer, and there can't be a "wrong" answer; the behavior is undefined according to the rules of C and C++) A final comment: parentheses can change the order of assignment(if they change the precedence of the operations), but they have no particular effect on the order of evaluation --- they might change things, or not. If the behavior is undefined because of dependence on order of evaluation, it's still undefined, no matter how many parentheses. Regards, Dave |
|
#9
|
||||
|
||||
Got it.Order of evaluation is what I had in mind, as I wrote on my posts.
Thank's for the examples. As we say in Israel : "האסימון נפל" Best regards, Kobi Hikri. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need a bit of help with switch() | Caibre | C Programming Language | 5 | 12-Apr-2005 17:02 |
| statement inside array | skyloon | MySQL / PHP Forum | 1 | 25-Aug-2004 11:02 |
| change statement | jilshi | Web Design Forum | 0 | 26-Apr-2004 22:40 |
| Help a C++ Idiot: I am trying to fill an array based on a switch statement. | Psycop | C Programming Language | 2 | 14-Apr-2004 03:12 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The