logic AND OR

tim griff

Member
Dear All,

20m2 picking up d1 d2 d3 d4 from GSM modem .

These will be four character words NORM, HOUR, FLIP

the PE passes the syntaxon the code below as correct, but "passes" if only the first character is correct. I'm trying to allow both uppercase and lowercase characters

I must be making a school boy error, and I've looked at this too long

regards
Tim




main:



norm_test:
if d1 = 78 or d1 = 110 AND d2 = 79 or d2 = 111 AND d3 = 82 or d3 =114 AND d4 = 77 or d4 = 79 then goto pre_norm


hour_test:'hour
if d1 = 72 or d1 = 104 AND d2 = 79 or d2 = 111 AND d3= 85 or d3= 117 AND d4 = 82 or d4 = 114 then goto pre_hour


flip_test:'flip
if d1 = 70 or d1 = 102 AND d2 = 76 or d2 = 108 AND d3= 73 or d3= 105 AND d4 = 80 or d4 = 112 then goto pre_flip



goto main

pre_norm:
goto main

pre_hour:
goto main

pre_flip:
goto main
 

hippy

Senior Member
It's a bad idea to mix AND and OR in an IF statement and can often lead to things not working. The best solution is to convert lower to upper than check the uppercase, and use ASCII characters where it makes things more readable and understandable ...

Code:
If d1 >= "a" And d1 <= "z" Then : d1 = d1 - "a" + "A" : End If
If d2 >= "a" And d2 <= "z" Then : d2 = d2 - "a" + "A" : End If
If d3 >= "a" And d3 <= "z" Then : d3 = d3 - "a" + "A" : End If
If d4 >= "a" And d4 <= "z" Then : d4 = d4 - "a" + "A" : End If

If d1 = "N" And d2 = "O" And d3 = "R" And d4 = "M" Then ...
If d1 = "H" And d2 = "O" And d3 = "U" And d4 = "R" Then ...
If d1 = "F" And d2 = "L" And d3 = "I" And d4 = "P" Then ...
 

tim griff

Member
It's a bad idea to mix AND and OR in an IF statement and can often lead to things not working. The best solution is to convert lower to upper than check the uppercase, and use ASCII characters where it makes things more readable and understandable ...

Code:
If d1 >= "a" And d1 <= "z" Then : d1 = d1 - "a" + "A" : End If
If d2 >= "a" And d2 <= "z" Then : d2 = d2 - "a" + "A" : End If
If d3 >= "a" And d3 <= "z" Then : d3 = d3 - "a" + "A" : End If
If d4 >= "a" And d4 <= "z" Then : d4 = d4 - "a" + "A" : End If

If d1 = "N" And d2 = "O" And d3 = "R" And d4 = "M" Then ...
If d1 = "H" And d2 = "O" And d3 = "U" And d4 = "R" Then ...
If d1 = "F" And d2 = "L" And d3 = "I" And d4 = "P" Then ...
Thanks hippy this works well, I'm sure I would not have come up with this!! Tim
 

BESQUEUT

Senior Member
It's a bad idea to mix AND and OR in an IF statement and can often lead to things not working.
+1

Another way to write the same thing :
Code:
[color=Blue]symbol [/color][color=Purple]d1[/color][color=DarkCyan]=[/color][color=Purple]b1[/color]
[color=Blue]symbol [/color][color=Purple]d2[/color][color=DarkCyan]=[/color][color=Purple]b2[/color]
[color=Blue]symbol [/color][color=Purple]d3[/color][color=DarkCyan]=[/color][color=Purple]b3[/color]
[color=Blue]symbol [/color][color=Purple]d4[/color][color=DarkCyan]=[/color][color=Purple]b4[/color]


[color=Black]main:[/color]
[color=Blue]do


      [/color][color=Blue]if [/color][color=Purple]d1 [/color][color=DarkCyan]= [/color][color=Red]"N" [/color][color=DarkCyan]or [/color][color=Purple]d1 [/color][color=DarkCyan]= [/color][color=Red]"n" [/color][color=Blue]then
            if [/color][color=Purple]d2 [/color][color=DarkCyan]= [/color][color=Red]"O" [/color][color=DarkCyan]or [/color][color=Purple]d2 [/color][color=DarkCyan]= [/color][color=Red]"o" [/color][color=Blue]then
                  if [/color][color=Purple]d3 [/color][color=DarkCyan]= [/color][color=Red]"R" [/color][color=DarkCyan]or [/color][color=Purple]d3 [/color][color=DarkCyan]=[/color][color=Red]"r" [/color][color=Blue]then
                        if [/color][color=Purple]d4 [/color][color=DarkCyan]= [/color][color=Red]"M" [/color][color=DarkCyan]or [/color][color=Purple]d4 [/color][color=DarkCyan]= [/color][color=Red]"m" [/color][color=Blue]then
                              [/color][color=Green]' pre-norm code
                        [/color][color=Blue]endif
                  endif
            endif
      endif

      if [/color][color=Purple]d1 [/color][color=DarkCyan]= [/color][color=Red]"F" [/color][color=DarkCyan]or [/color][color=Purple]d1 [/color][color=DarkCyan]= [/color][color=Red]"f" [/color][color=Blue]then
            if [/color][color=Purple]d2 [/color][color=DarkCyan]= [/color][color=Red]"L" [/color][color=DarkCyan]or [/color][color=Purple]d2 [/color][color=DarkCyan]= [/color][color=Red]"l" [/color][color=Blue]then
                  if [/color][color=Purple]d3 [/color][color=DarkCyan]= [/color][color=Red]"I" [/color][color=DarkCyan]or [/color][color=Purple]d3 [/color][color=DarkCyan]=[/color][color=Red]"i" [/color][color=Blue]then
                        if [/color][color=Purple]d4 [/color][color=DarkCyan]= [/color][color=Red]"P" [/color][color=DarkCyan]or [/color][color=Purple]d4 [/color][color=DarkCyan]= [/color][color=Red]"p" [/color][color=Blue]then
                              [/color][color=Green]' pre-flip code
                        [/color][color=Blue]endif
                  endif
            endif
      endif

loop[/color]
 

BESQUEUT

Senior Member
It's a bad idea to mix AND and OR in an IF statement and can often lead to things not working. The best solution is to convert lower to upper than check the uppercase, and use ASCII characters where it makes things more readable and understandable ...

Code:
If d1 >= "a" And d1 <= "z" Then : d1 = d1 - "a" + "A" : End If
If d2 >= "a" And d2 <= "z" Then : d2 = d2 - "a" + "A" : End If
If d3 >= "a" And d3 <= "z" Then : d3 = d3 - "a" + "A" : End If
If d4 >= "a" And d4 <= "z" Then : d4 = d4 - "a" + "A" : End If
Another way to write the same thing :
Code:
[color=Navy]#macro [/color][color=Black]ToUpper[/color][color=Blue]([/color][color=Black]Letter[/color][color=Blue])
If [/color][color=Black]Letter [/color][color=DarkCyan]>= [/color][color=Red]"a" [/color][color=DarkCyan]And [/color][color=Black]Letter [/color][color=DarkCyan]<= [/color][color=Red]"z" [/color][color=Blue]Then [/color][color=Black]: Letter [/color][color=DarkCyan]= [/color][color=Black]Letter [/color][color=DarkCyan]- [/color][color=Red]"a" [/color][color=DarkCyan]+ [/color][color=Red]"A" [/color][color=Black]: [/color][color=Blue]End If[/color]
[color=Navy]#endmacro[/color]

[color=Black]main:[/color]
[color=Blue]do
      [/color][color=Black]ToUpper[/color][color=Blue]([/color][color=Purple]d1[/color][color=Blue])
      [/color][color=Black]ToUpper[/color][color=Blue]([/color][color=Purple]d2[/color][color=Blue])
      [/color][color=Black]ToUpper[/color][color=Blue]([/color][color=Purple]d3[/color][color=Blue])
      [/color][color=Black]ToUpper[/color][color=Blue]([/color][color=Purple]d4[/color][color=Blue])
      [/color]
 

westaust55

Moderator
There are typically more than one way to skin the proverbial cat. As some alternatives to hippy&#8217;s code example to convert lower case to upper case:

If d1 >= "a" And d1 <= "z" Then : d1 = d1 - "a" + "A" : End If

Could alternatively become

If d1 >= "a" And d1 <= "z" Then : d1 = d1 AND $DF : End If ; mask bit 5 out

or

If d1 >= "a" And d1 <= "z" Then : d1 = d1 - $20 : End If ; clear bit 5


Both of which will mask / clear bit 5 thereby converting lower case to upper case.
 
Top