Nesting problem with If Then

enonod

Well-Known Member
Licensed User
Longtime User
I have the following
B4X:
Sub Test
   If ... Then 
      ...
   Else If ... Then 
      ...
      End If
      If ... Then 
          ...
      Else
      ...
      End If
   End If
End Sub
Compiling code. Error
Error parsing program.
Error description: Missing Keyword: end sub
Occurred on line: 62
End If
Line 62 being the final End If
Because the first Else has not been closed I cannot see what is wrong with having two nested If's following.
Will somebody please enlighten me.
 

enonod

Well-Known Member
Licensed User
Longtime User
Thank you Erel, can you confirm please that what you are saying is that End If only occurs if an Else has been used?

If (no pun) so I think it might be useful to include that under Keywords - If, because that example does not show in there.
Thank you for your help.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Sorry to drag this out but I need to be sure...
So are you saying then that 'Else If' does not count as 'If' ?
Even if I rewrote it

If ...
Else
If
...


Thanks
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Yes thank you both, I agree that I have one too many and can see it is because I counted the If following the Else as an IF and I should not have done.
I misunderstood the multiline example when I added my nested If and I should have seen it.
Sorry to have wasted time and space.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Setting aside the number of End If's I still have a problem. The problem is what caused me to add the extra End If in an attempt to solve it. I cannot.

'b' alternates to allow lines 5 and 7 to be done alternately unless a is reversed in which case only line 5 is used.
The condition of 'b' determines a bypass or not in lines 4 to 9
B4X:
Sub Test
1    If ... Then 
2        ...
3    Else If a=True Then b=True

4        If b=True Then 
5            ...
6        Else
7        ...
8          b= Not(b)
8        End If
9    End If
10  Whatever is next
End Sub
The intention is that ALL the code following the first Else i.e. 3 to 8 inc should be carried out.
What actually happens is that if the line 3 fails then the next line becomes 10.
I cannot find a place to put or remove and End If that solves it.

If it helps it is a direct translation (supposedly) of my Java which works and only works in this form
B4X:
 if(...){...;}
  else {b=(!a)?true:b;
          if (b){...;} 
          else {...;}     //Miss this if without 'a'
          b=!b;
       }

Any help appreciated, I must have missed looking at one combination
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Erel, apart from my typo when posting ie a=false which in my code is a=true;

The combination I missed was to place the 'If' on a new line instead of the Else line (which never occurred to me when reading the help, should be different).

For my peace of mind, can you please explain (I know you like short answers, but...) where if any, are 'implied' End Ifs in the above code.
The multiline Help does not show this combination and I would like to find it easier next time
I would have called that subtle. I now call it tricky and to be watched out for.
Thank you.
 
Upvote 0

DarkMann

Member
Licensed User
Longtime User
I never use Else If. I too found it a little confusing to tell the difference.

As Erel indicated above, Else can be followed by a new If - Else - End If block that is nested within the first.

When I write an If block and need an ELSE condition, I always do the three lines of code first, all on separate lines in the editor with space for the code they contain tabbed in. If I then need another IF condition I do the same within the already created one.

Sometimes I feel this may be adding a couple of extra lines of code, but it is much more readable. It probably gets compiled down to the same thing anyway.

Of course I can't find an example now!

Hope that helps,

David
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
In your original post you had:

B4X:
Sub Test
    If ... Then 
        ...
    Else If ... Then 
        ...
        End If       <<<<<<<< Not indented to line up with above If.
        If ... Then 
            ...
        Else
        ...
        End If
    End If
End Sub

The problem is that the indentation is incorrect. It should be --

B4X:
Sub Test
    If ... Then 
        ...
    Else If ... Then 
        ...
    End If
    If ... Then 
        ...
    Else
        ...
    End If
    End If
End Sub

With the indentation corrected, it is easy to see the double End If's at the end.

In your last post, you have this --

B4X:
Sub Test
1    If ... Then 
2        ...
3    Else If a=True Then b=True

4        If b=True Then 
5            ...
6        Else
7        ...
8          b= Not(b)
8        End If
9    End If
10  Whatever is next
End Sub

On line 3, it is bad practice to put anything after the Then.
If you move "b=True" down where it belongs, it looks like this --

B4X:
Sub Test
1    If ... Then 
2        ...
3    Else If a=True Then 
4        b=True
5        If b=True Then 
6            ...
7        Else
8            ...
9            b= Not(b)
10       End If
11    End If
12  ...
End Sub

Now it is easy to see that the code does not make any sense because you follow the line "b=True" with the line "If b=True..." where b must always be True. The following would fix the problem, but may not be what you intended --

B4X:
Sub Test
1    If ... Then 
2        ...
3    Else If a=True Then 
4        b=True
4    END IF
5    If b=True Then 
6        ...
7    Else
8        ...
9        b= Not(b)
10   End If
12  ...
End Sub
 
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
The version Erel provided worked fine and his explanation (last two lines) of what I had done wrong was exactly right.

The reason I put Then b=True on one line, bad practice or not was because I thought it was in itself a complete 'block?' and isolated it in my mind from the following section and also the Help says it is OK . BUT NOT as I needed it.

Fail or pass; the following If alt Then will be run but alt will not always true.

The intention was the situation that now exists, where b is not always true.

Thank you all for the time and trouble you have taken, I have learnt a lot from this.
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
Just skimming this thread I find it easier to use the SELECT statement rather than multiple If's its far tidier.
Compares a single value to multiple values.
Example:
Dim value As Int
value = 7
Select value
Case 1
Log("One")
Case 2, 4, 6, 8
Log("Even")
Case 3, 5, 7, 9
Log("Odd larger than one")
Case Else
Log("Larger than 9")
End Select
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
In many cases (pun) your choice will work better but unfortunately not in complicated tests where the basis of each test is on different variables.
 
Upvote 0
Top