Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

preference of logical operation in bascom avr

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Fri Dec 05, 2014 6:06 pm    Post subject: preference of logical operation in bascom avr Reply with quote

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
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1571
Location: Basel, Switzerland

switzerland.gif
PostPosted: Fri Dec 05, 2014 6:26 pm    Post subject: Reply with quote

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
View user's profile
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Fri Dec 05, 2014 7:29 pm    Post subject: Reply with quote

Hi

i test brackets (), it dont work on bascom!
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1571
Location: Basel, Switzerland

switzerland.gif
PostPosted: Fri Dec 05, 2014 7:41 pm    Post subject: Reply with quote

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
View user's profile
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Fri Dec 05, 2014 8:18 pm    Post subject: Reply with quote

so what can i do?
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1571
Location: Basel, Switzerland

switzerland.gif
PostPosted: Fri Dec 05, 2014 8:58 pm    Post subject: Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1135

poland.gif
PostPosted: Fri Dec 05, 2014 9:57 pm    Post subject: Reply with quote

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 Very Happy I test this (without lcd routines)
Back to top
View user's profile Visit poster's website
16mk

Bascom Member



Joined: 26 Feb 2016
Posts: 19

poland.gif
PostPosted: Sat Mar 04, 2023 11:32 am    Post subject: Reply with quote

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


= 1
= 1
= 1
= 2
= 2
= 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
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum