473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

declaring variables inside for loops

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.
Jul 22 '05 #1
3 2124
"Grey Plastic" <gr*********@hotmail.com>
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
Jul 22 '05 #2
"Grey Plastic" <gr*********@hotmail.com> 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.
Jul 22 '05 #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.

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

Marco

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
2
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date; DECLARE end_date date; SET start_date =...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
5
by: Patrick | last post by:
Hi all, In my SQL procedures in DB2 8.2, I current declare several cursors that return select statements with 10 columns in it. As a result, assigning host variables becomes a coding mess, as...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
4
by: Sister Ray | last post by:
I've searched for a way to this in a lot of forums. Haven't found a solution for this, however I've found a work around. Declaring a public object and then adding the variables i want as members...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.