PDA

View Full Version : The proper use of IF-ELSE (but especially begin-end; etc.)



Bijo
01-06-2007, 11:29 PM
System: WIN32 (XP)
Compiler: FreePascal
Libraries: -

I know I know: I should know the use of IF THEN ELSE, but you know what it is? I'm used to the IF ELSE and IF ELSE-IFs (etc.) of C++ and Perl and such. For example in C++ I sometimes did stuff like...



if &#40;x <= y&#41;&#123;
do_stuff&#40;&#41;;
&#125;

else if &#40;...&#41;&#123;
do_otherstuff&#40;&#41;;
do_more&#40;&#41;;
&#125;

else if &#40;...&#41;&#123;
do_this&#40;&#41;;
&#125;

else&#123;
do_that&#40;&#41;;
do_this&#40;&#41;;
&#125;



It's so easy, 'cause whenever there's a keyword I just use { } and get it over with, even if there's one statement. I also just put a ; after every statement or expression or whatever, and I can easily use IF and ELSE-IFs (and an opional ELSE).

When I'd try something like that (the IF-ELSE and such) with Pascal, I'm just getting totally confused with something that should be so simple, and I try to keep things even simpler than I could have done, I almost even avoid the IF-THEN-ELSE with BEGIN-ENDs and just rely on many IFs alone sometimes.

Is that piece of code there above equivalent to....


if (x <= y) then begin
do_stuff; {or I could just remove BEGIN-END and ; here?}
end;

else begin
if (...) then begin
do_otherstuff;
do_more;
end;
if (...) then begin
do_this;
end;
else begin
do_that;
do_this;
end;
end;


Arrrgh! These kinds of things are the most annoying syntax critters I encounter(!), but for the rest I mostly love the language :) (though I wouldn't mind a # instead of <> )


(* Why not just make it easier and force a BEGIN-END and force the use of ; every every statement and after every END? *)

Robert Kosek
01-06-2007, 11:56 PM
In pascal begin/end is only required if you wish to enter more than one line of code for a function, like so:if (boolean_val = true) then
Result := 'True'
else
Result := 'False';The line/end before an else never has a semicolon.

For a large block like you posted above, the proper way to write it is:
if (x <y>= 80) then
DoSomething
else if (intvar >= 60) then begin
DoSomethingElse;
CleanUpSomething;
end else if (intvar >= 10) then
DoAJig
else
Whatever;

In the case of a complex block such as I posted in this thread (http://www.pascalgamedevelopment.com/viewtopic.php?t=4484) (and what caused my bizarre errors) it is better to be on the safe side and use begin/ends almost superfluously so as to avoid wacky problems. It depends on how convoluted your block is, but sometimes when you're in doubt ... this is the fastest troubleshooting method you've got. ;)

I hope this helps to clear stuff up.

JSoftware
02-06-2007, 01:10 AM
Your particular C code would be this in pascal:

if x <= y then
do_stuff()
else if ... then
begin
do_otherstuff();
do_more();
end
else if ... then
do_this()
else
begin
do_that();
do_this();
end;

DarknessX
02-06-2007, 01:53 AM
There are times when if.. else can be better than a bunch of if's though... I've had some extremely complex blocks of if-then's...
And basically, it was like this:

begin
...
if p1turn = true then begin
..
if p2turn = true then begin

end else if p3turn = true then begin

end else if p4turn = true then begin

end;
end else if p2turn = true then begin
if p1turn = true then begin

end else if p3turn = true then begin

end else if p4turn = true then begin

end;
end else if p3turn = true then begin
if p1turn = true then begin

end else if p2turn = true then begin

end else if p4turn = true then begin

end;
end else if p4turn = true then begin
if p1turn = true then begin

end else if p2turn = true then begin

end else if p3turn = true then begin

end;
end;


Though thats a much simplified version... All it requires is placing placeholders for the code you need to place so you know what goes where, and it will work fine. It can get very confusing without though :evil:

Bijo
03-06-2007, 03:44 AM
Thanks for all the responses. I think I'm getting it :)



Your particular C code would be this in pascal:

if x <= y then
do_stuff()
else if ... then
begin
do_otherstuff();
do_more();
end
else if ... then
do_this()
else
begin
do_that();
do_this();
end;

So if I understand the whole shebang correctly, my safest bet would be to:

(1) always put a BEGIN-END
(2) always use a ; after every call or expression, etc.
(3) to use ONE ; after the final END to signify the end of the WHOLE thing (that started at the first IF), and I should not use a ; right after any other keyword?

wodzu
03-06-2007, 09:19 AM
From the Delphi help:

A special difficulty arises in connection with nested if statements. The problem arises because some if statements have else clauses while others do not, but the syntax for the two kinds of statement is otherwise the same. In a series of nested conditionals where there are fewer else clauses than if statements, it may not seem clear which else clauses are bound to which ifs. Consider a statement of the form

if expression1 then if expression2 then statement1 else statement2;

There would appear to be two ways to parse this:

if expression1 then [ if expression2 then statement1 else statement2];
if expression1 then [ if expression2 then statement1] else statement2;

The compiler always parses in the first way. That is, in real code, the statement

if ... { expression1 } then
if ... { expression2 } then
... { statement1 }
else
... { statement2 } ;

is equivalent to

if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
else
... { statement2 }
end;

The rule is that nested conditionals are parsed starting from the innermost conditional, with each else bound to the nearest available if on its left. To force the compiler to read our example in the second way, you would have to write it explicitly as

if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
end
else
... { statement2 } ;