I am curious if there is a way to translate the "C" for(i,start,last,incr)
statement into the PASCAL FOR i = start to last DO (* incr = 1 *).
I am interested in doing this using the #define statement.
Has anyone done this? Help is much appreciated.
Here is an example of how the "C" 'for' statement is translate into FORTRAN
'DO':
#define DO(i,start,last,incr) for((i)=(start);(i)<=(last);(i)+=(incr))
Thanks in advance.
Nhan H. Huynh, PASCAL 's structure 's best
Science and Engineering Aprrentice, Naval Base, Philadelphia PA.
I'm not sure exactly what you want here.
The Pascal code
for i := lo to hi do body ;
Is (mostly) equivalent to this C code:
for( i = lo ; i <= hi ; i++ ) body ;
If you want to be pedantic, you could do this:
for( index = lo ; index < hi ; index++ ) { int i = index ; body ; }
(assuming `index' is not referenced anywhere in `body').
If you want to go the other way, the C code
for( start_expr ; done_expr ; inc_expr ) body ;
Has no direct equivalent in Pascal. The best you can do is
start_expr ;
while done_expr do begin body ; inc_expr ; end ;
>I am interested in doing this using the #define statement.
No. PLEASE don't do this. It is a bad, vile thing to use the preprocessor
to re-define the language to make it look like some other language.
Trust me, you're better off using C's control structures as they stand.
You're programs will be easier to understand, and you'll learn C a lot
quicker if you don't try to pretend that it's Pascal.
This means that "cute" processor tricks like
#define BEGIN {
#define END }
#define REPEAT do {
#define UNTIL(x) } while( !(x) )
#define AND &&
#define OR ||
/* etc */
Are also a bad idea. In many ways, C and Pascal are very similar
languages. But it is a mistake to assume that they are the same.
Their differences are important too, and the above trickery not only
obscures differences between the two languages, it also makes code
harder to understand, particularly for non-Pascal programmers.
--
You unlock this door with the key of imagination. Beyond it is another
dimension: a dimension if sound, a dimension of sight, a dimension of mind.
You're moving into a land of both shadow and substance, of things and ideas.
You've just crossed over into... the Twilight Zone.
In article <1992Jul28.2...@organpipe.uug.arizona.edu> da...@cs.arizona.edu (Dave Schaumann) writes:
>No. PLEASE don't do this. It is a bad, vile thing to use the preprocessor
>to re-define the language to make it look like some other language.
>
>Trust me, you're better off using C's control structures as they stand.
>You're programs will be easier to understand, and you'll learn C a lot
>quicker if you don't try to pretend that it's Pascal.
Here, here! I heartily agree with Dave Schaumann. I knew a guy that did
this and found (later) that his programs were not very maintainable. Not
only that, but no one else wanted them either! The preprocessor is a
powerful thing, but it is so easy to misuse.
jC
--
__/o___~_ James Card (Firmware Engineer)
(________| Nothin' like Sybus Corporation
@ @ top down 2300 Tall Pines Dr. Suite 100
weather... Largo, Fl 34641 (813) 535-6999
> Hello,
>
> I am curious if there is a way to translate the "C" for(i,start,last,incr)
> statement into the PASCAL FOR i = start to last DO (* incr = 1 *).
> I am interested in doing this using the #define statement.
> Has anyone done this? Help is much appreciated.
>
> Here is an example of how the "C" 'for' statement is translate into FORTRAN
> 'DO':
> #define DO(i,start,last,incr) for((i)=(start);(i)<=(last);(i)+=(incr))
I think the Pascal FOR doesn't do the assignment if the condition is not met:
so
i := 2;
FOR i := 3 TO 0 ...
would never assign 3 to i, since the loop wouldn't execute.
I could be wrong, it's been years since I read the User Manual and
Report (which may not be ANSI/ISO anyway).
>
> Thanks in advance.
>
> Nhan H. Huynh, PASCAL 's structure 's best
> Science and Engineering Aprrentice, Naval Base, Philadelphia PA.
>
>
--
Jim Segrave (Segrave Software Services) j...@grendel.demon.co.uk