Log in

View Full Version : end else



zluo
06-30-2012, 05:52 AM
so is something like this,
if FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
something;
end else
anotherthing;
equivalent to this

if FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
something;
end;
if not FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
anotherthing;
end;

CephaXz
06-30-2012, 05:55 AM
Yes, its the same. And...

Only one thing to do.

if FindDTM(asdfasdf, x, y, 1,2,3,4) then
something //No ;
else
anotherthing;



More than one

if FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
something;
thisthing;
thatthing
end else
begin
anotherthing;
andthis;
andthat;
end;

Based Lord
06-30-2012, 06:38 AM
Using the else is definitely cleaner :)

P1ng
06-30-2012, 06:44 AM
Be careful when using it, because if you want to do multiple actions after the else that also requires a begin and an end. Without a begin and an end the else will result in the following line and then it will continue on as below..
if FindThis then
begin
DoThis;
DoThisOtherThing;
end else
SearchForSomethingElse;
WriteLn('Failed To Find This');
TerminateScript;

if it finds this it will do this and do this other thing. Otherwise, if it didn't find this it will search for something else. But the way this is currently written it will always write 'Failed To Find This' and terminate the script.

That code SHOULD be written like this -
if FindThis then
begin
DoThis;
DoThisOtherThing;
end else
begin
SearchForSomethingElse;
WriteLn('Failed To Find This');
TerminateScript;
end;

It might seem really simple when explained, but I fell into that trap a couple of times before I worked out what I was doing wrong just hoping you don't :)

riwu
06-30-2012, 06:54 AM
They are slightly different actually...
in the first case, it will search the DTM ONCE, and if failed, perform the "anotherthing".
In the 2nd case, it will search the DTM once, and if failed, it will search another time, and if failed again, it will do the "anotherthing". (i.e. its going to search twice since u called it twice)

masterBB
06-30-2012, 06:55 AM
so is something like this,
if FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
something;
end else
anotherthing;
equivalent to this

if FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
something;
end;
if not FindDTM(asdfasdf, x, y, 1,2,3,4) then
begin
anotherthing;
end;

No it is not. The second example will check for the DTM twice, the first example will check only once.

Shortest example:

if FindDTM(asdfasdf, x, y, 1,2,3,4) then
something //notice I removed the ; I did that because the else will close the command.
else
anotherthing;

CephaXz
06-30-2012, 08:00 AM
Now that you guys mentioned it, it does really seem different if using it in other circumstances which is different from what I actually thought just now :p