Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

TO statement within an IF condition?


bKlippert May 15, 2013 06:03 PM

What I've got is this:

If StatusLED(1) = True OR StatusLED(2) = True OR StatusLED(3) = True OR StatusLED(n) = True Then
Shutdown = True
Else
Shutdown = False
EndIf


I want to make this line more concise so I tried:

If StatusLED(1 To n) = True Then
Shutdown = True
Else
Shutdown = False
EndIf


But this didn't work. Any suggestions???


Sam May 16, 2013 04:52 AM

*removed content, see below *

* Last updated by: Sam on 5/16/2013 @ 10:53 AM *


jtrauntvein May 16, 2013 03:46 PM

In CRBasic true and false are predefined constants that have specific values of -1 and 0 respectively. A comparison of a value such as "x = true" is only going to give a true result if the value of x is exactly equal to -1. A call to avgspa(statusled) is only going to return -1 if all of the elements in the statusled array are equal to -1.

In my opinion, the true and false constants are more useful for initialising a variable than they are for comparison. As a general rule, it is preferable to have something like:

if bool_value then
' true stuff
endif

then it is to use the following:

if bool_value = true then
' true stuff
endif

The former will execute the conditional statements if the bool_value predicate is anything other than zero while the latter will only execute if bool_value is exactly equal to -1.


Sam May 16, 2013 04:43 PM

Now that I'm at a proper computer ...

=====================================================
=====================================================

A recap of some methods for evaluating if one or more values in an array are TRUE. There are many other ways not listed here.

With:

Public Bool(3) as Boolean
Public DoIt as Boolean
Public A, B(2)
Dim I

-------------------
If Bool(1) OR Bool(2) OR Bool(3) Then
DoIt = TRUE
Else
DoIt = FALSE
Endif

-------------------

DoIt = Bool(1) OR Bool(2) OR Bool(3)

-------------------

DoIt = FALSE
For I = 1 to 3
If Bool(I) Then DoIt = TRUE
Next I

-------------------

DoIt = FALSE
For I = 1 to 3
DoIt = DoIt OR Bool(I)
Next I

------------------

AvgSpa (A,3,Bool)
If A Then 'non zero
DoIt = TRUE
Else
DoIt = FALSE
EndIf

------------------

MinSpa (B,3,Bool)
'B(1) will be -1 if one of the values is TRUE
DoIt = B(1)

=====================================================
=====================================================

Here are some methods for evaluating if ALL the values are TRUE. There are many other ways not listed here.

With:

Public Bool(3) as Boolean
Public DoIt as Boolean
Public A, B(2)
Dim I


-------------------
If Bool(1) AND Bool(2) AND Bool(3) Then
DoIt = TRUE
Else
DoIt = FALSE
Endif

-------------------

DoIt = Bool(1) AND Bool(2) AND Bool(3)

-------------------

DoIt = TRUE
For I = 1 to 3
DoIt = DoIt AND Bool(I)
Next I

------------------

AvgSpa (A,3,Bool)
DoIt = A 'all 3 will need to be TRUE for DoIt to be TRUE

* Last updated by: Sam on 5/16/2013 @ 10:46 AM *

Log in or register to post/reply in the forum.