Starting from B4A v7.0 the following warning will appear for DoEvents calls: DoEvents is deprecated. It can lead to stability issues. Use Sleep(0) instead (if really needed). The purpose of DoEvents was to allow the UI to be updated while the main thread is busy. DoEvents which shares the same...
You don't *have* to define a value as an int gets set to 0 but it appears in this case you may want to.
You can combine the definition like this...
B4X:
dim sa as int = 999 'or whatever value'
However your logic is flawed to get the results you are trying to achieve. Setting sa to anything above 520 will allow your loop to function but it will always result in an output where d = 0 and sa = whatever value you use. You should try stepping through your code (use the F8 key) to see what's happening line by line.