Combine two or more conditions in one if statement

Is it possible to 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 this in Delphi?

I need to check if the user is entering a value for the three Edit controls on the form. thanks for the help

+6
source share
5 answers

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.

+16
source

Of course. You can do something like:

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

Or for Edit controls:

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

or, if this is what you want:

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

etc.etc.

It may be really useful to read a book about Delphi, for example, the Delphi Language Guide, which comes with each version of Delphi (in the help, in the "Links" section) or can be found on the Internet .

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

+10
source

You must use the And and Or operators to combine the conditions into if clause

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

It is very important to remember that expressions are evaluated from left to right.

in this example

 if False and SomeFunction() then 

SomeFunction will not be evaluated. If you roll them around

 if SomeFunction() and False then 

The SomeFunction () function is defined.

+5
source

However, if you use "if not", then:

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

0
source

Source: https://habr.com/ru/post/919077/


All Articles