Xorifelse
I'd love to elaborate about discussions...........
- Reaction score
- 87
Me and 2 clan members were discussing in topic topic how conditions were evaluated. I decided to post it here as well, cause this could be quite useful in resolving bugs.
This being said let me point out that we knew this already but rather what has priority.
In mathematics there are specific rules in what order it is calculated, let me give an example.
If done correctly, this should be your calculation (1+((2*3)-(4/5))) and the answer should be 6.2.
Now a mathematics calculation isn't too much off from evaluating a condition, just like here you can control the flow of execution by using parenthesis's.
Take a look at this php script for example:
However, what is prioritized in this evaluation?
My guts tell me the "&&" evaluation is skipped because the variable before the or statement returned true already, however there are people who think, or thought otherwise and think the && is evaluated first in wich case the script execution can change completely.
Now I'm not saying that he / she is wrong, because there are many languages out there who may prioritize but usually this is an obsolete thought. You should use parenthesis's by default to avoid any issues with this.
For example, Warcraft III reads this script ( converted to Jass ) from left to right.
However, my question to you is what do you think is the output of this script?
I personally think that this output can differ in different programming languages [Criticism _needed].
Don't test it, just compile this shizzle in your head and just vote what you think the output is!
It is actually quite mind-boggling
This being said let me point out that we knew this already but rather what has priority.
In mathematics there are specific rules in what order it is calculated, let me give an example.
- ^
- Square Root
- *
- /
- +
- -
- 1 + 2 * 3 - 4 / 5
- 1 + 6 - 4 / 5
- 1 + 6 - 0.8
- 7 - 0.8
If done correctly, this should be your calculation (1+((2*3)-(4/5))) and the answer should be 6.2.
Now a mathematics calculation isn't too much off from evaluating a condition, just like here you can control the flow of execution by using parenthesis's.
Take a look at this php script for example:
PHP:
$bool = true;
function relapse()
{
global $bool;
return $bool = ! $bool;
}
if( $bool || $bool && ! relapse() )
{
print 'A';
}
if( $bool )
{
print 'B';
}
else
{
print 'C';
}
However, what is prioritized in this evaluation?
My guts tell me the "&&" evaluation is skipped because the variable before the or statement returned true already, however there are people who think, or thought otherwise and think the && is evaluated first in wich case the script execution can change completely.
Now I'm not saying that he / she is wrong, because there are many languages out there who may prioritize but usually this is an obsolete thought. You should use parenthesis's by default to avoid any issues with this.
For example, Warcraft III reads this script ( converted to Jass ) from left to right.
However, my question to you is what do you think is the output of this script?
I personally think that this output can differ in different programming languages [Criticism _needed].
Don't test it, just compile this shizzle in your head and just vote what you think the output is!
It is actually quite mind-boggling