use of **and** operator within an **if statement**

gerard picture gerard · Mar 30, 2011 · Viewed 29.8k times · Source

I was directed to this website by a friend.

I am trying to use and in Delphi, but I seem to be doing something wrong. Is there something you need to put in uses?

I have the following code:

procedure TForm1.Button1Click(Sender: TObject);
var
a,b:string;
begin
a:=edit1.Text;
b:=edit2.Text;

if a=abc and b=def then
showmessage(a+b);

end;

I get an error at the second = sign

Answer

PA. picture PA. · Mar 30, 2011

You have to put some parentheses to change the operator precedence:

  if (a=abc) and (b=def) then

Operator and precedes = so the construction without parenthesis is understood as a=(abc and b=def) which produces the syntax error.