Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3802340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:11:37+00:00 2026-05-19T14:11:37+00:00

Adam Ko has provided a magnificent solution to this question, thanks Adam Ko. BTW

  • 0

Adam Ko has provided a magnificent solution to this question, thanks Adam Ko.

BTW if, like me, you love the c preprocessor (the thing that handles #defines), you may not be aware there is a handy thing in XCode: right click on the body of one of your open source files, go down near the bottom .. "Preprocess". It actually runs the preprocessor, showing you the overall "real deal" of what is going to be compiled. It’s great!


This question is a matter of style and code clarity. Consider it similar to questions about subtle naming issues, or the best choice (more readable, more maintainable) among available idioms.

As a matter of course, one uses loops like this:

for(NSUInteger _i=0; _i<20; ++_i)
  {
  .. do this 20 times ..
  }

To be clear, the effect is to to do something N times. (You are not using the index in the body.)

I want to signal clearly for the reader that this is a count-based loop — ie, the index is irrelevant and algorithmically we are doing something N times.

Hence I want a clean way to do a body N times, with no imperial entanglements or romantic commitments. You could make a macro like this:

 #define forCount(N) for(NSUinteger __neverused=0; __neverused<N; ++__neverused)

and that works. Hence,

forCount(20)
  {
  .. do this 20 times ..
  }

However, conceivably the "hidden" variable used there could cause trouble if it collided with something in the future. (Perhaps if you nested the control structure in question, among other problems.)

To be clear efficiency, etc., is not the issue here. There are already a few different control structures (while, do, etc etc) that are actually of course exactly the same thing, but which exist only as a matter of style and to indicate clearly to the reader the intended algorithmic meaning of the code passage in question. "forCount" is another such needed control structure, because "index-irrelevant" count loops are completely basic in any algorithmic programming.

Does anyone know the really, really, REALLY cool solution to this? The #define mentioned is just not satisfying, and you’ve thrown in a variable name that inevitably someone will step on.

Thanks!


Later…

A couple of people have asked essentially "But why do it?"

Look at the following two code examples:

for ( spaceship = 3; spaceship < 8; ++spaceship )
   {
   beginWarpEffectForShip( spaceship )
   }

forCount( 25 )
   {
   addARandomComet
   }

Of course the effect is utterly and dramatically different for the reader.

After all, there are alresdy numerous (totally identical) control structures in c, where the only difference is style: that is to say, conveying content to the reader.

We all use "non-index-relative" loops ("do something 5 times") every time we touch a keyboard, it’s as natural as pie.

So, the #define is an OKish solution, is there a better way to do it? Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:11:37+00:00Added an answer on May 19, 2026 at 2:11 pm

    A better way is to do this to allow nested forCount structure –

    #define $_TOKENPASTE(x,y)   x##y
    #define $$TOKENPASTE(x,y)   $_TOKENPASTE(x, y)
    
    #define $itr          $$TOKENPASTE($_itr_,__LINE__)
    #define forCount(N)   for (NSUInteger $itr=0; $itr<N; ++$itr)
    

    Then you can use it like this

    forCount(5)
    {
        forCount(10)
        {
            printf("Hello, World!\n");
        }
    }
    

    Edit:
    The problem you suggested in your comment can be fixed easily. Simply change the above macro to become

    #define $_TOKENPASTE(x,y)   x##y
    #define $$TOKENPASTE(x,y)   $_TOKENPASTE(x, y)
    
    #define UVAR(var)           $$TOKENPASTE(var,__LINE__)
    #define forCount(N)         for (NSUInteger UVAR($itr)=0, UVAR($max)=(NSUInteger)(N); \
                                     UVAR($itr)<UVAR($max); ++UVAR($itr))
    

    What it does is that it reads the value of the expression you give in the parameter of forCount, and use the value to iterate, that way you avoid multiple evaluations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have A WCF service that has a class that inherits System.Web.Security.RoleProvider. In this
G'day All, I am working in R. Sorry about this really basic question, but
I have a file that I scrape with PHP. It has 3 rows and
I have ADAM set-up & I've written web-services to complete admin tasks like adding
For example, I have a table which looks like this : id | name
We have a couple of ASP.NET WebForms applications that use the ADAM Membership provider,
I want to show the projects that has had it's checkbox ticked as Branding,
Everyone has this huge massively parallelized supercomputer on their desktop in the form of
I've heard that the automotive industry has something called MISRA C. What are the
I have a form in ColdFusion that initially has 5 input fields for file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.