General view of the conditional operator:
IF Boolean Expression THEN ... ELSE ...
A logical expression is any logical expression. Boolean expression - an expression can be evaluated as TRUE or FALSE.
A Boolean expression can be constructed using comparison operators and Boolean operators.
Comparison Operators:
= equals <> not equals > greater than >= greater than or equals < less than <= less than or equals
Set comparison operators:
= equals <= returns true, if set1 is a subset of set2 >= returns true, if set1 is a superset of set2 in returns true, if an element is in the set
Boolean operators:
AND logical and OR logical or NOT logical not XOR logical exclusive disjucntion
Examples:
IF A = 10 THEN ... IF A >= B THEN ... IF C or D THEN ... (Note: C and D have to be logical, ie TRUE or FALSE) IF NOT E THEN ... (Note: E has to be logical, ie TRUE or FALSE)
C, D and E can be replaced with any logical expression, for example:
IF (edit1.text = '') OR ( ISEMPTY( edit2.text ) ) THEN ... IF NOT checkbox1.checked THEN ...
Note that a logical expression can be constructed from simpler logical expressions using Boolean operators, for example:
IF ( A = 10 ) AND ( A >= B ) THEN ... IF NOT ( ( A = 10 ) AND ( A >= B ) ) THEN ...
A common mistake when writing a logical expression does not pay attention to the priority of the operator (which the operator first evaluated). Boolean operators have higher priority than comparison operators, for example:
IF A = 10 OR A >= B THEN ...
The above is incorrect because Delphi is trying to evaluate
10 OR A
first instead
A = 10
. If A itself is not a logical expression, an error occurs.
The solution is done using parentheses, so the above IF ... THEN ... should be written as:
IF (A = 10) OR (A >= B) THEN ...
To test 3 controls, the conditional statement becomes:
IF ( Edit1.text <> '' ) AND ( Edit2.text <> '' ) AND ( Edit3.text <> '' ) THEN ...
Note. Slightly off topic, but with its help. The free TJvValidators, TJvValidationSummary, and TJvErrorIndicator components from the Jedi JVCL project provide a good validation mechanism.