Thread: Any clever construct for a foreach loop in C?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Any clever construct for a foreach loop in C?

    I have some code where I want to do a for loop, iterating over a fixed set of integers. Unfortunately, the set isn't in any incrementalable order - e.g., it's something like
    Code:
    { 10, 14, 23, 25 }
    These numbers are known at the time I'm writing the code.

    I can think of various ways of simulating a "foreach loop" (like perl, etc. have) - put them in an array and do a for loop by index, a linked list, etc.

    I'm just wondering if there is some clever construct that I'm overlooking to do a for() loop that isn't limited to number sets that are neatly incremental.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by monk64 View Post
    I have some code where I want to do a for loop, iterating over a fixed set of integers. Unfortunately, the set isn't in any incrementalable order - e.g., it's something like
    Code:
    { 10, 14, 23, 25 }
    These numbers are known at the time I'm writing the code.

    I can think of various ways of simulating a "foreach loop" (like perl, etc. have) - put them in an array and do a for loop by index, a linked list, etc.

    I'm just wondering if there is some clever construct that I'm overlooking to do a for() loop that isn't limited to number sets that are neatly incremental.
    Just put it in an array and iterator over the indices.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You say that the data is known, where is the data located? If it's on a file you can do something like this:

    Code:
            while( fscanf(fp, "%d", &in) != EOF ){
                    //do something
            }
    Otherwise, an array is probably the best way to store your numbers.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    There is no clever way of doing this, the best path is what you have already thought of by using an array and loop by index. Not to skew too much but the Linux scheduler works very much like this using run time/ wait queues(FIFO) methodology for single proc systems.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What do you expect foreach to do for you that you can't do by any other kind of looping construct?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  2. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM