How is this code working?? val3 is defined as constant
In your code, the const int val3 = 7; definition, is outside of any functions so it defines a global constant/variable (available in every part of your code).

The int val3 = 8; definition is inside your setup () function, so it acts as a local variable only within that function. This overrides the val3 constant defined outside of the setup () function. I.e. the global val3 constant and the local val3 variable inside of setup () are not the same variable, which will inevitably lead to confusion (as in your case).

It’s valid syntax but I don’t consider it good coding practice. It’s good to know why it works that way though.