GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-May-2005, 07:45
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

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.
  #2  
Old 17-May-2005, 08:00
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
do you mean as in arithmetic expression?
Code:
if((5*5)<=(3*3)) { do work}
i always use prentices just to be sure i get the result i need.
__________________
Xrum!
  #3  
Old 17-May-2005, 08:12
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by kobi_hikri
Is the order of evaluation of expressions in if statement defined ?
Are there any specific examples you have in mind?
Quote:
6.5 Expressions

3 The grouping of operators and operands is indicated by the syntax.71) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

71) The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first. Thus, for example, the expressions allowed as the operands of the binary + operator (6.5.6) are those expressions defined in 6.5.1 through 6.5.6. The exceptions are cast expressions (6.5.4) as operands of unary operators (6.5.3), and an operand contained between any of the following pairs of operators: grouping parentheses () (6.5.1), subscripting brackets [] (6.5.2.1), function-call parentheses () (6.5.2.2), and the conditional operator ?: (6.5.15). Within each major subclause, the operators have the same precedence. Left- or right-associativity is indicated in each subclause by the syntax for the expressions discussed therein.
Quote:
Originally Posted by kobi_hikri
Should we use braces to define the order of evaluation ?
To me I read braces as these: {}, so my answer then would be no. But assuming you mean parentheses () instead of braces, then it may be helpful to enclose subexpressions in parentheses.
  #4  
Old 17-May-2005, 09:21
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

whould the usage of parentheses change this fact ?


Quote:
Originally Posted by Dave Sinkula
6.5 Expressions

3 The grouping of operators and operands is indicated by the syntax.71) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

The header of this message referes to the above qoute.

Quote:
Originally Posted by Dave Sinkula
To me I read braces as these: {}, so my answer then would be no. But assuming you mean parentheses () instead of braces, then it may be helpful to enclose subexpressions in parentheses.

I should have been more accurate...
  #5  
Old 17-May-2005, 18:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by kobi_hikri
Is the order of evaluation of expressions in if statement defined ?
Yes of course they are defined

Quote:
Originally Posted by kobi_hikri
Should we use braces to define the order of evaluation ?
Yes you should use "braces" to make sure you don't have to memorize the order of evaluation -- which also makes your statement more readable anyway.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 18-May-2005, 16:33
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

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

[#1] An expression is a sequence of operators and operands
that specifies computation of a value, or that designates an
object or a function, or that generates side effects, or
that performs a combination thereof.

[#2] Between the previous and next sequence point an object
shall have its stored value modified at most once by the
evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.70)

[#3] The grouping of operators and operands is indicated by
the syntax.71) Except as specified later (for the function-
call (), &&, ||, ?:, and comma operators), the order of
evaluation of subexpressions and the order in which side
effects take place are both unspecified.


Live in peace with this :

Quote:
Originally Posted by WaltP
Yes of course they are defined

What am I missing ?
  #7  
Old 18-May-2005, 18:26
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
>I am using parentheses with sub-expressions
Quote:
Originally Posted by Dave Sinkula
Are there any specific examples you have in mind?
  #8  
Old 18-May-2005, 18:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by kobi_hikri
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) :



Live in peace with this :



What am I missing ?


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:
3+4*5 
has an unambiguous value of 23 (since multiplication has a higher precedence than addition). Assignments of values of sub-expressions are made according to the precedence rules.

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:
f() + g()*h()

Here's an example where order of evaluation affects the results:

CPP / C++ / C Code:
/*
 * This program released into the public domain, and can be used for 
 * any purposes that can be served by your imagination (with no
 * liability assumed by me).
 *
 * Regards,
 *
 * Dave
 *
 * Dave Evans
 * Petaluma, CA
 *
 * May 18, 2005
 *
 *
 * This program is an example of undefined behavior
 *
 * With Microsoft Visual C++, I got the following output
 *
 *  f(): a = 0, b = 1, c = 2
 *  returning a = 0
 *  
 *  g(): a = 0, b = 2, c = 2
 *  returning b = 2
 *  
 *  h(): a = 0, b = 2, c = 3
 *  returning c = 3
 *  
 *  f()+g()*h() = 6
 *
 *  That is, f() was evaluated first, then g(), then h()
 *
 *  Note that the assignment of returned values is correct,
 *  according to the precedence rules:
 *  the value of the expression is 0 + 2 * 3 = 6
 *
 *
 * With Borland bcc32, I got this:
 *
 *
 *  g(): a = 0, b = 1, c = 2
 *  returning b = 1
 *  
 *  h(): a = 0, b = 1, c = 3
 *  returning c = 3
 *  
 *  f(): a = 1, b = 1, c = 3
 *  returning a = 1
 *  
 *  f()+g()*h() = 4
 *
 *  That is, g() is evaluated first, then h(), then f().
 *
 *  Note that the assignment of returned values is correct,
 *  according to the precedence rules:
 *  the value of the expression is 1 + 1 * 3
 *
 *
 * Since the functions have side-effects on global variables
 * used by the other functions, the result depends on the order
 * of evaluation. The order of evaluation is undefined, so the
 * behavior is undefined.
 *
 * (Note that adding parentheses arount the g()*h() term has no
 * effect on the compilers that I used here. They don't change
 * the operator precedence, and order of evaluation is still
 * undefined.)
 *
 */
#include <stdio.h>

  int f(void);
  int g(void);
  int h(void);

  int a;
  int b;
  int c;

int main()
{

  a = 0;
  b = 1;
  c = 2;
  printf("f()+g()*h() = %d\n", f()+g()*h());

  return 0;
}

int f()
{
  printf("f(): a = %d, b = %d, c = %d\n", a, b, c);
  printf("returning a = %d\n\n", a);
  b++;
  return a;
}

int g()
{
  printf("g(): a = %d, b = %d, c = %d\n", a, b, c);
  printf("returning b = %d\n\n", b);
  c++;
  return b;
}

int h()
{
  printf("h(): a = %d, b = %d, c = %d\n", a, b, c);
  printf("returning c = %d\n\n", c);
  a++;
  return c;
}


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:
a[i] = i++;

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  
Old 19-May-2005, 03:27
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

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 GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 10:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.