Sign in Join

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

Mailing lists / fpc-pascal / 2012-05

converting c code

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as is a trace of the loop parameter values). I'm a bit puzzled how this should look elegantly in pascal. Anyone has an idea?

Regards, Darius

unsigned nc = 4;

for (unsigned i = 0, j = nc-1; i < nc; j=i++)
{

	cout << "i:" << i << endl;
	cout << "j:" << j << endl;

}

loop values are:
i:0
j:3
i:1
j:0
i:2
j:1
i:3
j:2

Marco van de Voort
to Darius Blaszyk

In our previous episode, Darius Blaszyk said:

Show quoted text 12 lines
I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as is a trace of the loop parameter values). I'm a bit puzzled how this should look elegantly in pascal. Anyone has an idea? Regards, Darius unsigned nc = 4; for (unsigned i = 0, j = nc-1; i < nc; j=i++) { cout << "i:" << i << endl; cout << "j:" << j << endl; }

Keep in mind that a C loop like

for (x;y;z)
{a}

is equal to Pascal's

x
while y do
  begin
    a
    z
  end;

there _is_ no general case with Pascal for.

so

i:=0;
j:=nc-1
while i<nc do
   begin
     writeln('i:',i);
     writeln('j:',j);
     j:=i;
     inc(i); // post increment, so j gets old version
   end;
Mattias Gaertner
to Darius Blaszyk: I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as…
I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as is a trace of the loop parameter values). I'm a bit puzzled how this should look elegantly in pascal. Anyone has an idea?

IMHO the C code is short, but not elegant. It would be more elegant if the j=i is at the end.

Show quoted text 20 lines
Regards, Darius unsigned nc = 4; for (unsigned i = 0, j = nc-1; i < nc; j=i++) { cout << "i:" << i << endl; cout << "j:" << j << endl; } loop values are: i:0 j:3 i:1 j:0 i:2 j:1 i:3 j:2

Maybe:

var i,j: integer;
begin
  j:=nc-1;
  for i:=0 to nc-1 do begin
    writeln('i:',i);
    writeln('j:',j);
    j:=i;
  end;

Mattias

Dimitri Smits
to Mattias Gaertner

----- Oorspronkelijk e-mail -----

Show quoted text 32 lines
On Sun, 13 May 2012 15:09:15 +0200 Darius Blaszyk wrote: > I'm struggling to convert a simple for loop from C to pascal. The > code is shown below (as is a trace of the loop parameter values). > I'm a bit puzzled how this should look elegantly in pascal. Anyone > has an idea? IMHO the C code is short, but not elegant. It would be more elegant if the j=i is at the end. > unsigned nc = 4; > > for (unsigned i = 0, j = nc-1; i < nc; j=i++) > { > cout << "i:" << i << endl; > cout << "j:" << j << endl; > } > Maybe: var i,j: integer; begin j:=nc-1; for i:=0 to nc-1 do begin writeln('i:',i); writeln('j:',j); j:=i; end;

actualy, Marco's version is more correct. A C(++) for loop does it's evaluation on the fly, whereas in pascal (or some dialects), the for variable(s) are immutable or evaluated as early as possible. In this case it might work, but in general, a C++ for loop is better written as a pascal's "while ... do" of "repeat ... until" construct because of this.

consider a loop where you want to exit out of when a condition is met. In C(++) you can change the values of the for loop's variables on the fly et voila, on next iteration you return.

at least, that was what we were told in our CS courses, comparing C(++) vs Pascal/Modula/Oberon (almost 2 decades ago).

kind regards,
Dimitri Smits

Darius Blaszyk
to Darius Blaszyk: I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as…

Thanks to all. I got the code working now.

Regards, Darius

Darius Blaszyk wrote 24 lines
I'm struggling to convert a simple for loop from C to pascal. The code is shown below (as is a trace of the loop parameter values). I'm a bit puzzled how this should look elegantly in pascal. Anyone has an idea? Regards, Darius unsigned nc = 4; for (unsigned i = 0, j = nc-1; i < nc; j=i++) { cout << "i:" << i << endl; cout << "j:" << j << endl; } loop values are: i:0 j:3 i:1 j:0 i:2 j:1 i:3 j:2_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal