declaring variables inside for loops

Discussion in 'C++' started by Grey Plastic, Aug 10, 2004.

  1. Grey Plastic

    Grey Plastic Guest

    I'm looking for a way to declare variables inside for statements (or
    perhaps some other statement) and have the following statement execute
    exactly once. For example,

    for(Type var=blah; 1; )

    would be what I wanted, if it ran only once, instead of forever.
    Similarly,

    if(Type var=blah)

    would be what I wanted if the statement following it ran even when
    blah evaluated as false.

    What I'm attempting to do is write my own for-loop style interface
    using defines. For example, I want to write something like

    #define CUSTOM_FOR_BEGIN(x,y,var) \
    for(int x=0; x<256; x++) \
    for(int y=0; y<256; y++) { \
    Type var = blah;

    #define CUSTOM_FOR_END() }

    But I want to omit the CUSTOM_FOR_END part. I want something like

    #define CUSTOM_FOR(x,y,var) \
    for(int x=0; x<256; x++) \
    for(int y=0; y<256; y++) \
    for(Type var = blah; runOnce; )

    so that I can have code like

    CUSTOM_FOR(i,j,whee)
    foo(whee,j);

    and

    CUSTOM_FOO(i,j,whee) {
    foo(whee,j);
    bar(j,i);
    }

    I'm using GNU C++, and am totally cool with using GNU extensions.
     
    Grey Plastic, Aug 10, 2004
    #1
    1. Advertising

  2. Grey Plastic

    David Fisher Guest

    "Grey Plastic" <>
    > I'm looking for a way to declare variables inside for statements (or
    > perhaps some other statement) and have the following statement execute
    > exactly once. For example,
    >
    > for(Type var=blah; 1; )
    >
    > would be what I wanted, if it ran only once, instead of forever.


    [snip]

    > I want something like
    >
    > #define CUSTOM_FOR(x,y,var) \
    > for(int x=0; x<256; x++) \
    > for(int y=0; y<256; y++) \
    > for(Type var = blah; runOnce; )
    >
    > so that I can have code like
    >
    > CUSTOM_FOR(i,j,whee)
    > foo(whee,j);


    What you really need is a functor - have a look at
    http://www.codeproject.com/cpp/TTLFunction.asp ...

    But if you really want to call a macro with some arbitrary code as an
    argument, just put it in brackets (braces ?). All the preprocessor cares
    about is that there is a closing bracket, not what is inside ... Someone
    here wrote some code like this (please excuse C-style output in a C++ news
    group) -

    #define DEBUG_PRINTF(x) printf x
    DEBUG_PRINTF(("%d %d\n", x, y));

    which expands to printf("%d %d\n", x, y);

    David Fisher
    Sydney, Australia
     
    David Fisher, Aug 10, 2004
    #2
    1. Advertising

  3. "Grey Plastic" <> wrote...
    > I'm looking for a way to declare variables inside for statements (or
    > perhaps some other statement) and have the following statement execute
    > exactly once. For example,
    >
    > for(Type var=blah; 1; )
    >
    > would be what I wanted, if it ran only once, instead of forever.


    So, couldn't you just say

    {
    Type var=blah;

    > Similarly,
    >
    > if(Type var=blah)
    >
    > would be what I wanted if the statement following it ran even when
    > blah evaluated as false.


    But that doesn't work, does it?

    >
    > What I'm attempting to do is write my own for-loop style interface
    > using defines. For example, I want to write something like
    >
    > #define CUSTOM_FOR_BEGIN(x,y,var) \
    > for(int x=0; x<256; x++) \
    > for(int y=0; y<256; y++) { \
    > Type var = blah;
    >
    > #define CUSTOM_FOR_END() }


    The parentheses are really unnecessary here.

    >
    > But I want to omit the CUSTOM_FOR_END part. I want something like
    >
    > #define CUSTOM_FOR(x,y,var) \
    > for(int x=0; x<256; x++) \
    > for(int y=0; y<256; y++) \
    > for(Type var = blah; runOnce; )
    >
    > so that I can have code like
    >
    > CUSTOM_FOR(i,j,whee)
    > foo(whee,j);
    >
    > and
    >
    > CUSTOM_FOO(i,j,whee) {
    > foo(whee,j);
    > bar(j,i);
    > }


    You could extend your own CUSTOM_FOR to be

    #define CUSTOM_FOR(x,y,var) \
    for (int x=0;x<256;++x) \
    for (int y=0, runOnce = 1 ;y<256;++y) \
    for (Type var=blah; runOnce; --runOnce)

    >
    > I'm using GNU C++, and am totally cool with using GNU extensions.


    If you need extensions, you should ask in a gnu newsgroup.
     
    Victor Bazarov, Aug 10, 2004
    #3
  4. Grey Plastic wrote:

    > I'm looking for a way to declare variables inside for statements (or
    > perhaps some other statement) and have the following statement execute
    > exactly once.



    #define LET(DECL) if (int __once=1) for (DECL;__once;__once--)
    LET(int i=0) { ... }

    Marco
     
    Marco Manfredini, Aug 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns

    Declaring local variables inside loop

    Hendrik Maryns, Feb 22, 2006, in forum: Java
    Replies:
    37
    Views:
    4,430
    Patricia Shanahan
    Feb 25, 2006
  2. Roy Smith
    Replies:
    5
    Views:
    660
    Bo Persson
    May 11, 2006
  3. Me
    Replies:
    2
    Views:
    263
  4. SM
    Replies:
    8
    Views:
    166
    -Lost
    May 1, 2007
  5. Sister Ray
    Replies:
    4
    Views:
    138
    Sister Ray
    Sep 23, 2008
Loading...

Share This Page