Conditions

About Conditions

In GCBASIC (and most other programming languages) a condition is a statement that can be either true or false. Conditions are used when the program must make a decision. A condition is generally given as a value or variable, a relative operator (such as = or >), and another value or variable. Several conditions can be combined to form one condition through the use of logical operators such as AND and OR.

GCBASIC supports these relative operators:

Symbol Meaning

=

Equal

<>

Not Equal

<

Less Than

>

Greater Than

Less than or equal to

>=

Greater than or equal to

In addition, these logical operators can be used to combine several conditions into one:

Name Abbreviation Condition true if

AND

&

both conditions are true

OR

|

at least one condition is true

XOR

#

one condition is true

NOT

!

the condition is not true

NOT is slightly different to the other logical operators, in that it only needs one other condition. Other arithmetic operators may be combined in conditions, to change values before they are compared, for example.

GCBASIC has two built-in conditions - TRUE, which is always true, and FALSE, which is always false. These can be used to create conditional tests and infinite loops.

The condition bit_variable = TRUE is treated as TRUE if the bit is on. Any non-zero value will be treated as equal to a high bit. The condition bit_variable = other_type_of_variable generates a warning. If the byte_variable is set to TRUE and then compared to the bit, it will always be FALSE, because the high bit will be treated as a 1. The warning generated is: "Comparison will fail if %nonbit% is any value other than 0 or 1".

It is also possible to test individual bits in conditions. To do this, specify the bit to test, then 1 or 0 (or ON and OFF respectively). Presently there is no way to combine bit tests with other conditions - NOT, AND, OR, and XOR will not work with them.

Example conditions:

Condition Comments

Temp = 0

Condition is true if Temp = 0

Sensor <> 0

Condition is true if Sensor is not 0

Reading1 > Reading2

True if Reading1 is more than Reading2

Mode = 1 AND Time > 10

True if Mode is 1 and Time is more than 10

Heat > 5 OR Smoke > 2

True if Heat is more than 5 or Smoke is more than 2

Light >= 10 AND (NOT Time > 7)

True if Light is 10 or more, and Time is 7 or less

Temp.0 ON

True if Temp bit 0 is on

Constraints when using Conditional Tests

As GCBASIC is very flexible with the use of variable types, this can cause issues when testing constants and/or functions.

A few simple rules: Always put the function or constant first, or always call the function with the addition of the braces.

The example code below shows the correct method, and an example that compiles but will not work as expected.

    'Example A - works
    'Call the function by adding the braces
    '
    Do
    Loop While HSerReceive() <> 62          ' <<< calling the function explicitly with braces

    'Example B - works
    'Put the constant first - this is the general rule.
    '
    Do
    Loop While 62 <> HSerReceive

Key line: Loop While HSerReceive() <> 62 — the trailing () forces GCBASIC to call HSerReceive and compare its return value against 62 on every loop pass; without the braces (and with the constant on the right), the compiler instead treats the bare function name as a value, which does not call the function at all.

This fails, as the function will not be called:

    'Example C - compiles but does not operate as expected
    Do
    Loop While HSerReceive <> 62



Keep bitwise comparisons simple: a single condition can combine at most two AND/OR operations. A condition combining more than two will fail to compile with "More than two AND/OR statements - reduce complexity." Split the comparison across two or more lines/statements instead.

Similarly, comparing more than one system-generated bit variable together with relational operators or braces in the same condition can silently produce a wrong result, and triggers the warning "Potential invalid BIT comparison error when using complex Bitwise logic." Keep this style of comparison to one bit variable at a time. The warning can be silenced with #DEFINE DISABLE1173, but silencing it does not fix the underlying risk of an incorrect comparison.

See Also:

  • If — using conditions to branch
  • Do — using conditions to loop, as in the examples above
  • HSerReceive — the function used in the worked example
  • Constraints and Error Messages — the full list of compiler constraints and error messages, including the AND/OR and bit-comparison limits above
  • Performance — why nested If can outperform a combined And/Or condition