# If then end if

Is there a limit to the number of Or’s in an if then statement?

Haven’t met one, but if you are considering writing something which tests that, you are making life hard for yourself.

``````if a or b or c or d or e or f or g or h or i or j or k then //etc

end if
``````

The line length i code gets excessive very quickly, and any changes you need to make in the future will be difficult to edit.

for clarity (if not speed), I would be tempted to

``````dim do_it as boolean
do_it = do_it or a
do_it = do_it or b
do_it = do_it or c
do_it = do_it or d
do_it = do_it or e
do_it = do_it or f
//as many as you like

if do_it then

end if
``````
2 Likes

Other than the practical limits of what makes sense, I don’t think so. But if you have that many to even prompt this question, think about doing what you’re doing differently.

One possibility:

``````Select Case False
Case condition1
Case condition2
Case condition3

Case Else
// All conditions passed

End Select
``````
3 Likes

This is my code (number partitions)
If _
t(1,1) + t(2,1) + t(3,1) + t(4,1) + t(5,1) + t(6,1) = n Or _
t(1,1) + t(2,1) + t(3,1) + t(4,1) + t(5,2) = n Or _
t(1,1) + t(2,1) + t(3,1) + t(4,2) + t(6,1) = n Or _
t(1,1) + t(2,1) + t(3,1) + t(4,3) = n Or _
t(1,1) + t(2,1) + t(3,2) + t(5,1) + t(6,1) = n Or _
t(1,1) + t(2,1) + t(3,2) + t(5,2) = n Or _
t(1,1) + t(2,1) + t(3,3) + t(6,1) = n Or _
t(1,1) + t(2,1) + t(3,4) = n Or _
t(1,1) + t(2,2) + t(4,1) + t(5,1) + t(6,1) = n Or _
t(1,1) + t(2,2) + t(4,1) + t(5,2) = n Or _
t(1,1) + t(2,2) + t(4,2) + t(6,1) = n Or _
t(1,1) + t(2,2) + t(4,3) = n Or _
t(1,2) + t(3,1) + t(4,1) + t(5,1) + t(6,1) = n Or _
t(1,2) + t(3,1) + t(4,1) + t(5,2) = n Or _
t(1,2) + t(3,1) + t(4,2) + t(6,1) = n Or _
t(1,2) + t(3,1) + t(4,3) = n Or _
t(1,2) + t(3,2) + t(5,1) + t(6,1) = n Or _
t(1,2) + t(3,2) + t(5,2) = n Or _
t(1,2) + t(3,3) + t(6,1) = n Or _
t(1,2) + t(3,4) = n Or _
t(1,3) + t(4,1) + t(5,1) + t(6,1) = n Or _
t(1,3) + t(4,1) + t(5,2) = n Or _
t(1,3) + t(4,2) + t(6,1) = n Or _
t(1,3) + t(4,3) = n Or _
t(1,4) + t(5,1) + t(6,1) = n Or _
t(1,4) + t(5,2) = n Or _
t(1,5) + t(6,1) = n Then

Something does not work but the error will be in another place
Thank you

(Please use the code tags when posting code to make it easier for us to help you.)

That’s fine, but this is more readable and maintainable:

``````Var wasMatch As Boolean = True

Select Case n
Case t(1,1) + t(2,1) + t(3,1) + t(4,1) + t(5,1) + t(6,1)
Case t(1,1) + t(2,1) + t(3,1) + t(4,1) + t(5,2)
Case t(1,1) + t(2,1) + t(3,1) + t(4,2) + t(6,1)
...

Case Else
wasMatch = False
End Case

If wasMatch Then ...
``````
1 Like

(sorry)

I will try this

Thank you

The error was in the development of the partition. Items were missing.
Using IF a or b THEN saves a lot of execution time. If a part of the OR (from left to right) is true, the rest is not evaluated.
Thank you very much for your contribution.

maybe you can wrap a method around

`t(1,1) + t(2,1) + t(3,1) + t(4,1) + t(5,1) + t(6,1) = n`

`testmethod(t,array(1,2,3,4,5,6),array(1,1,1,1,1,1),n)`

Yes, that’s called “short-circuiting” and is common to a lot of languages.

`False And SomeFunction()` will never reach `SomeFunction()`.

`True Or SomeFunction()` won’t either.

The `Select Case` structure I suggested works the same way.

2 Likes