Simplifying IF..THEN based code
Hello all,
I'm trying to simplify some code based on the IF...THEN statement. I·thought the branch and Select...case instructions·are possible alternatives...generally. Looking at my code I see a possibility to replace it with the select...case instruction, however this will not save me any space nor time. Therefore I started with the branch instruction.·However as this instruction seems to rely·on simple whole-number variables/constants I·don't see how I can·use branch in stead of the if...then·instructions in my code.·Could·somebody·please have a look at my code and tell me what direction I should go, or maybe if he/she·comes to the same conclusion as I did, that is.. that it's not possible to use some other instruction·for the·if...then statements for this particular code (due to the use of input data together with the outcome of a statement). You're help is really appreciated.
Kind regards Arno (from the Netherlands)
Find the code below, as one can se it's a subroutine, I have 5 more in my program all comparable to this one.
'---subroutine2---
Forward2:
fw2=fw2+1
IF fw2>=120 THEN
GOSUB standing
ENDIF
IF IN4^IN5=1 THEN
GOSUB standing
ENDIF
DEBUG DEC fw2,CR
IF IN1 THEN Forward2
IF fw2 <60 AND IN0=1 THEN
DEBUG "spd1",CR
ELSEIF fw2 <60 AND IN3=1 THEN
DEBUG "spd2",CR
ELSEIF fw2<75>=60 AND IN0=1 THEN
DEBUG "spd3",CR
ELSEIF fw2<75>=60 AND IN3=1 THEN
DEBUG "spd4",CR
ELSEIF fw2<90>=75 AND IN0=1 THEN
DEBUG "spd5",CR
ELSEIF fw2<90>=75 AND IN3=1 THEN
DEBUG "spd6",CR
ELSEIF fw2<120>=90 AND IN0=1 THEN
DEBUG "spd7",CR
ELSEIF fw2<120>=90 AND IN3=1 THEN
DEBUG "spd8",CR
ENDIF
fw2=0
GOTO main··
I'm trying to simplify some code based on the IF...THEN statement. I·thought the branch and Select...case instructions·are possible alternatives...generally. Looking at my code I see a possibility to replace it with the select...case instruction, however this will not save me any space nor time. Therefore I started with the branch instruction.·However as this instruction seems to rely·on simple whole-number variables/constants I·don't see how I can·use branch in stead of the if...then·instructions in my code.·Could·somebody·please have a look at my code and tell me what direction I should go, or maybe if he/she·comes to the same conclusion as I did, that is.. that it's not possible to use some other instruction·for the·if...then statements for this particular code (due to the use of input data together with the outcome of a statement). You're help is really appreciated.
Kind regards Arno (from the Netherlands)
Find the code below, as one can se it's a subroutine, I have 5 more in my program all comparable to this one.
'---subroutine2---
Forward2:
fw2=fw2+1
IF fw2>=120 THEN
GOSUB standing
ENDIF
IF IN4^IN5=1 THEN
GOSUB standing
ENDIF
DEBUG DEC fw2,CR
IF IN1 THEN Forward2
IF fw2 <60 AND IN0=1 THEN
DEBUG "spd1",CR
ELSEIF fw2 <60 AND IN3=1 THEN
DEBUG "spd2",CR
ELSEIF fw2<75>=60 AND IN0=1 THEN
DEBUG "spd3",CR
ELSEIF fw2<75>=60 AND IN3=1 THEN
DEBUG "spd4",CR
ELSEIF fw2<90>=75 AND IN0=1 THEN
DEBUG "spd5",CR
ELSEIF fw2<90>=75 AND IN3=1 THEN
DEBUG "spd6",CR
ELSEIF fw2<120>=90 AND IN0=1 THEN
DEBUG "spd7",CR
ELSEIF fw2<120>=90 AND IN3=1 THEN
DEBUG "spd8",CR
ENDIF
fw2=0
GOTO main··
Comments
Used a for next loop to simulate fw2 minimum and maximum values
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
I tried your suggested code and have tried all kinds of combinations to get this into my code . However it doesn't work like it should. It even does some strange things. What happens is that when I run the code the debug screen neer shows any SPD1, SPD2, etc. With my old code the debug screen showed something like:
1
2
3
4
5
6
..
..
66
67
spd1··········· <= this is how it should be it counts until I hit an IN0, IN2, IN3, etc.
1·················<=then starts over again
2
3
..
..
93
94
spd6
This is how it should go. But with your code it goes something like:
1
2
3
...
10
11
12
13d········ <= why the D???
24········· <=counting by 11???
35
46
57
68
79
OR
1
2
3
..
..
10
11
12
13d····· <= always 13d and/or 14d sometimes "normal" 13 o 14 very strange
101····· <=sometimes it jumps 30, 40, 50 counts
102
103
1
2
sp3······ <=sometimes it shows sp and than a number
so it looks like a bug or something but I encounter this with 2 different BS2's and also with my BSpx
I really can't figure out what's happening? I hope somebody knows what's going on.
Thank you in advance,
Arno (the Netherlands)
·
It is hard to figure without your complete code. All I can see is your first try subroutine and a snippet that I suggested to cover part of that subroutine.
@allenlane, hehe, :
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
It worked, I got it working. Thank you very much for your valuable help.
By the way why did you use 49 in the equation x = x * 2 + 49, any number will do... or not?
Have a great new year all!
Greetings, Arno
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Kind regards