combine two or more conditions in one if statement
Asked Answered
J

5

8

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

Jungle answered 26/6, 2012 at 22:46 Comment(2)
Every programming language I know of, and that's over 200, has some form of boolean logic, involving the operations AND, OR, and NOT. If you haven't seen those yet, you really need to read a book. In fact if you did it in C# why not just try it in delphi? THe only difference is that in Delphi parenthesis are not always required with an IF statement. But when making multiple equality comparisons, you should write if (a = b) or (c = d) for example.Wedding
Additional. you need to consider also that there are some condition need to be group in a parenthesis like if ((a=x1) and (b=y1)) or ((a=x2) and (a=y2)) then do something.Khaki
H
19

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.

Hermosa answered 27/6, 2012 at 5:22 Comment(4)
+1 for the explanation, but please don't use all caps for keywords. That is so 1950s.Breccia
I added <> to your list of operators.Philoctetes
in should be there too, but I don't know how to summarize it in less than 3 words...Philoctetes
I added XOR to Boolean operatorsAbana
P
11

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.

Pretext answered 26/6, 2012 at 22:50 Comment(2)
They can go into a very complex statement with parentheses too, for example, if ((A > 7) or ((B = 'abc') and not (C = 'def')) or (D <> 8)) and (R > 8) then ... (Speaking of which <> is just like != in other languages like C#)Philoctetes
Personally I always use ()s when logical operations are involved. It's safer than trying to keep the rules straight between multiple languages. The compiler will produce the same code anyway, there's no harm in using them when they aren't needed.Lir
D
8

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
Danaedanaher answered 26/6, 2012 at 22:51 Comment(0)
S
6

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.

Swats answered 27/6, 2012 at 9:12 Comment(3)
This is an important answer, all the other answers are very obvious.Vehemence
This depends on Boolean short-circuit evaluation {$B}/{$BOOLEVAL} by default it is set to OFF. but if it is set to ON the SomeFunction will be evaluated in the first example.Mccartan
@Mccartan you're right. But if anytime a developer would set this on, I would hunt him down and question his sanity.Swats
M
1

When using if not then:

if not ( (edit1.Text = '2') or (edit2.Text = '3') ) then ...
Matronymic answered 21/11, 2016 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.