'sharing variables in fragment shader GLSL

I have tried this fragment shader. but the result is unexpected.

void main() {
   float test;
   if(gl_FragCoord.x==300.0){
       test=1.0;
   }


   gl_FragColor = vec4(test, 1,1,1);
} 

This program makes all fragments white colored. But it's unexpected because shaders run asynchronously for each fragment. Note that the colour is dependent to value of test in if block. For example if i change it to 0 it will fill all frags with lightblue. I had expected to just fill fragments with x=300. Please explain how ?



Solution 1:[1]

I tested a lot. It seems GLSL considers the first initialization statement, default value for an uninitialized variable; despite where it's located.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Modus Tollens