# If then else endif nested

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

1. ### camerartActive Member

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

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.

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. ### camerartActive 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.

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

5. ### camerartActive 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. ### zahwiMember

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. ### eTechActive 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
EndIf
# end of nested if-the-else code block

EndIf

8. ### camerartActive Member

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

Is this all correct?

C.

#### Attached Files:

• ###### IF THEN ELSE ENDIF.gif
File size:
8.2 KB
Views:
96

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. ### zahwiMember

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

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..

12. ### zahwiMember

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.

Last edited: Jan 3, 2016
13. ### camerartActive 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.

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:----

15. ### camerartActive 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:

• ###### IF THEN ELSE ENDIF NESTED.gif
File size:
8.5 KB
Views:
97
Last edited: Jan 4, 2016