Recent

Author Topic: while loop with code before comparison  (Read 7711 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: while loop with code before comparison
« Reply #30 on: August 12, 2020, 12:18:14 pm »
I think Circular needs some kind of C-like for loop:
Code: C  [Select][+][-]
  1.   for (<initialization>; <condition>; <iteration>) <code-block>;
  2.  
I don't like it a lot mainly because you can use almost any code inside the for declaration.  I know that's not possible in Pascal, but just to illustrate:
Code: C  [Select][+][-]
  1.   for (
  2.     puts ("Hello, I'm inside the loop initialization");
  3.     b = a > some_function () ? (printf ("a = %d", a), TRUE) : FALSE;
  4.     puts ("I'm here but not needed")
  5.   )
  6.     a++;
  7.  
Actually I'm not sure it will compile (iirc printf is void but the "," operator should deal with it), but you see it is quite confusing.  And I've seen code like that.

I'm not sure how such loop would look in Pascal but let me try:
Code: Pascal  [Select][+][-]
  1. WHEN <assignation> IS <condition> DO <code>
  2.  

For example:
Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewLoopType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   WHEN Value := Random (101) IS Value <= 75 DO
  8.     WriteLn (Value, '%')
  9. END.
  10.  

[edit] Had another idea:
Code: Pascal  [Select][+][-]
  1.   WHEN <condition> IN <assignation> DO <code>
  2.  

Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewLoopType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   WHEN Value <=75 IN Value := Random (101) DO
  8.     WriteLn (Value, '%')
  9. END.
« Last Edit: August 12, 2020, 12:22:16 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: while loop with code before comparison
« Reply #31 on: August 12, 2020, 12:30:00 pm »
That would not be readable.
I agree.... Just pointed out the possibility.
Its basically the: assignment and condition in one....


But what you really want is something that:
- Ideally makes it clear what variable is your loop var. Maybe even protects it, like for does
- Has a clear "loop condition" (rather than zero, one or more "break" conditions somewhere buried in the middle of the loop code)
- has an initial value for the loop var
- has an update for the loop var
=> Or in your case initial and update is the same.

Even the  "for(initial, condition, update)"  does not fulfil that


In other words, the conflict you have is IMHO that:
- You need the loop condition in the middle (after getting the random number, but before printing it)
- You want the loop condition to be clearly noticeable, which means it should be part of the loop statement (no infinite loop statement)

Those 2 statements directly contradict each other.
So the problem is logical at first, rather than just missing syntax....
- Init goes before the loop
- update goes into the loop
If both are combined, where should they go?



If you ignore the fact that in your case "init loop var" and "update loop var" is the same. And therefore leads to syntactical duplication (the meaning is not duplicated), then you should end up with an earlier proposal:
Code: Pascal  [Select][+][-]
  1. init;
  2. while cond do begin
  3.    work
  4.    update
  5. end

or
Code: Pascal  [Select][+][-]
  1. a := random(101); // init
  2. while a <= 75 do begin
  3.   writeln(a); // work
  4.   a := random(101); // update => different meaning, even if same text
  5. end;
  6.  

Or (not what I would do...)
Code: Pascal  [Select][+][-]
  1. goto loopstart;
  2. while a <= 75 do begin
  3.   writeln(a);
  4. loopstart:
  5.   a := random(101);
  6. end;
  7.  
« Last Edit: August 12, 2020, 12:35:58 pm by Martin_fr »

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: while loop with code before comparison
« Reply #32 on: August 12, 2020, 01:01:58 pm »
Or (not what I would do...)
Code: Pascal  [Select][+][-]
  1. goto loopstart;
  2. while a <= 75 do begin
  3.   writeln(a);
  4. loopstart:
  5.   a := random(101);
  6. end;
  7.  

I love the look of that

But not sure if fpc can compile such unusual constructs properly

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: while loop with code before comparison
« Reply #33 on: August 12, 2020, 01:15:00 pm »
@ But not sure if fpc can compile such unusual constructs properly

It can, it only give false-positive warning on this line:
Code: Pascal  [Select][+][-]
  1. while a <= 75 do begin

Warning: Local variable "a" does not seem to be initialized
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: while loop with code before comparison
« Reply #34 on: August 12, 2020, 02:55:11 pm »
Code: Pascal  [Select][+][-]
  1.   WHEN <condition> IN <assignation> DO <code>
  2.  

Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewLoopType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   WHEN Value <=75 IN Value := Random (101) DO
  8.     WriteLn (Value, '%')
  9. END.

Hum, I like it, if I may (and if everybody is cool), I would propose if something could be changed, to refactor the "IN" reserved-word-behavior and give it some extra feature.

Afaik, the "IN" reserved-word was not a creation of Wirth (but I may be wrong).

So it could be something like this (without the new "WHEN" keyword):

Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewLoopType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   WHILE Value <=75 IN Value := Random (101) DO
  8.     WriteLn (Value, '%');
  9. END.

And it could be used for IF too:

Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewIFType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   IF Value <=75 IN Value := Random (101) THEN
  8.     WriteLn (Value, '%');
  9. END.

And for 'FOR' too.

Fre;D
« Last Edit: August 12, 2020, 04:00:27 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: while loop with code before comparison
« Reply #35 on: August 12, 2020, 05:07:18 pm »
Code: Pascal  [Select][+][-]
  1.   WHEN Value <=75 IN Value := Random (101) DO
  2.  

I don't quite see what you're trying to do, but my opinion is that that is far too close to the ALGOL/C multiple assignment

Code: [Select]
  a == b = c;

...which is the cause of a number of bugs massive even by C standards.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: while loop with code before comparison
« Reply #36 on: August 12, 2020, 06:24:29 pm »
I'd support the addition of a whole Modula2 mode to FPC. :)

I'm not sure how to interpret that with the smiley in there...  LOL

 :-X Well, at least I have no legacy code that wouldn't stand to gain from such a course. 
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: while loop with code before comparison
« Reply #37 on: August 12, 2020, 06:38:38 pm »
I think Circular needs some kind of C-like for loop:
Code: C  [Select][+][-]
  1.   for (<initialization>; <condition>; <iteration>) <code-block>;
  2.  
I see what you mean but not exactly. For such construct, I am ok with:
Code: Pascal  [Select][+][-]
  1. begin
  2.   #initialization
  3.   while #condition do
  4.   begin
  5.     #code-block
  6.     #iteration
  7.   end
  8. end

Quote
Code: Pascal  [Select][+][-]
  1. PROGRAM TestNewLoopType;
  2. VAR
  3.   Value: INTEGER;
  4. BEGIN
  5.   WriteLn ('Here are random percentages <= 75%');
  6.   Randomize;
  7.   WHEN Value := Random (101) IS Value <= 75 DO
  8.     WriteLn (Value, '%')
  9. END.
  10.  
Yeah something like, though I feel like it does not express that we are doing this repeatedly. Hence the need for "for" or "foreach".

@Martin_fr  :D the goto is clever, though a bit upside down
Conscience is the debugger of the mind

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: while loop with code before comparison
« Reply #38 on: August 12, 2020, 08:37:29 pm »
I'd support the addition of a whole Modula2 mode to FPC. :)

I'm not sure how to interpret that with the smiley in there...  LOL

I think that a complete Modula-2 mode might be overkill. Specifically, FPC's uses and declaration/definition syntax and semantics are probably at least as comprehensive as Modula-2's, and while LOOP would be desirable it would probably be worth retaining the current usage of CONTINUE and BREAK. Case-sensitivity enabled or disabled by pragma, since TBH I'd like to see inconsistent case usage at least trigger a warning in the other modes.

Apart from that most things that have been added post-J&W are nearer Modula-2's syntax than Pascal anyway. I'm thinking of the explicit end after finally and exception blocks among other things.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: while loop with code before comparison
« Reply #39 on: August 13, 2020, 11:39:24 am »
Code: Pascal  [Select][+][-]
  1.   WHEN Value <=75 IN Value := Random (101) DO
  2.  

I don't quite see what you're trying to do, but my opinion is that that is far too close to the ALGOL/C multiple assignment

Code: [Select]
  a == b = c;

...which is the cause of a number of bugs massive even by C standards.

MarkMLl
After reading this I've found the flaw in my proposal:  IN keyword is an operator (or almost) that returns a boolean.  So Fred's suggestion to refactor it would be hard and generate some ambiguity wich is against Pascal spirit. :(
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: while loop with code before comparison
« Reply #40 on: August 13, 2020, 12:47:16 pm »
I think the canonical solution is this one, based on @lucamar's proposal:

Code: Pascal  [Select][+][-]
  1.   writeln('here are random percentages <= 75%');
  2.   randomize;
  3.   repeat
  4.     a := random(101);
  5.     if a <= 75 then
  6.       writeln(a, '%')
  7.     else
  8.       break
  9.   until false;
  10.  

It does obviously assume that the compiler optimises usage of the local variable well, and it would upset any purist who equates break/continue with goto.

I don't like any suggestion of multiple assignment in a language that supports polymorphism and/or type promotion, or (in particular) allows the result of a function to be discarded.

The only other realistic possibility would be something like

Code: Pascal  [Select][+][-]
  1.   writeln('here are random percentages <= 75%');
  2.   randomize;
  3.   repeat
  4.     try
  5.       writeln(randomValue, '%')
  6.     except
  7.       exit
  8.     end
  9.   until false;
  10.  

where randomValue is a function that raises an exception if it thinks its caller needs to exit. Note that I've not decorated it with parentheses in an effort to make life as confusing as possible >:-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: while loop with code before comparison
« Reply #41 on: August 13, 2020, 01:20:17 pm »
I'd support the addition of a whole Modula2 mode to FPC. :)

Then why not Oberon?
And I am almost serious.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: while loop with code before comparison
« Reply #42 on: August 13, 2020, 01:35:00 pm »
I'd support the addition of a whole Modula2 mode to FPC. :)

Then why not Oberon?
And I am almost serious.

marcov is a Modula2 fan, so that explains why he'd prefer Modula2 to Oberon. ;)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: while loop with code before comparison
« Reply #43 on: August 13, 2020, 01:39:02 pm »
I'd support the addition of a whole Modula2 mode to FPC. :)

Then why not Oberon?
And I am almost serious.

Because relative to FPC, the major thing that Modula-2 does is sort out the damaging-else problem. Oberon AIUI does other things like deprecating enumerations, subranges, and sets other than bitsets. IMO, being able to define an array as subscripted by an enumeration/subrange or a set as containing members of a particular enumeration/subrange are major expressive elements.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: while loop with code before comparison
« Reply #44 on: August 13, 2020, 01:39:10 pm »
After reading this I've found the flaw in my proposal:  IN keyword is an operator (or almost) that returns a boolean.  So Fred's suggestion to refactor it would be hard and generate some ambiguity wich is against Pascal spirit. :(

You may use also the Martin's idea in conjunction of yours:

Code: Pascal  [Select][+][-]
  1. WHEN Value <=75 IN GetValue(value) DO

But yes, the best would be to refactor "IN" behaviour.

For example I would like to have the possibility of this:

Code: Pascal  [Select][+][-]
  1. Program inextended;
  2. ...
  3. uses
  4. myunit in '../mydir/myotherdir', ...

But all this is for the sport, it is only proposition in the air, without any bad intention.

Fre;D

I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018