"For" loop for boolean variables?

I have two Boolean variables, namely "a" and "b".

In general, there are 4 states for variables (TT, TF, FT, FF).

I want to see how I can automatically change variables from true to false and code for each state.

Something like a "For" loop, but for boolean variables.

For a = True to False

    For b = True to False

    Next
Next
+4
source share
4 answers

You simply represent boolean values ​​as 1 or 0, for example:

Dim b1 As Byte
Dim b2 As Byte
Dim a As Boolean
Dim b As Boolean

For b1 = 0 To 1
    For b2 = 0 To 1
        a = CBool(b1)
        b = CBool(b2)
    Next
Next
+2
source

You need only one loop:

Dim a As Boolean
Dim b As Boolean
Dim i As Integer

For i = 0 To 3
    a = i And 1
    b = i And 2
    ...
Next
+1
source

This is the best solution I have found for VBA that maintains consistency with your source code:

Dim a As Boolean 'defining your Boolean Variables
Dim b As Boolean

For i = 1 To 0 Step -1: a = i 'this take 'a' from TRUE to FALSE
    For j = 1 To 0 Step -1: b = j 'this take 'b' from TRUE to FALSE

    Next j
Next i
+1
source

It:

Do
    a = CBool(Not a)
    b = CBool(IIf(a, Not b, b))
Loop Until Not a And Not b

Will produce all 4 results and then exit

0
source

Source: https://habr.com/ru/post/1617610/


All Articles