View previous topic :: View next topic |
Author |
Message |
hamedhamedi
Joined: 12 Feb 2012 Posts: 70
|
Posted: Fri Dec 05, 2014 6:06 pm Post subject: preference of logical operation in bascom avr |
|
|
Hi
i want to know wich of operation is claculate in basic first?
and or not?
for example when i write the code:
Code: | if a>b and c<d or f>g and g<d then set status_bit
|
which operation calculate first and which one second?
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Fri Dec 05, 2014 6:26 pm Post subject: |
|
|
Hi,
Why not use brackets () to make it clearer for you and the Compiler:-
so do you mean:
if (a>b and c<d) or (f>g and g<d) then set status_bit
or maybe
if a>b and (c<d or f>g) and g<d then set status_bit
use brackets to group the conditions you want to combine
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
hamedhamedi
Joined: 12 Feb 2012 Posts: 70
|
Posted: Fri Dec 05, 2014 7:29 pm Post subject: |
|
|
Hi
i test brackets (), it dont work on bascom! |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Fri Dec 05, 2014 7:41 pm Post subject: |
|
|
Hi,
Oh sorry, I though that worked in Bascom. Looks as if I've spent far to much time programming in other languages in the last few months.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
hamedhamedi
Joined: 12 Feb 2012 Posts: 70
|
Posted: Fri Dec 05, 2014 8:18 pm Post subject: |
|
|
so what can i do? |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Fri Dec 05, 2014 8:58 pm Post subject: |
|
|
Hi,
The only Thing I can think of is split the IF into several if's that set a variable, that you later use something like
Code: |
Result=0
if A > 0 and B > 0 then
Result=1
endif
if Result=0 and C>0 and D>0 then
Result=1
endif
if Result = 1 then
Do something
endif
|
Not nice, but atleast it's obvious what the code does. Another way would be to use else to handle the or, something like
Code: |
Result=0
if A > 0 and B > 0 then
Result=1
else
if C>0 and D>0 then
Result=1
endif
if Result = 1 then
Do something
endif
|
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Dec 05, 2014 9:57 pm Post subject: |
|
|
Question was "what is first and what is second"
I always think that is simple but I test this in simulator (run LCD also)
Code: | $sim
$regfile = "m32def.dat"
Dim A As Byte , B As Byte , C As Byte , D As Byte , F As Byte , G As Byte
Dim Status_bit As Bit
A = 1
B = 1
C = 1
D = 2
F = 2
G = 1
If A > B And C < D Or F > G And G < D Then Set Status_bit
Cls
Lcd Status_bit
End
|
Or is tested at the end
This work too
Code: | $sim
$regfile = "m32def.dat"
Dim A As Byte , B As Byte , C As Byte , D As Byte , F As Byte , G As Byte
Dim Status_bit As Bit
A = 1
B = 1
C = 1
D = 2
F = 2
G = 1
If A > B Then
If C < D Then Set Status_bit
Elseif F > G Then
If G < D Then Set Status_bit
End If
'If A > B And C < D Or F > G And G < D Then Set Status_bit
Cls
Lcd Status_bit
End |
With IF`s code is 16 bytes smaller I test this (without lcd routines) |
|
Back to top |
|
|
16mk
Joined: 26 Feb 2016 Posts: 19
|
Posted: Sat Mar 04, 2023 11:32 am Post subject: |
|
|
EDC writes:
Code: |
$sim
$regfile = "m32def.dat"
Dim A As Byte , B As Byte , C As Byte , D As Byte , F As Byte , G As Byte
Dim Status_bit As Bit
A = 1
B = 1
C = 1
D = 2
F = 2
G = 1
If A > B And C < D Or F > G And G < D Then Set Status_bit
Cls
Lcd Status_bit
End |
Or is tested at the end
In my example, it turns out that the conditions are checked in the order in which they were written. Try to check two conditions:
T = 3, K = &H80, N = &HAA
1. If T > 2 And K <> &H0D Or T > 2 And N <> &HAA Then
2. If T > 2 And N <> &HAA Or T > 2 And K <> &H0D Then
Mathematically both cases are equivalent: A or B, the second B or A but in Bascom 2085 they give different results 1. false 2. true
Checking one by one comes out as in Bascom, mathematically both true.
Maybe in version 2085 something has changed but the problem is there.
Bascom 2085 |
|
Back to top |
|
|
|