Software Archive
Read-only legacy content
17061 Discussions

.or. - expression

faidor
Novice
580 Views
Hello to all,

following question: Because of calculating speed, I would like to know the following. When using the .or.-expression in for example an if-else-statement. How is it internally parsed? Does Fortran first check the first expression and, if it is true, it skips checking for the second? Or does it check both expressions in any case?

Example
if ( (input .eq. 1.0) .or. (input .eq. 2) ) then
do something
end if

-> does Fortran check the frist expression (input=1?) and, if true skips checking the second (input=2?), or does it check both wether the first one is true or not?

I appreciate every answer, thanks a lot,

Andre
0 Kudos
6 Replies
Steven_L_Intel1
Employee
580 Views
The compiler may evaluate either or both subexpressions, in either order, as it sees fit. It might even not evaluate either if it can determine what the result is at compile-time. If you want to force the order of evaluation, or do 'short-circuit' evaluation, use nested IF-THEN.

Steve
0 Kudos
durisinm
Novice
580 Views
According to the CVF Language Reference Manual, Section 4.1.4, "You should not write logical expressions whose results might depend on the evaluation order of subexpressions. The compiler is free to evaluate subexpressions in any order."

Mike
0 Kudos
faidor
Novice
580 Views
Thanks for the fast answer...
I was just too much programming Perl in the last time -> in Perl the order is defined by the programmed order ;-).
that was very helpful.

@Steve: I wanted to circumvent using nested if then, since comparisons like "if"-conditions and loops are the most computing power using elements (in general - afaik), so I wanted to reduce necessary comparisons. Anyhow, I do know something more - thanks a lot!

Andre
0 Kudos
Steven_L_Intel1
Employee
580 Views
There really isn't any difference between nested IFs and compound expression IFs. If anything, the compiler has a better chance of optimizing well with nested IFs.

You might want to see if the SELECT CASE statement is appropriate to your application - if the case values are from a small set of integers, the compiler can generate special optimized code for the selection.

Steve
0 Kudos
faidor
Novice
580 Views
Thanks, Steve, I'll keep that in mind and check it out. Is this special optimized code also usable for non-equal cases with ocmparisons to real numbers? Like greater than, lower than? I would have to compare some values to be smaller than 1.00001 and bigger than -0.00001. Normally it would be ok to use 0 and 1, but I have to deal with rounding errors ;-).
Thanks a lot, greetz form germany,

Andre
0 Kudos
Steven_L_Intel1
Employee
580 Views
If that's what you're doing, then SELECT CASE isn't for you. When comparing reals, you probably want something like this:


if (abs(x-1.0) < 0.00001) then ...


Steve
0 Kudos
Reply