Thread: How to run a loop a set number of times?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    How to run a loop a set number of times?

    Hello C Board! or should I say "Hello World" ok that was uncalled for sorry

    Anyways, I'm a EE trying to pedal my way through C for a project I'm working on (build guitars for a living and am rigging up a pickup winder program that drives a stepper.


    Anyways, I have a loop that I'd like to run a set number of times is there a way I can do this say 3 times and then proceed to the rest of the program?

    Code:
      for(pos = 76; pos < 101; pos += 1)  
      {                                  
        myservo.write(pos);             
        delay(7);                       
      } 
      
      for(pos = 102; pos>=79; pos-=1)  
      {                                
        myservo.write(pos);          
        delay(8);                // 83-95 #4         
      }

    Thanks!
    Nick

  2. #2
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Just put both in an outer for loop that loops 3 times.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    So I guess

    Code:
    void loop
    {
    
    void loop
    {
    }
    
    }
    but how does it know how many and what tells it to stop the loop and continue with the rest after the 3 times?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Code:
    int i;
    for(i = 0;i < 3;i++) /* outer loop */
        for(....) /* inner loop */{
        }
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCL Scripting
    By johndejesus in forum Tech Board
    Replies: 5
    Last Post: 08-07-2009, 03:35 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM