Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 432
user1469630 Avatar asked Jun 26 '12 22:06

user1469630


People also ask

How do you combine two conditions in if?

How to use IF function with multiple conditions. In essence, there are two types of the IF formula with multiple criteria based on the AND / OR logic. Consequently, in the logical test of your IF formula, you should use one of these functions: AND function - returns TRUE if all the conditions are met; FALSE otherwise.

Can IF statement have 2 conditions in Excel?

The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

Can you combine if and/or functions in Excel?

IF Function is one of the most powerful functions in excel. And, the best part is, that you can combine other functions with IF to increase its power. Combining IF and OR functions is one of the most useful formula combinations in excel.


3 Answers

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.

like image 161
Hendra Avatar answered Sep 28 '22 10:09

Hendra


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.

like image 44
Rudy Velthuis Avatar answered Sep 28 '22 08:09

Rudy Velthuis


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
like image 34
RRUZ Avatar answered Sep 28 '22 09:09

RRUZ