Board index » delphi » if..then..else and semicolons

if..then..else and semicolons

Is the "no semicolon before 'else'" rule confusing?

Consider:

if a = b then
        if c = d then
                x := y
else
        n := m;

Written with the indentation I've used, it's clear that I mean the else to
refer to the first if (the 'a = b' one). However, the compiler - correctly -
interprets the else as belonging to the 'c = d' if. Putting a semicolon
after 'x = y' merely causes the compiler to complain that semicolons are not
allowed before an "else" clause. Admittedly recoding the first two lines as
"if a = b and c = d" is a work around, but is there a syntactically correct
way of expressing which "if" the "else" belongs to?
-------------
Keith Edmunds
Reading,   UK
-------------

 

Re:if..then..else and semicolons


Quote
k...@itworks.demon.co.uk (Keith Edmunds) wrote:
>Is the "no semicolon before 'else'" rule confusing?

>Consider:

>if a = b then
>    if c = d then
>            x := y
>else
>    n := m;

>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?
>-------------
>Keith Edmunds
>Reading,   UK
>-------------

The only way is to write something like:

if a = b then
        if c = d then
        begin
                x := y;
        end
else
        n := m;

There is no point to explain more, because it's directly concerned with the
grammar of the language. What you have to understand is that it's impossible to
do it without the begin..end statement.

        Alan.

--
Alan GARNY                              http://www.physiol.ox.ac.uk/~gry
University Laboratory of Physiology       Job Phone: +44 (0)1865 272-501
Parks Road                                 Job  Fax: +44 (0)1865 272-554
Oxford, OX1-3PT, UK                      Home Phone: +44 (0)1865 370-240

Re:if..then..else and semicolons


Quote
k...@itworks.demon.co.uk (Keith Edmunds) wrote:
>Is the "no semicolon before 'else'" rule confusing?

>Consider:

>if a = b then
>    if c = d then
>            x := y
>else
>    n := m;

>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?
>-------------
>Keith Edmunds
>Reading,   UK
>-------------

The only way is to write something like:

if a = b then
        if c = d then
        begin
                x := y;
        end
else
        n := m;

There is no point to explain more, because it's directly concerned with the
grammar of the language. What you have to understand is that it's impossible to
do it without the begin..end statement.

        Alan.

--
Alan GARNY                              http://www.physiol.ox.ac.uk/~gry
University Laboratory of Physiology       Job Phone: +44 (0)1865 272-501
Parks Road                                 Job  Fax: +44 (0)1865 272-554
Oxford, OX1-3PT, UK                      Home Phone: +44 (0)1865 370-240

Re:if..then..else and semicolons


Hi!

Try this:

if a = b then begin
        if c = d then x := y
end
else n := m;

-Geir Bratlie-
ro...@sn.no

On 12. juli 1996, Keith Edmunds wrote...

Quote
> Is the "no semicolon before 'else'" rule confusing?

> Consider:

> if a = b then
>    if c = d then
>            x := y
> else
>    n := m;

> Written with the indentation I've used, it's clear that I mean the else
to
> refer to the first if (the 'a = b' one). However, the compiler -
correctly -
> interprets the else as belonging to the 'c = d' if. Putting a semicolon
> after 'x = y' merely causes the compiler to complain that semicolons are
not
> allowed before an "else" clause. Admittedly recoding the first two lines
as
> "if a = b and c = d" is a work around, but is there a syntactically
correct
> way of expressing which "if" the "else" belongs to?
> -------------
> Keith Edmunds
> Reading,   UK
> -------------

Re:if..then..else and semicolons


Use a BEGIN-END block:

if a = b then begin
   if c = d then
      x := y
   end
else
   n := m;

Quote
k...@itworks.demon.co.uk (Keith Edmunds) wrote:
>Is the "no semicolon before 'else'" rule confusing?
>Consider:
>if a = b then
>    if c = d then
>            x := y
>else
>    n := m;
>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?
>-------------
>Keith Edmunds
>Reading,   UK
>-------------

Re:if..then..else and semicolons


Quote
Alan GARNY <g...@physiol.ox.ac.uk> wrote:
>k...@itworks.demon.co.uk (Keith Edmunds) wrote:
>>Is the "no semicolon before 'else'" rule confusing?

>>Consider:

>>if a = b then
>>        if c = d then
>>                x := y
>>else
>>        n := m;

>>Written with the indentation I've used, it's clear that I mean the else to
>>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>>after 'x = y' merely causes the compiler to complain that semicolons are not
>>allowed before an "else" clause. Admittedly recoding the first two lines as
>>"if a = b and c = d" is a work around, but is there a syntactically correct
>>way of expressing which "if" the "else" belongs to?
>>-------------
>>Keith Edmunds
>>Reading,   UK
>>-------------

>The only way is to write something like:

>if a = b then
>    if c = d then
>    begin
>            x := y;
>    end
>else
>    n := m;

>There is no point to explain more, because it's directly concerned with the
>grammar of the language. What you have to understand is that it's impossible to
>do it without the begin..end statement.

My big mistake!!! Of course it's not what I wrote, but rather what Geir said:
if a = b then
        begin
        if c = d then
                x := y;
        end
else
        n := m;

--
Alan GARNY                              http://www.physiol.ox.ac.uk/~gry
University Laboratory of Physiology       Job Phone: +44 (0)1865 272-501
Parks Road                                 Job  Fax: +44 (0)1865 272-554
Oxford, OX1-3PT, UK                      Home Phone: +44 (0)1865 370-240

Re:if..then..else and semicolons


Quote
Keith Edmunds wrote:

> Is the "no semicolon before 'else'" rule confusing?

> Consider:

> if a = b then
>         if c = d then
>                 x := y
> else
>         n := m;

> Written with the indentation I've used, it's clear that I mean the else to
> refer to the first if (the 'a = b' one). However, the compiler - correctly -

[...]

if a = b then
        if c = d then
                x := y
        else
                { nop }
else
        n := m;

This works.

Magnus

Re:if..then..else and semicolons


A bit of history: this was an embarrassing gap in the original Algol-60
standard many years ago. The authors didn't notice the ambiguity of nested
if/then/else in the BNF construction.

OP resolves this ambiguity by simply declaring that else clauses associate
with the innermost if.  Offhand, I can't see how the no-semicolon rule helps
anything, unless it makes it easier to write the parser.

=steve

In article <01bb6fe3.aa893a40$29188fc2@geir>, ro...@sn.no says...

Quote

>Hi!

>Try this:

>if a = b then begin
>        if c = d then x := y
>end
>else n := m;

>-Geir Bratlie-
>ro...@sn.no

>On 12. juli 1996, Keith Edmunds wrote...
>> Is the "no semicolon before 'else'" rule confusing?

>> Consider:

>> if a = b then
>>       if c = d then
>>               x := y
>> else
>>       n := m;

>> Written with the indentation I've used, it's clear that I mean the else
>to
>> refer to the first if (the 'a = b' one). However, the compiler -
>correctly -
>> interprets the else as belonging to the 'c = d' if. Putting a semicolon
>> after 'x = y' merely causes the compiler to complain that semicolons are
>not
>> allowed before an "else" clause. Admittedly recoding the first two lines
>as
>> "if a = b and c = d" is a work around, but is there a syntactically
>correct
>> way of expressing which "if" the "else" belongs to?
>> -------------
>> Keith Edmunds
>> Reading,   UK
>> -------------

Re:if..then..else and semicolons


Quote
>>> Consider:

>>> if a = b then
>>>       if c = d then
>>>               x := y
>>> else
>>>       n := m;

if a = b then
  if c = d then
    x := y
  else  {if c=d}
else {if a=b}
n := m

...simple...

Re:if..then..else and semicolons


?|!

Quote
>if a = b then
>             if c = d then
>                x := y
>else
>        n := m;

>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?

if a=b then
 begin
  if c-d then x:=y;
 end
else n:=m;

                                           a.
---
Bu...@stk.ksu.ras.ru
{Excuse me please for my bad russian, my native language is DELPHI 32-bit}

Re:if..then..else and semicolons


In article <31e57f12.18148...@news.demon.co.uk>,
   k...@itworks.demon.co.uk (Keith Edmunds) wrote:

Quote
>Is the "no semicolon before 'else'" rule confusing?

>Consider:

>if a = b then
>    if c = d then
>            x := y
>else
>    n := m;

>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?

Formatting and white space are for your understanding of the code not the
compiler's. It can take a rather amorphous mess and compile it like it's
written.

I usually sprinkle in a few extra Begin..End pairs in places where the syntax
can be a little ambigiuos(sp?). Nested If..Then..Else statements can be very
confusing without some sort of formatting discipline.

if a = b then
Begin
  if c = d then
    x := y
  else
    n := m;
End;

-----------------------------------------------
Mike Chapin
Powder River
mcha...@vcn.com
http://www.vcn.com/server/netizens/mchapin/first.html
Gillette, WY

Not the end of the earth but you can see it from
there.
-----------------------------------------------

Re:if..then..else and semicolons


Quote
Bulat R. Sirazetdinov wrote:

> ?|!

> >if a = b then
> >             if c = d then
> >                x := y
> >else
> >        n := m;

> >Written with the indentation I've used, it's clear that I mean the else to
> >refer to the first if (the 'a = b' one). However, the compiler - correctly -
> >interprets the else as belonging to the 'c = d' if. Putting a semicolon
> >after 'x = y' merely causes the compiler to complain that semicolons are not
> >allowed before an "else" clause. Admittedly recoding the first two lines as
> >"if a = b and c = d" is a work around, but is there a syntactically correct
> >way of expressing which "if" the "else" belongs to?

> if a=b then
>  begin
>   if c-d then x:=y;
>  end
> else n:=m;

>                                            a.
> ---
> Bu...@stk.ksu.ras.ru
> {Excuse me please for my bad russian, my native language is DELPHI 32-bit}

It just simply amazes me that someone who knows how to use a IF THEN
ELSE process does not know how to use BEGIN and END.. this is basic
programming in many of the languages. this type of problem belongs in a
group called comp.never.wrote.a.program.before.in.my.life.

Re:if..then..else and semicolons


On Fri, 12 Jul 1996 08:43:28 GMT, k...@itworks.demon.co.uk (Keith

Quote
Edmunds) wrote:
>Is the "no semicolon before 'else'" rule confusing?

>Consider:

>if a = b then
>    if c = d then
>            x := y
>else
>    n := m;

>Written with the indentation I've used, it's clear that I mean the else to
>refer to the first if (the 'a = b' one). However, the compiler - correctly -
>interprets the else as belonging to the 'c = d' if. Putting a semicolon
>after 'x = y' merely causes the compiler to complain that semicolons are not
>allowed before an "else" clause. Admittedly recoding the first two lines as
>"if a = b and c = d" is a work around, but is there a syntactically correct
>way of expressing which "if" the "else" belongs to?

Yes...

if a=b then
begin
         if c=d then
                         x:=y;
end
else
        n:=m;

I hope it will help you.
bye.

GRillo

email: osc...@lander.es
       p...@ctv.es

Re:if..then..else and semicolons


Quote
chris houghten <choug...@mcgh.org> wrote:
>It just simply amazes me that someone who knows how to use a IF THEN
>ELSE process does not know how to use BEGIN and END.. this is basic
>programming in many of the languages. this type of problem belongs in a
>group called comp.never.wrote.a.program.before.in.my.life.

It appears your intolerance is matched by your ignorance. I think
Pascal and Algol are the only major languages to use begin and end
statements. In particular begin/end do not exist in VB which is where
a lot of new Delphi programmers are coming from.

So give the guy a break and post something helpful.

--
Bob Cousins, Software Engineer.
http://www.demon.co.uk/sirius-{*word*104}netics/

Go to page: [1] [2]

Other Threads