Thread: for loops in c++

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    for loops in c++

    I am writing a program using for loops and outputting the number of iterations in each loop. However, my program is outputting a line for every iteration of the loop, not just the total number of iterations.

    Is there a way to count the number of iterations of a loop and output that total?

    Attached is the program.

    Brian

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int Counter = 0;
    
    for(int i=0; i<10; i++)
    {
       Counter++;
    
       ... //Do whatever you want
    }
    
    cout << Counter;
    You could also print i+1, but that depends on which starting value i has. A general formula for this loop:

    for(int i=a; i<b; i++)

    would be: b-a times (assuming that the for loop completes, and is not breaked).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    You mean something like:

    Code:
    int cnt = 0;
    for (int i = -10; i <= 10; i++)
    {
      cnt++;
    }
    
    cout << "Loop 4 executes " << cnt << " times.";
    }
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    Thanks for the help

    Thanks for your help.

    Brian

  5. #5
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    problems

    In my program I am running 7 for loops, using counter for each one leaves me with each loop adding its iterations to the counter. Do I need to use a different name for each counter?

    Brian

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Depends. If you print the result after each loop you can use the same. Just remember to nullify it (set it to 0) after every loop.
    Code:
    int Counter = 0;
    
    for(...)
    {
       Counter++;
    }
    
    cout << "Loop 1: " << Counter;
    Counter = 0;
    
    for(...)
    {
       Counter++;
    }
    
    cout << "Loop 2: " << Counter;
    Counter = 0;
    
    for(...)
    {
       Counter++;
    }
    
    cout << "Loop 3: " << Counter;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    edit: I guess I answered his question about the same time Magos did... sorry

  8. #8
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    thanks all

    Thanks for your help again. I am just getting started so I'm sure most of my questions will be easily answered.

    Brian

Popular pages Recent additions subscribe to a feed