user1469630
user1469630

Reputation: 301

combine two or more conditions in one if statement

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

Upvotes: 8

Views: 65629

Answers (5)

Pieter B
Pieter B

Reputation: 1967

It's very important to remember that expressions are evaluated left to right.

In this example:

if False and SomeFunction() then

SomeFunction() will not be called. If you turn them around:

if SomeFunction() and False then

SomeFunction() will be called.

Upvotes: 6

user763539
user763539

Reputation: 3699

When using if not then:

if not ( (edit1.Text = '2') or (edit2.Text = '3') ) then ...

Upvotes: 1

RRUZ
RRUZ

Reputation: 136391

You must use the And and Or operators to combine conditions in a if sentence

 if (Edit1.Text<>'') and (Edit2.Text<>'') and (Edit3.Text<>'') then

Upvotes: 8

Hendra
Hendra

Reputation: 755

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.

Upvotes: 19

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

Of course. You can do something like:

if (A > 7) and (B < 13) or (C in [2, 4, 7]) then

Or for the Edit controls:

if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') then

or, if that is what you want:

if (Edit1.Text = '') or (Edit2.Text = '') or (Edit3.Text = '') then

etc.etc.

It might be beneficial to actually read a book about Delphi, for instance the Delphi Language Guide, which comes with each version of Delphi (in the help, in the References part) or can be found online.

For the question: in general, you can combine different conditions using and, or and not. To avoid problems with operator precedence, you should usually put each condition in parentheses, as I did above.

Upvotes: 11

Related Questions