Mixing :OrIf and :Return
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
2 posts
• Page 1 of 1
Mixing :OrIf and :Return
Can one mix :Return with :OrIf?
EG:
∇ age1 foo age2
[1] :If (age1<age2)
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] :Return
[4] :OrIf (age1=age2)
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] :Return
[7] :OrIf (age1>age2)
[8] 'Age ',(⍕age1),' is greater than ',⍕age2
[9] :Return
[10] :End
∇
Lets try:
49 50 51 foo¨ 50
Age 49 is greater than 50
Age 50 is less than 50
Age 51 is less than 50
This suggests that one should not mix the two.
(I always try to avoid using :OrIf as I find the logic too confusing.)
EG:
∇ age1 foo age2
[1] :If (age1<age2)
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] :Return
[4] :OrIf (age1=age2)
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] :Return
[7] :OrIf (age1>age2)
[8] 'Age ',(⍕age1),' is greater than ',⍕age2
[9] :Return
[10] :End
∇
Lets try:
49 50 51 foo¨ 50
Age 49 is greater than 50
Age 50 is less than 50
Age 51 is less than 50
This suggests that one should not mix the two.
(I always try to avoid using :OrIf as I find the logic too confusing.)
Ray Cannon
Please excuse any smelling pisstakes.
Please excuse any smelling pisstakes.
-
ray - Posts: 222
- Joined: Wed Feb 24, 2010 12:24 am
- Location: Blackwater, Camberley. UK
Re: Mixing :OrIf and :Return
You can mix the two but you must understand how :OrIf works.
If you have doubts try translating your code with 'good ol' branches':
as you can see the meaning of all but the last Orif is to execute the code (the last OrIf skips if it is NOT true) and because you exit the program with :return lines [4-7] will never be executed.
What you probably want is this:
or, in control structure terms:
I think :OrIfs are useful but not if you put too much code between them. Then it becomes difficult to read and can be confusing, specially when the code to execute is 25 lines down with lots of OrIfs in between.
If you have doubts try translating your code with 'good ol' branches':
- Code: Select all
∇ age1 foo2 age2
[1] →(age1<age2)/goforit ⍝ :If (age1<age2)
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] :Return
[4] →(age1=age2)/goforit ⍝ :OrIf (age1=age2)
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] :Return
[7] →(age1>age2)↓0 ⍝ :OrIf (age1>age2)
[8] goforit:'Age ',(⍕age1),' is greater than ',⍕age2
∇
as you can see the meaning of all but the last Orif is to execute the code (the last OrIf skips if it is NOT true) and because you exit the program with :return lines [4-7] will never be executed.
What you probably want is this:
- Code: Select all
∇age1 foo3 age2
[1] →(age1<age2)↓eq
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] :Return
[4] eq:→(age1=age2)↓gt
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] :Return
[7] gt:→(age1>age2)↓0 ⍝ unnecessary
[8] 'Age ',(⍕age1),' is greater than ',⍕age2
∇
or, in control structure terms:
- Code: Select all
∇ age1 foo4 age2
[1] :If (age1<age2)
[2] 'Age ',(⍕age1),' is less than ',⍕age2
[3] ⍝ :Return - no need
[4] :ElseIf (age1=age2)
[5] 'Age ',(⍕age1),' is equal to ',⍕age2
[6] ⍝ :Return - no need
[7] :Else ⍝ (age1>age2)
[8] 'Age ',(⍕age1),' is greater than ',⍕age2
[9] :EndIf
∇
I think :OrIfs are useful but not if you put too much code between them. Then it becomes difficult to read and can be confusing, specially when the code to execute is 25 lines down with lots of OrIfs in between.
- DanB|Dyalog
2 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group