Oxygene if statement working wrong

Hi,

I’ve got problem with Oxygene if statement

	var co2 := 500;
	var wilg := 236;

  if ((co2<2500) and (wilg>=5 and wilg<=100 )) then        
	begin
  	writeLn('ok.');
	end
	else 
	begin
	  writeLn('wrong.');
	end;

For me it should write wrong…

If I change it to

  if ((co2<2500) and (wilg>=5) and (wilg<=100 )) then        

it’s ok, but I don’t know what could go wrong in my whole code.
ConsoleApplication1.zip (29.2 KB)

Version 11.0.0.2705

Unfortunately, in Pascal, the “and” and “or” operators have precedence over ≥ and ≤, (yeah, I hate that, too, but we can’t mess with 50+ years of history ;), so the code you wrote evaluares as:

((500<2500) and (236 >= (5 and 236) <= 100 ))

true and (236 >= 4  <= 100)

tru and false

false

because “236 >= 4 <= 100” is an (albeit awkward Double Boolean Comparisons, and false.

Rule of thumb when and'ing or or'ing expressions, always our parenthesis around both operands to make the intent clear.

I recommend that even for C# & Co, which do not have this exact issue ("wilg >= 5 && wilg <= 100" would be fine), because, who can really remember what “a || b && c” does? :wink:

2 Likes

And I’m smarter again :smiley:

2 Likes

My post from Sept 21 touched on this very topic as well :nerd_face:

2 Likes