Forum > Beginners

ways to avoid begin-end’s?

(1/10) > >>

Weiss:
I am thinking, there are quite often times when there are only two statements between begin-end encapsulation. Wouldn’t it be neat if I could say something like
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if x=1 then x:=2 AND DoSomething(x)
rather than currently

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if x=1 then     begin       x:=2;       doSomething(x)     end; 
or maybe there is some other neat ways of combining statements

MarkMLl:
If there's a single statement then you can omit the begin-end. If there isn't then you can't, and trying to change things will make the language into something that isn't Pascal: isn't going to happen.

Now if an assignment also had a value, which is the situation in C and also in Pascal's predecessor ALGOL-60, then you could do something like


--- Code: ---if x=1 then
  (x:=2 = 2) AND DoSomething(x);

--- End code ---

However (a) that is at least as cumbersome as having an explicit begin-end and (b) that kind of chained assignment becomes incredibly problematic when a language supports any form of automatic casting (e.g. an expression which probably returns an 8-bit value is assigned to a 32-bit variable).

Wirth's newer languages such as Modula-2 required the end in all cases:


--- Code: ---IF x=1 THEN
  x:=2;
  DoSomething(x)
END;

--- End code ---

In that case it would be possible to argue for "syntactic sugar" e.g.


--- Code: ---IF x=1 THEN:
  x:=2;

--- End code ---

But I am really not convinced that that would be a good thing.

MarkMLl

marcov:
No!

cdbc:
Hi
The pascal language entails 'begin .. end', if it doesn't, it's not pascal.
...And I can imagine a debugging nightmare with 'Italian cuisine'-code like this:
--- Quote ---if x=1 then x:=2 AND DoSomething(x)
--- End quote ---
Not to mention the confusion regarding bit/logical operators...
Nah, I like my begin-ends, they make it easy(ier) to spot mistakes  :D
Right..., /now/ you can call me a 'conservative old fart'  :P
Regards Benny

Weiss:
ok. Point taken. I agree, I was wrong. In retrospect, it was probably begin-end’s that made me like Pascal in first place.

Navigation

[0] Message Index

[#] Next page

Go to full version