combine two or more conditions in one if statement

delphi, delphi-7

Solution

The general form of conditional statement is :

IF "Logical expression" THEN ...ELSE ...

The "Logical expression" is any boolean expression. A boolean expression is 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, i.e. TRUE or FALSE)
IF NOT E THEN ...  (Note: E has to be logical, i.e. TRUE or FALSE)

C, D and E can be replace with any logical expression, for example:

IF (edit1.text = '') OR ( ISEMPTY( edit2.text ) ) THEN ...
IF NOT checkbox1.checked THEN ...

Note that logical expression can be constructed from simpler logical expressions by using boolean operators, for examples:

IF ( A = 10 ) AND ( A >= B ) THEN ...
IF NOT ( ( A = 10 ) AND ( A >= B ) ) THEN ...

Common mistake in writing logical expression is not paying attention of operator precedence (which operator evaluated first). The boolean operators have higher precedence than comparison operators, for example:

IF A = 10 OR A >= B THEN ... 

The above is wrong because Delphi tries to evaluate

`10 OR A` first, instead of

`A = 10`. If A itself is not a logical expression, then error occurs.

The solution is by using brackets, so the above IF...THEN...should be written as:

`IF (A = 10) OR (A >= B) THEN ...`

For checking 3 edit controls, the conditional statement becomes:

`IF ( Edit1.text <> '' ) AND ( Edit2.text <> '' ) AND ( Edit3.text <> '' ) THEN ...`

Note: Slightly off topic, but related. Free components TJvValidators, TJvValidationSummary and TJvErrorIndicator from Jedi JVCL project provide a nice validation mechanism.

Problem

Can we combine two or more conditions in one if statement? I know that in C# we can combine two or more conditions in an IF statement. Can we do it in Delphi? I have to check whether the user entered a value for the three Edit controls in the form. Thanks for all the help

Original source