- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to implement some Fortran code based on some C code but there is a statement that I don't understand, so can someone please tell me whar does
j += (i-j);
do? If i == j then is j incremented or is it incrementewd when they are no equal?
Thanks much,
Brooks
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you set aside any optimizations that the compiler can do, the following is a straightforward description of the effect of executing this statement.
The right hand side expression is evaluated with the current values of the variables i and j. The result is added to the current value of j, and the sum is stored in j.
Optimization may cause some of these operations to be bypassed if unnecessary, and memory reads and stores may be delayed or bypassed if j is already in a register or is used in the statements that come after the current one. The optimizer could note that the statement has the same effect as
j = i
and implement this with load/store instructions without doing any arithmetic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You didn't answer my question. Ignore optimization. What is (i=j) compiled to mean. If it is .true. or .false.? so what does "i += (i=j)" mean. If i == j then does this translate to i = i + 1? And if i /= j then does it translate to i = i + 0? I know what += means I just want to know what "(i = j)" means related to the variable "i".
Brooks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What you ask in #3 is a different question than the one in #1. The way boolean expressions are handled is different in C and Fortran. In C, i=j; implies assignment, not comparison. If you use it as an expression or a sub-expression, the value is the result of the assignment. You seem to have the misconception that i=j is a boolean expression. You should consult an introductory book for C. The following program may help you understand how your two questions are answered in C.
#include <stdio.h> int main(void){ int i,j; i = 2; j = 3; j += (i-j); printf("j = %d\n",j); // #1 i = 2; j = 3; i += (i=j); printf("i = %d\n",i); // #3 }
The output from this program:
j = 2 i = 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your original post had (I-j) which is subtraction. If indeed (I=j) is written, then the = in this case is assignment, not comparison. The result is that the value of j is assigned to i and then assigned back to j. I have to assume this is NOT what the actual code has - if it is, that's likely to be an error.
Now if the code actually has (I==j), then what gets assigned to j is 1 if I and j have the same value and 0 otherwise. C doesn't have the concept of .true. and .false.. If you wrote this same statement in Fortran, it would not be standard-conforming since you're not alloweed to assign a LOGICAL expression to an integer (I assume i and j are integers.) Intel Fortran lets you do this as an extension - you'll get -1 or 0 by default, or 1 and 0 if -standard-semantics is in effect.
Perhaps you need to restate the question and verify that you are representing the code correctly.
![](/skins/images/C4F7392A5C68ACA60290C9576F107044/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page