Understanding ForEach() loop with 3 parameters -- please help!

A

almurph

Hi,

I'm translating some C into C#.NET and came accross the "ForEach"
loopp with 3 parameters. For the life of me i can't figure it out and
can;t seem to get any info online (strange, no?)
Anyway can anyone explain it to me please?

Code is like this:

ForEach(a, 0, c)

{

DeltaFreq[a] = 0;


}




Would appreciate any comments/suggestions/code-samples/relevent URLs
that you would like to share.

Thanking you,
Al.
The Confused.
 
G

Guest

        I'm translating some C into C#.NET and came accross the "ForEach"
loopp with 3 parameters.

<snip>

C has no ForEach construct. Ask on a C# newgroup
 
J

James Kuyper

Hi,

I'm translating some C into C#.NET and came accross the "ForEach"
loopp with 3 parameters. For the life of me i can't figure it out and
can;t seem to get any info online (strange, no?)
Anyway can anyone explain it to me please?

Code is like this:

ForEach(a, 0, c)

{

DeltaFreq[a] = 0;


}

ForEach is not part of the C programming language. It is either a
function or a function-like macro; either way, it's not part of the C
standard library. It's defined somewhere else, and you'll have to find
that definition before you can be sure what it does

Given it's name, I strongly suspect that it is a macro defined something
like the following:

#define ForEach(x,y,z) for(x = (y); x<(z); x++)

In which case the above code would be equivalent to:

for(a=0; a<c; a++)
{
DeltaFreq[a] = 0;
}

I consider this macro to be a bad idea. I recommend using a lot of
caution when translating code that makes use of such macros.
 
A

almurph

James,

Your a star - it is a macro for:

#define ForEach(a,b,c) for(a=b ; a<=c ; ++b)

Merci,
Al.
The enlightened

James said:
Hi,

I'm translating some C into C#.NET and came accross the "ForEach"
loopp with 3 parameters. For the life of me i can't figure it out and
can;t seem to get any info online (strange, no?)
Anyway can anyone explain it to me please?

Code is like this:

ForEach(a, 0, c)

{

DeltaFreq[a] = 0;


}

ForEach is not part of the C programming language. It is either a
function or a function-like macro; either way, it's not part of the C
standard library. It's defined somewhere else, and you'll have to find
that definition before you can be sure what it does

Given it's name, I strongly suspect that it is a macro defined something
like the following:

#define ForEach(x,y,z) for(x = (y); x<(z); x++)

In which case the above code would be equivalent to:

for(a=0; a<c; a++)
{
DeltaFreq[a] = 0;
}

I consider this macro to be a bad idea. I recommend using a lot of
caution when translating code that makes use of such macros.
 
J

James Kuyper

James,

Your a star - it is a macro for:

#define ForEach(a,b,c) for(a=b ; a<=c ; ++b)

Check that again. I strongly suspect that you will find that it ends
with ++a, not ++b.

If you've copied it correctly, there's only a few very obscure ways it
could be used, and any such use would belong in the International
Obfuscated C Code Contest <http://www.ioccc.org/>.
 
K

Keith Thompson

James Kuyper said:
Hi,
I'm translating some C into C#.NET and came accross the
"ForEach"
loopp with 3 parameters. For the life of me i can't figure it out and
can;t seem to get any info online (strange, no?)
Anyway can anyone explain it to me please?
Code is like this:
ForEach(a, 0, c)
{
DeltaFreq[a] = 0;
}

ForEach is not part of the C programming language. It is either a
function or a function-like macro; either way, it's not part of the C
standard library. It's defined somewhere else, and you'll have to find
that definition before you can be sure what it does
[...]

We've already established that it's a macro, but I'll mention anyway
that it couldn't be a function, since a function call can't be
immediately followed by a '{' character.
 
C

CBFalconer

I'm translating some C into C#.NET and came accross the "ForEach"
loopp with 3 parameters. For the life of me i can't figure it out
and can;t seem to get any info online (strange, no?)
Anyway can anyone explain it to me please?

C# has nothing to do with C, and C has no ways of translating
function calls. Find a group that deals with C#. It will probably
be something of Microsofts, because I believe that is a language
peculiar to them.
 
I

Ian Collins

CBFalconer said:
C# has nothing to do with C, and C has no ways of translating
function calls. Find a group that deals with C#. It will probably
be something of Microsofts, because I believe that is a language
peculiar to them.
Late unhelpful and inattentive yet again.

Did you even bother to read the OP, or the helpful set of responses?

It's pretty clear to those with a basic reading level that the OP
thought "ForEach" was a C construct.
 
J

J. J. Farrell

CBFalconer said:
C# has nothing to do with C, and C has no ways of translating
function calls. Find a group that deals with C#. It will probably
be something of Microsofts, because I believe that is a language
peculiar to them.

Where do function calls come into it?
 
B

Bart

christian.bau said:
Whoever wrote that #define should be shot.

Well, it should have been called just For (and the original probably had
++a). (And is more suited to 1-based arrays when used for looping over one.)

Other than that, what's the problem? The author clearly had an issue with
the fiddliness of C's for statement (where, essentially, you have to show
the compiler how to code the for statement every time you write one), and
wanted to make coding a little less painful and error-prone.

For example, the potential for making the ++b and similar errors is limited
to the one macro, instead of in dozens of places in the code.

Anyway getting shot is probably a little extreme for transgressing someone
else's idea of coding style..
 
K

Keith Thompson

Bart said:
Well, it should have been called just For (and the original probably
had ++a). (And is more suited to 1-based arrays when used for looping
over one.)

Other than that, what's the problem? The author clearly had an issue
with the fiddliness of C's for statement (where, essentially, you have
to show the compiler how to code the for statement every time you
write one), and wanted to make coding a little less painful and
error-prone.

For starters, it's wrong; the "++b" should be "++a" if you ever want
the loop to terminate.

The problem is that anyone (other than, perhaps, the author) reading
the code has to (a) understand how ForEach maps to an ordinary for
loop, and (b) understand how a for loop works. Whereas just directly
writing
for (a = b; a <= c; ++a)
or, even better:
for (i = start; i <= end; ++i)
only requires understanding how a for loop works, and is a common
enough idiom that anyone who has trouble with it probably won't
understand the program anyway.
For example, the potential for making the ++b and similar errors is
limited to the one macro, instead of in dozens of places in the code.

Anyway getting shot is probably a little extreme for transgressing
someone else's idea of coding style..

Yes, that is a bit much.

Stabbed?
 
C

CBFalconer

Keith said:
.... snip ...


Yes, that is a bit much.

Stabbed?

That is also somewhat extreme. How about a diet of pre'sarved
turnips. See Al Capp if missing the connection.
 
G

Guest

You are commenting, I assume, upon the typo (should be a++, not ++b).

#define ForEach(a,b,c) for (a = b; a <= c; ++a)
Other than that, it is fine.

no it is not fine. It disguises the syntax of C for
no net gain, not even in space. Macros like this
make the code hard to read. Bigger examples make
it debugger unfriendly as well :)


--
Nick Keighley

/*****************************************************
- Note:
- I wanted to put a check in getGuiExtract(), but it's a void
method, inherited from the father of the father of the father of
the
father of the father of the father of the father of the father
of
the father of the father of the father of the father of the
father
of the father of the father of the father of the father of the
father of the father of the father of the father of the father
of
the father of the father of the father of the father of the
father ...

So I can't modify its interface.
*****************************************************/

Found in C++ code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top