[fpc-pascal] basic question on begin, end;

Ralf Quint freedos.la at gmail.com
Wed Sep 23 17:28:20 CEST 2020


On 9/22/2020 5:46 AM, dano none via fpc-pascal 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

> 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



More information about the fpc-pascal mailing list