Counting with loops up, down, up, ... ets

This is a discussion on Counting with loops up, down, up, ... ets within the C++ Programming forums, part of the General Programming Boards category; I am trying to find out how I can count with a loop in a range from lowest to highest ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Counting with loops up, down, up, ... ets

    I am trying to find out how I can count with a loop in a range from lowest to highest then highest to lowest...maybe a loop is not the best thing to use?

    something like

    1 2 3 4 5

    then 5 4 3 2 1

    then 1 2 3 4 5 again and this keep repeating until i choose to stop it..which i know how to do

    any help would be appreciated!

    I would like for the number to be stored in a single variable.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    A straightforward way is to use two inner loops nested in an outer loop. It is possible to use a single inner loop if you use a formula, or handle two cases.
    Last edited by laserlight; 10-18-2007 at 12:14 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    would u mind giving me an example with the 1-5 then back again?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    well I can give u the exact code I am trying to use this for but its not as simple as counting from 1-5 then from 5-1 again....this is just the simplest concept of what I must accomplish. I am drawing a square with 4 lines from 1 side of a drawing area to another, once the edge touches the side I want it to go back. In essence this is accomplished the same way I would do from 1-5 and 5-1 but in the while loops i must have arguments ofcourse

    Code:
    			//Asteroid 1 Redrawing
    			while(a1_line1x2<=490){
    				SetDrawColor(win, 1.0, 1.0, 1.0);
    				Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
    				Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
    				Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
    				Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);
    				
    				a1_line1x1+=update2;
    				a1_line1x2+=update2;
    				
    
    				a1_line2x1+=update2;
    				a1_line2x2+=update2;
    				
    
    				a1_line3x1+=update2;				
    				a1_line3x2+=update2;
    				
    
    				a1_line4x1+=update2;				
    				a1_line4x2+=update2;
    				
    
    				SetDrawColor(win, 0.0, 0.0, 0.0);
    				Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
    				Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
    				Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
    				Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);
    			
    			}
    			
    			while(a1_line3x2>=10) {
    				SetDrawColor(win, 1.0, 1.0, 1.0);
    				Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
    				Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
    				Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
    				Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);
    				
    				a1_line1x1-=update2;
    				a1_line1x2-=update2;
    				
    
    				a1_line2x1-=update2;
    				a1_line2x2-=update2;
    				
    
    				a1_line3x1-=update2;				
    				a1_line3x2-=update2;
    				
    
    				a1_line4x1-=update2;				
    				a1_line4x2-=update2;
    				
    
    				SetDrawColor(win, 0.0, 0.0, 0.0);
    				Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
    				Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
    				Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
    				Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2); }
    this is my code for one of my boxes, right now it keeps jumping back and forth with "while" loops but if i put an if there the box will go to the edge...redraw itself backwards once ... then redrawn itself forwards. the whole box redrawing is done in a body of a bigger event loop.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    imagine you have two functions:
    Code:
    int count_up(int from, int distance); //returns the ending position
    int count_down(int from, int distance); //returns the ending position
    call these in a for loop (one after the other) and run them as many times as you want.
    inside these functions, you can do whatever you want as long as its done counting when it returns.

    you could even combine the two functions into one and pass a direction. you will need to change the direction appropriately though. Right now do you want it to change directions when it hits a boundary? When it hits a boundary, you can return the direction and pass it back into the function when it goes through the loop again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Even / Odd Counting with Loops
    By chris515nuke in forum C Programming
    Replies: 7
    Last Post: 11-09-2008, 04:24 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. Counting in loops?
    By chuy in forum C Programming
    Replies: 4
    Last Post: 10-17-2005, 02:56 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

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