1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

If then else endif nested

Discussion in 'Oshonsoft' started by camerart, Jan 2, 2016.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.
    Hi,

    Can someone clarify these please:

    IF a=b THEN
    IF c=d THEN
    Do something
    ELSE
    Do somethng different
    ENDIF
    ENDIF

    1st/ if a =b and c=d
    2nd if a=b and c does not = d
    3rd if a does not = b and c=d

    What does THEN mean?

    I use these all the time, but have to play around till they do what I want. It woul;d be easier if I had a crib sheet.
    Thanks, Camerart.
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    Normally the statements run like this..

    if a=b then do this!! The else isn't required!!
    if a didn't equal b if would dismiss and carry on!!

    You can also have two if criteria..
    Code (basic):

    if a=b and c=d then
       do this
    else
      do that
    endif
     
     
  3. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.
    Hi Ian,

    Thanks, I think the other two examples I posted are what's called nesting, but so far I haven't found a clear explanation.

    C.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.

    Hi,

    Just done a search and found this: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/nest-if.html I'll work through it tomorrow.

    C.
     
  6. zahwi

    zahwi Member

    Joined:
    Oct 24, 2015
    Messages:
    186
    Likes:
    16
    1st/ if a =b and c=d > Do something
    2nd if a=b and c does not = d > Do somethng different
    3rd if a does not = b and c=d > do nothing

    This is one if nested in another if. If the first if is true it evaluate the second (nested) one
    IF a=b THEN

    IF c=d THEN
    Do something
    ELSE
    Do somethng different
    ENDIF

    ENDIF

    THEN is only part of the structural of IF statement, the compiler needs it to know that it's the end of the conditions.
     
  7. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    Hi

    Nesting means you are placing an If-Then statement inside another If-Then statement:

    # if Y=1 then evaluate the nested if-then-else code block
    If (Y=1)

    # begin nested if-the-else code block
    If (X=1) Then
    # do this
    Else
    #do this instead
    EndIf
    # end of nested if-the-else code block

    EndIf
     
  8. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.
    Hi,

    Is this all correct?

    C.
     

    Attached Files:

  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    No!!
    IF( statement is true ) THEN do the true thing
    ELSE do the untrue thing
    ENDIF
     
  10. zahwi

    zahwi Member

    Joined:
    Oct 24, 2015
    Messages:
    186
    Likes:
    16
    Ian, look at it again, camerart passed his test 100%.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    I have looked at it several times.. The Logic is right but it looks misplaced.... The branch after the "then" isn't entirely correct.

    I have just done this..

    upload_2016-1-3_11-3-17.png
     
  12. zahwi

    zahwi Member

    Joined:
    Oct 24, 2015
    Messages:
    186
    Likes:
    16
    To me it looks like camerart answered 3 separate questions and the A in question 1 is not the A in question 2 or 3.
    I can't see the differences between your answer and his answer to question 3 .
     
    Last edited: Jan 3, 2016
  13. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.
    Hi,

    There are three different examples.

    Ian, can you open my image with 'say' PAINT (start > all programs> accessories> PAINT) and point to the offending 'THEN' please.

    I hope your correct Zahwi:), it's not as easy as it looks.

    C.
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    Its not offensive, Its just you mentioned that it was the THEN of the loop that confused you...

    As I said your logic is spot on.... Realistically there should be a branch off of the true...

    My take:----
    upload_2016-1-3_19-30-58.png
     
  15. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,326
    Likes:
    11
    Location:
    Dorset UK.
    Hi Ian,
    I see! It's the way we see things. As long as I've got the logic correct, that's the main thing. I suppose that after each NOT TRUE the branch should go to it's ENDIF for accuracy.

    When I read THEN IF, it doesn't ring true to me, but that's ok.

    Thanks for all inputs, it's clearer now.

    EDIT: With ENDIFs connected. Is the logic still correct? Are all the ENDIF connections correct?

    C.
     

    Attached Files:

    Last edited: Jan 4, 2016

Share This Page