Talk:Short-circuit evaluation: Difference between revisions

(→‎Compiler optimisations?: Covered already?)
Line 51:
::If short-circuiting isn't covered by the language then the last sentence of the task description should hold: ''"If the language does not have short-circuit evaluation, this might be achieved with nested if statements."'' If short-circuiting is covered by the language then a compiler is either wrong or any optimisations must preserve short-circuit operations.
::--[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 15:24, 29 April 2015 (UTC)
 
Just a few notes about Fortran. The references refer to the Fortran 2003 standard.
 
Short-circuit evaluation is allowed by the standard, but not mandatory: ''It is not necessary for a processor to evaluate all of the operands of an expression, or to evaluate entirely each operand, if the value of the expression can be determined otherwise.'' (7.1.8.1) It may be painful if evaluation causes side effects, or when testing for an array index before testing the array value.
* For example, ''IF(I < N .AND. A(I) < X) ...'' may crash the program. In languages with guaranteed short-circuiting, this is a usual construct though.
* In ''Z = F(X) .AND. Y'', the ''F(X)'' part may, or may not, be evaluated. This could of course be a problem if ''F'' has side effects.
* However, in ''Z = F(X) . AND. G(Y)'', if ''F(X)'' has side effects that change the value of ''G(Y)'', this is not standard conformant: ''The evaluation of a function reference shall neither affect nor be affected by the evaluation of any other entity within the statement.'' (7.1.8)
 
[[User:Arbautjc|Arbautjc]] ([[User talk:Arbautjc|talk]]) 10:27, 30 April 2015 (UTC)
Anonymous user