1. Computing

The Traps of The If Then Else Statement in Your Delphi Code

By May 31, 2012

Follow me on:

in Delphi for Beginners :: In Delphi, the if statement is used to test for a condition and then execute sections of code based on whether that condition is True or False. Watchout for If Then Else traps if you are a beginner using nested if statements.

Read the full article to learn about The Traps of The If Then Else Statement in Delphi Code

Related:

Comments
August 10, 2011 at 3:31 am
(1) Andreaa says:

Just use a semicolon and it looks a lot better.

if j >= 0 then
if j = 100 then Caption := ‘Number is 100!’ else ;
else
Caption := ‘Number is NEGATIVE!’;

August 10, 2011 at 4:26 am
(2) BTaylor says:

@Andreaa

You may think it looks better, but it can’t compile as the semicolon refers to the *outermost* IF statement, so the second ELSE clause is now illegal.

August 13, 2011 at 12:40 pm
(3) aly says:

better to use:
if (…) then
if (…) then (…)
else begin end
else (…);

Wathch for the begin-end pair without any contents!

August 13, 2011 at 12:41 pm
(4) aly says:

Another option is to use one begin-end pair without contents:

if (…) then
if (…) then (…)
else begin end
else (…);

August 18, 2011 at 7:02 am
(5) patman says:

It should be this.

if (j >= 0)
then begin
if (j = 100)
then Caption := ‘Number is 100!’;
end
else Caption := ‘Number is NEGATIVE!’;

August 18, 2011 at 7:05 am
(6) patman says:

The above code is not showing my indentation. The second If Then End should line up with the first Begin

May 31, 2012 at 2:37 pm
(7) Cameron says:

Hoping one day all block statements in a language will have a simple end mechanic without having to begin/end or squiggly:

if x > 0 then
blah;
blah;
else
foo;
foo;
end;

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.