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

  • SEARCH
  • Home
  • 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 8504713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:06:46+00:00 2026-06-11T02:06:46+00:00

Say you want to loop through 3 elements like this: for(int i=0; i<3; i++)

  • 0

Say you want to loop through 3 elements like this:

for(int i=0; i<3; i++)
{
    doSomething();
}

Of course, this is the same as saying: doSomething(); doSomething(); doSomething();.

Now, let’s say you want to do something BETWEEN each iteration, as if you are coding this:
doSomething(); doBetween(); doSomething(); doBetween(); doSomething();

Notice how doSomething() is called 3 times, but doBetween() is called 2 times.

Right now, the only way I know how to do this in a loop is:

for(int i=0; i<3; i++)
{
    doSomething();
    if(i<2)
        doBetween();
}

To me, it seems inefficient to run that conditional within a loop. It also makes you have to look at it twice in order to understand the programming intention. Plus, if you change the “3” to something else in the “for” header, you could easily forget to change the conditional, especially as the logic grows. Not only that, but this trick won’t work in a foreach loop because there is no easy way to detect whether we are running the last iteration.

What tips do you have for doing something like this in a way that gives you better performance, better readability, better maintainability, or in a foreach loop?

  • 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-06-11T02:06:47+00:00Added an answer on June 11, 2026 at 2:06 am

    You could always write a reusable function, not sure this would be a good idea unless you needed to reuse this a lot

        public static IEnumerable<TSource> ForEach<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Action<TSource> action, Action<TSource> between)
        {
            bool first = true;
            foreach (TSource item in source)
            {
                if (first) first = false; else between();
                action(item);
            }
            return source;
        }
    

    This would be called like so:

    myList.ForEach(i => DoSomething(), i => DoBetween());
    

    Or

    Enumerable.Range(0, 3).ForEach(i => DoSomething(), i => DoBetween());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to write a function like this: int get_some_int(string index) { ...perform
Let's say I have 2 lists in Python and I want to loop through
Say i want to loop through XML nodes but i want to ignore the
I'm thinking of an approach something like this. Please let me know if this
Say I want to loop through a datareader and create a load of objects
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have this each loop in Ruby. @list.each { |i| puts i
Say I want to define that an URI such as: myapp://path/to/what/i/want?d=This%20is%20a%20test must be handled
Say I want to copy all elements of list A into list B. But
id like to know how I can execute this. say i have an array

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.