Bidirectional loop? (Increase/decrease depending on values)

This is a discussion on Bidirectional loop? (Increase/decrease depending on values) within the C Programming forums, part of the General Programming Boards category; I have two values: x_start and x_end. From time to time any value can be the highest. How can I ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    Bidirectional loop? (Increase/decrease depending on values)

    I have two values: x_start and x_end. From time to time any value can be the highest.
    How can I make a single for or while-loop to iterate through x in any directions to count either from x_start to x_end or the other way around?

    Of course I can do it with an if statement and two for loops, but since I'm gonna fill the loop with lots of stuff, I'd like to smack it into one loop. Is that possible?

    Code:
    int step = 2;     // Can vary
    int x_start = 10; // This could be 20
    int x_end = 20;   // And this could be 10
    
    for (int x = x_start; x<=x_end; x+=step) {
      printf ("x = %d\n", x);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640
    Yep. Have a second set of variables, called smallest and largest. Use the classic
    Code:
    int smallest = x_start < x_end ? x_start : x_end;
    // similar for largest
    
    for (int x = smallest; x <= largest; x += step)
    ...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or, if you have to start at start and stop at end (regardless of whether that's up or down), then you do the same sort of ?: trickery, except making step positive or negative.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Either make step negative when x_end < x_start, or put the body of the loop into its own function, and just go with two loops.
    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"

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Thanks a lot, that worked out great. Have coded PHP for 10 years, never had to use that before. But now when coding a game, that was handy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding values to array in a for loop
    By bman900 in forum C Programming
    Replies: 1
    Last Post: 11-06-2010, 07:58 AM
  2. Problem in Reading values from structure
    By Nisha_B in forum C Programming
    Replies: 3
    Last Post: 09-12-2010, 01:03 PM
  3. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 03:56 PM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 06:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21