Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-09

basic question on begin, end;

12 messages · 11 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

dano none

I have a basic shuffle routine. I put being, end statements in the outer loop for clarification. It ended up producing random results.
Commenting out the begin, end combination allows the code to run as expected. My code is below, can someone tell me if the begin, end combination should actually make a difference, or should they be more ornamental in function? Thanks!

   {Let's Shuffle Col1  - routine from: https://www.theproblemsite.com/reference/science/technology/programming/randomizing-an-array }
   my_base := 0;
   for i:= 0 to 4 do  { Column we are currently working on }
    {  begin }
         for j:= 0 to 90 do { 20 swaps on the working column }
            begin
               index1 := RandomRange(my_base,my_base+14);   { a random # = length of the array }
               index2 := RandomRange(my_base,my_base+14);
               writeln('index1 ', index1,' ', 'index2 ', index2);
               while (index1 = index2) do
               begin
                  index2 := RandomRange(my_base,my_base+14); { avoid swaping on the same square }
                  writeln('index2 trap ',index2);
               end;
               my_temp := shuffle_array[i,index1];     { store position to get a random value }
               shuffle_array[i,index1] := shuffle_array[i,index2];
               shuffle_array[i,index2] := my_temp;
               writeln(shuffle_array[i,index1],'  ',shuffle_array[i,index2]);
               { ok... Index isn't a valid row.. }
            end;
            my_base := my_base + 15;
            writeln('current base ',my_base:2);
            ch := ReadKey;
    {  end;  { end i = column indexer }}

Reply by email

Bart
to dano none

On Wed, Sep 23, 2020 at 4:25 PM dano none via fpc-pascal wrote:

for i := start to stop do
  for j := start2 to stop 2 do CompoundStatement
if functionally the same as
for i := start to stop do
begin
  for j := start2 to stop 2 do CompoundStatement
end;

A Compound Statement is a statement that consists of more than one statement and as such must be enclosed in a begin/end pair.

Bart

Bart

Reply by email

Tomas Hajny
to dano none: I have a basic shuffle routine. I put being, end statements in the outer loop for…

On 2020-09-22 14:46, dano none via fpc-pascal wrote:

Hello,

Show quoted text 8 lines
I have a basic shuffle routine. I put being, end statements in the outer loop for clarification. It ended up producing random results. Commenting out the begin, end combination allows the code to run as expected. My code is below, can someone tell me if the begin, end combination should actually make a difference, or should they be more ornamental in function?

.
.

Well, your question suggests that your understanding of the Pascal language (and creating algorithms in general) shall be improved. The begin / end pair creates a block which then becomes part of the program structure. If there is a language construct (e.g. a condition, a loop or with) which is applied to a statement within the source following this construct, using begin / end causes the construct to apply to the whole block between begin / end rather than a single statement, thus it indeed makes a big difference in many (most) cases.

Tomas

Reply by email

Christo Crause
to dano none: I have a basic shuffle routine. I put being, end statements in the outer loop for…

On Wed, Sep 23, 2020 at 4:25 PM dano none via fpc-pascal <

Show quoted text 8 lines
wrote: end; my_base := my_base + 15; writeln('current base ',my_base:2); ch := ReadKey; { end; { end i = column indexer }}

A different way to explain Bart's comment is that the inner loop over j is a compound statement (between the inner loop begin/end block) which is followed by more statements (the statements after the first end quoted above). If you comment out the outer begin/end block these statements are not included in the execution of the outer loop, so for example my_base is not incremented as part of the outer loop if the begin/end pair is commented out as indicated in your example.

Reply by email

Ralf Quint
to dano none: I have a basic shuffle routine. I put being, end statements in the outer loop for…
dano none wrote
I have a basic shuffle routine. I put being, end statements in the outer loop for clarification. It ended up producing random results. Commenting out the begin, end combination allows the code to run as expected. My code is below, can someone tell me if the begin, end combination should actually make a difference, or should they be more ornamental in function?

Well, in that code snippet, you have commented out one begin statement and two end statements in a nested comment. As Bart already mentioned, having the first for loop's statements enclosed with a begin/end does not make any functional difference, unless you have somehow unbalanced pairs of begin/end (outside of that code snippet).

Most of the time, this will result in the code not even compiling or throwing some warnings but there can be cases where it accidentally changes the flow of the code. Similar like moving code blocks around in Python with a one-off indentation and all the sudden the flow of that code changes, without complaining...

What editor do you use to write your code? If you are using the Lazarus IDE for example, it show show you/highlight the matching pairs of begin/end and could help to find some stray begins or ends in your code...

Ralf

Show quoted text 27 lines
Thanks! {Let's Shuffle Col1 - routine from: https://www.theproblemsite.com/reference/science/technology/programming/randomizing-an-array } my_base := 0; for i:= 0 to 4 do { Column we are currently working on } { begin } for j:= 0 to 90 do { 20 swaps on the working column } begin index1 := RandomRange(my_base,my_base+14); { a random # = length of the array } index2 := RandomRange(my_base,my_base+14); writeln('index1 ', index1,' ', 'index2 ', index2); while (index1 = index2) do begin index2 := RandomRange(my_base,my_base+14); { avoid swaping on the same square } writeln('index2 trap ',index2); end; my_temp := shuffle_array[i,index1]; { store position to get a random value } shuffle_array[i,index1] := shuffle_array[i,index2]; shuffle_array[i,index2] := my_temp; writeln(shuffle_array[i,index1],' ',shuffle_array[i,index2]); { ok... Index isn't a valid row.. } end; my_base := my_base + 15; writeln('current base ',my_base:2); ch := ReadKey; { end; { end i = column indexer }}
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email

Bo Berglund
to Ralf Quint: Well, in that code snippet, you have commented out one begin statement and two end…
Ralf Quint wrote
Similar like moving code blocks around in Python with a one-off indentation and all the sudden the flow of that code changes, without complaining...

This use of whitespace as block delimiter is why I never could cope with Python when I was working (now retired).

Begin-end are really big helpers to correctly structure loops etc and I use them all the time to make things clearer.

Also putting begin right below if, for, while etc makes it much easier in Lazarus to see what happens in multi-level code when one is selecting begin or end since they match vertically. So I never do:

if something then begin
  some multi-line code here
end;

Instead:

if something then
begin
  some multi-line code here
end;

And of course as has already been pointed out the original question's example code fundamentally changes execution with or without the begin-end pair!

Bo Berglund Developer in Sweden

Reply by email

Markus Greim
to Bo Berglund

Bo,

"Programs must not be regarded as code for computers, but as literature for humans"

Niklaus Wirth, the inventor of PASCAL.
Last sentence on the last slide of his presentation. given at a conference to honor of his 80th birthday at the ETH Zürich in 2014.

(i had the honor to participate)

Kind Regards

Markus

Earlier message 72 lines
--- original message --- On September 24, 2020, 10:04 AM GMT+2 wrote: On Wed, 23 Sep 2020 08:28:20 -0700, Ralf Quint via fpc-pascal wrote: >> Similar like moving code blocks around in >> Python with a one-off indentation and all the sudden the flow of that >> code changes, without complaining... > This use of whitespace as block delimiter is why I never could cope > with Python when I was working (now retired). > Begin-end are really big helpers to correctly structure loops etc and > I use them all the time to make things clearer. > Also putting begin right below if, for, while etc makes it much easier > in Lazarus to see what happens in multi-level code when one is > selecting begin or end since they match vertically. > So I never do: > if something then begin > some multi-line code here > end; > Instead: > if something then > begin > some multi-line code here > end; > And of course as has already been pointed out the original question's > example code fundamentally changes execution with or without the > begin-end pair! > -- > Bo Berglund > Developer in Sweden > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal --- end of original message ---

Reply by email

James Richters
to Markus Greim: "Programs must not be regarded as code for computers, but as literature for humans"…

I think that’s a GREAT quote from Niklaus Wirth, and I agree with that whole heartedly… programs should be readable by humans… otherwise do all your programming in assembly language… the whole POINT of a hi level language is to make it readable by humans… not computers. I can’t stand trying to muddle through things like C++, it’s just to confusing.. trying to follow all those curly braces and figure out what this line of code is going to do.. it’s just a mess. yes I can manage, but I defiantly prefer the clarity of PASCAL… so I also name my variable very clearly instead of using cryptic shorthand.. who cares how verbose my variable names are… it doesn’t make the program any less efficient.. but very clear function and variable names sure make it easier to remember what you were thinking when you have to go back and modify code you originally wrote 30 years ago.

I don’t like the begin on the same line after the then, and I prefer indents.. it really does help a lot to make sure the correct things are included..

if something then
   begin
      some code here;
      some more code;
   end
else
   begin
      some other code here;
      some more other code;
   end;

with complex code, the indents are a big help, but I also don’t like python that is strictly defined by the indents.. it becomes too tedious. I admit my code gets a little sloppy with the indents, so after a while it sometimes looks like:

if something then
   begin
some code here;
      some more code;
end;

at least it compiles correctly because the begin and end; are defining things… once I get a function or procedure working the way I want it, I will then take the time to go back and fix my indents. Its one of the reasons I don’t care for python.. when I’m busy trying to get some basic logic figured out, I don’t’ have time to count the indents.. it interferes with my thought process. I know I’m going to fix it later.. so I defiantly prefer my program not to break if I get a little off on my indents trying to get something done… Yes PASCAL is my favorite language, and I only even look at others if I need to.

James

Earlier message 69 lines
From: fpc-pascal On Behalf Of Markus Greim via fpc-pascal Sent: Thursday, September 24, 2020 4:12 AM To: ; Cc: Markus Greim Subject: Re: [fpc-pascal] basic question on begin, end; Bo, "Programs must not be regarded as code for computers, but as literature for humans" Niklaus Wirth, the inventor of PASCAL. Last sentence on the last slide of his presentation. given at a conference to honor of his 80th birthday at the ETH Zürich in 2014. (i had the honor to participate) Kind Regards Markus <https://app.frontapp.com/api/1/noauth/companies/schleibinger_geraete_gmbh/s een/msg_88fgbb6/han_9pzu6a/06a948e6.gif> On September 24, 2020, 10:04 AM GMT+2 wrote: On Wed, 23 Sep 2020 08:28:20 -0700, Ralf Quint via fpc-pascal < > wrote: >Similar like moving code blocks around in >Python with a one-off indentation and all the sudden the flow of that >code changes, without complaining... This use of whitespace as block delimiter is why I never could cope with Python when I was working (now retired). Begin-end are really big helpers to correctly structure loops etc and I use them all the time to make things clearer. Also putting begin right below if, for, while etc makes it much easier in Lazarus to see what happens in multi-level code when one is selecting begin or end since they match vertically. So I never do: if something then begin some multi-line code here end; Instead: if something then begin some multi-line code here end; And of course as has already been pointed out the original question's example code fundamentally changes execution with or without the begin-end pair! -- Bo Berglund Developer in Sweden

Reply by email

Bernd Oppolzer
to James Richters
James Richters wrote 32 lines
I think that’s a GREAT quote from Niklaus Wirth, and I agree with that whole heartedly… programs should be readable by humans… otherwise do all your programming in assembly language… the whole POINT of a hi level language is to make it readable by humans… not computers.I can’t stand trying to muddle through things like C++,it’s just to confusing.. trying to follow all those curly braces and figure out what this line of code is going to do.. it’s just a mess. yes I can manage, but I defiantly prefer the clarity of PASCAL… so I also name my variable very clearly instead of using cryptic shorthand.. who cares how verbose my variable names are… it doesn’t make the program any less efficient.. but very clear function and variable names sure make it easier to remember what you were thinking when you have to go back and modify code you originally wrote 30 years ago. ... I admit my code gets a little sloppy with the indents, so after a while it sometimes looks like: if something then begin some code here; some more code; end; at least it compiles correctly because the begin and end; are defining things… once I get a function or procedure working the way I want it, I will then take the time to go back and fix my indents.

Same for me, except that I totally automated the process of fixing the indents;
when inserting new code into a program, I don't care much about indentation;
instead after compiling successfully, I run a (self-written) Pascal program which fixes the
indentation etc.; it also draws boxes around certain comments and inserts blank lines,
when necessary. The parts of the program, which are already well-formed, remain unchanged.

The program, which fixes the indentation, is in many cases scheduled automatically after
a successful compiler run.

This is how a program looks after automated indentation: https://github.com/StanfordPascal/Pascal/blob/master/PASCAL1.pas

Kind regards

Bernd

Reply by email

James Richters
to Bernd Oppolzer
This is how a program looks after automated indentation: https://github.com/StanfordPascal/Pascal/blob/master/PASCAL1.pas

Automating the formatting is a great idea, your formatted code is exactly the way I would do it. I especially like the comments for the Begins and Ends so you can see what procedure or function they belong to without scrolling. even though everything is properly indented, I'm sure those comments come in handy.
James

Reply by email