Android Question DO LOOP PROBLEM.

sonistp

Member
dim d, sa as int
Do Until (sa-520)<0
d=d+1
sa=1400-(d*520)
Loop
Log(d)
Log(sa

expected result is sa=360 and d=2
but, i always got sa=0 and d=0

Anyone can help for my problem?
 

John Naylor

Active Member
Licensed User
Longtime User
When you define your int's they default to 0 so you're straight away saying

Do until -520 < 0 which of course it is and the loop exits immediately.

[you should use the code option to display code by the way like this...]


Loop problem:
    Dim d, sa As Int
    Do Until (sa-520)<0
        d=d+1
        sa=1400-(d*520)
    Loop
    Log(d)
    Log(sa)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
EDIT: WRONG ANSWER, IGNORE

You should not use Do Loop anymore

 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi José,
as you indicated it is DoEvents that was deprecated not Do Until loops. Too fast reading? It happens :)
 
Upvote 0

sonistp

Member
loop problem:
 dim
When you define your int's they default to 0 so you're straight away saying

Do until -520 < 0 which of course it is and the loop exits immediately.

[you should use the code option to display code by the way like this...]


Loop problem:
    Dim d, sa As Int
    Do Until (sa-520)<0
        d=d+1
        sa=1400-(d*520)
    Loop
    Log(d)
    Log(sa)
Thankyou for you answer really help me for this situation.
So, i must define value of sa after define the int of sa.
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
So, i must define value of sa after define the int of sa.

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.
 
Upvote 0
Top