Delphi Error – E2012 Type of expression must be BOOLEAN

Delphi Compiler Error

E2012 Type of expression must be BOOLEAN

Reason for the Error & Solution

This error message is output when an expression serves as a condition and must therefore be of Boolean type. This is the case for the controlling expression of the if, while and repeat statements, and for the expression that controls a conditional breakpoint.

program Produce;
var
  P: Pointer;
begin
  if P then
    Writeln('P <> nil');
end.

Here, a C++ programmer just used a pointer variable as the condition of an if statement.

program Solve;
var
  P: Pointer;
begin
  if P <> nil then
    Writeln('P <> nil');
end.

In Delphi, you need to be more explicit in this case.