View previous topic :: View next topic |
Author |
Message |
hast66
Joined: 18 Aug 2005 Posts: 28
|
Posted: Thu May 24, 2007 6:54 pm Post subject: for next question, 1 to far |
|
|
Hi
Code: |
Dim X As Byte , Rood As Byte
For X = 0 To 255
Rood = X
Next
End 'end program
|
this gives as final result: rood = 255 and X=0
seems not logical to me, X should go to 0 and not further
now I adjust x after the next instruction, but if I use interupts I have a problem since rood<>x
any comments/solutions? |
|
Back to top |
|
|
rileyesi
Joined: 19 Dec 2006 Posts: 398
|
Posted: Thu May 24, 2007 7:43 pm Post subject: |
|
|
Hi.
You need to define the step size for your FOR NEXT loop as well as tell the program which variable you want to NEXT.
Try this code:
Code: |
Dim X As Byte , Rood As Byte
For X = 0 To 255 Step 1
Rood = X
Print "X= " ; X ; " Rood= " ; Rood
Waitms 10
Next X
|
I like to use PRINT statements to help debug. If you run this, you will see that both X and Rood go from 0 to 255.
Hope this helps.
Regards,
Pete |
|
Back to top |
|
|
rileyesi
Joined: 19 Dec 2006 Posts: 398
|
Posted: Thu May 24, 2007 7:53 pm Post subject: |
|
|
Hi.
Actually, I ran your code in the simulator (I did not before my last post) and it works fine. Add a print statement and you will see it count up.
Code: |
Dim X As Byte , Rood As Byte
For X = 0 To 255
Rood = X
Print X ; Rood
Next
|
Pete[/quote] |
|
Back to top |
|
|
hast66
Joined: 18 Aug 2005 Posts: 28
|
Posted: Fri May 25, 2007 10:18 am Post subject: |
|
|
thanks for the reaction
I also ran it on the simulator. I monitor the variables (wich updates the display of it, only after a pause or step buttonclick).
Now if you let the code finish and then click pause or step, you should see my results.
After the last time Next is executed, X goes 1 step further.
Also if it goes down: For x = 255 to 0 step -1.
X ends at 255 |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Fri May 25, 2007 12:25 pm Post subject: |
|
|
kolonelvonklink wrote: | After the last time Next is executed, X goes 1 step further. |
Hi,
This is normal, without that, how do you check if it is time to
exit the FOR loop? This is the same for any other programming
language.
* * *
From your first post:
kolonelvonklink wrote: | but if I use interupts I have a problem since rood<>x |
Code: | Dim X As Byte , Rood As Byte
For X = 0 To 255
Rood = X
Next
End |
While the FOR loop is executed:
If the interrupt occurs just before "Rood = X", rood and x will be different.
If the interrupt occurs just after "Next", rood and x will be different.
Best regards,
Luciano |
|
Back to top |
|
|
oe9vfj
Joined: 17 Jun 2004 Posts: 269 Location: Austria, Hard
|
Posted: Fri May 25, 2007 1:34 pm Post subject: |
|
|
This effort depends, how FOR ... NEXT work.
In the FOR X = 0 TO 255
x is incremented by 1. If the new value is greater then highest desired value, the FOR ... NEXT loop ends and leaves X with the value of STEP (1) higher then the TO Value.
If you want, that X have the last value in the loop, you can
DECR X
after the NEXT statement _________________ regards Josef
DOS - File System for BASCOM-AVR on http://members.aon.at/voegel |
|
Back to top |
|
|
hast66
Joined: 18 Aug 2005 Posts: 28
|
Posted: Sat May 26, 2007 6:51 pm Post subject: |
|
|
Still this seems contra-intuitive to me. I would write a compiler to leave the loop when x equals the TO value so after the loop x still equals the TO value and not one more (or less).
isn't this some kind of bug
PS the interrupt problems could easely be solved by disable en enable this interrupt in the loop |
|
Back to top |
|
|
lpa
Joined: 12 Dec 2006 Posts: 27 Location: Oreokastro, Greece
|
Posted: Sun May 27, 2007 7:26 am Post subject: |
|
|
you can use the following code if you want to preserve the value of X
Code: | Dim X As Byte , Rood As Byte, Limit as Byte
Limit=255
For X = 0 To Limit
Rood = X
If X=Limit then
exit For
end if
Next
End |
|
|
Back to top |
|
|
|