[fpc-pascal] converting c code
Marco van de Voort
marcov at stack.nl
Sun May 13 15:13:40 CEST 2012
In our previous episode, Darius Blaszyk said:
> 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;
More information about the fpc-pascal
mailing list