HACKER Q&A
📣 camillomiller

Anyone knows how to write this code in Pascal?


I have a piede of python code and I would like to know how this would be written in Pascal (it’s for historical research reasons). I tried to look it up but I couldn’t figure it out for the life of me.

Here’s the python code:

for i in range(116000): x = i y = (x*4)/2.71 print(x, y)

Thanks (and sorry for the weird Ask HN :) )


  👤 epc Accepted Answer ✓
It’s been 35+ years for me but basically:

Alternate version using while:

  program hnscratch;

  var i: integer;
    x: real;
    y: real;

  i := 0;

  begin;
    while i <116000 do;
      begin;
      x := i;
      y := (x*4) / 2.71;
      writeln(x,y);
      end; 
  end.

👤 tonetheman
Could not tell from the line if you want to print all of those or just one at the end... I just printed at the end

Gah formatting is horrible ... [EDIT] here is a gist: https://gist.github.com/tonetheman/db1c3d3cc8a63a0498ed3b165...

program TestMe; var i : Longint; x : Longint = 0; y : Real = 0; begin

        for i:=0 to 116000 do
                begin
                        x := i;
                        y := (x*4)/2.71;
                end;
        writeln(x,y);
end.

👤 test666v666
Tested at https://www.jdoodle.com/execute-pascal-online/

Program test(output); var i,x:integer; y:real; begin for i:=1 to 10 do begin x:=i; y:=(x*4)/2.71; writeln(x,y); end;

end.


👤 compressedgas
You might want to check this with a compiler.

  program example;
  
  var 
     i: integer;
     x: real;
     y: real;

  begin
     for i := 0 to 116000-1 do begin
        x := i;
        y := (x*4)/2.71;
        println(x, y)
     end
  end.
The difference to note is that the Pascal FOR loop is end inclusive that is it is a closed interval rather than the half open interval that Python uses for its range.

👤 test666v666
Program test(output); var i,x:integer; y:real; begin for i:=1 to 116000 do begin x:=i; y:=(x*4)/2.71; writeln(x,y); end;

end.