In: Computer Science
Short circuit evaluation is when the language evaluates the first portion of a BOOLEAN expression and if, knowing the result of the value, then skips the evaluation of the second expression. For example, A & B is false if A is false... no need to evaluate B. A similar scenario is true for OR. Most languages implement short circuit evaluation
create a program for the following languages: FORTRAN
Write the summary of your result
An example could look like:
function f()
{ write('I have been evaluated');
return(1);
}
main()
{ int i=1;
if ( i ==0 && f() )
then write ('true')
else write ('false)
} 33
Given the above example following is the summary for the same.
function f()
{ write('I have been evaluated');
return(1);
}
main()
{ int i=1;
if ( i ==0 && f() )
then write ('true')
else write ('false)
}
In the above case, as i is not equal to 0, thus it will
calculate f() thus "i have been evaluated" string will be
printed.
Then as the function is returning 1, so it will print
true.
Next Example:
function f()
{ write('I have been evaluated');
return(1);
}
main()
{ int i=1;
if ( i ==1 && f() )
then write ('true')
else write ('false)
}
So in the above case as the i is equal to 1 thus the next statement i.e evaluation of f() is not done. Then it will print only true.
Example:
function f()
{ write('I have been evaluated');
return(1);
}
main()
{ int i=1;
if ( i ==1 || f() )
then write ('true')
else write ('false)
}
In this case too as i is equal to 1, thus it will not calculate the function as there is an OR operator. Thus it will simply print "true" but not "I have been evaluated"
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks