Thread: Counting in loops?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Counting in loops?

    Hello Everyone!
    I am very, very new to C programming and have a question that I cannot find the answer too. I have a while loop and I want it to count the number of times the loop must repeat itself and then work with that number. How do I do this? Using incrementing?

    Also every time I try to run my program it comes up with the message No module definition file specified: using defaults and the windows box terminates before I can finish inputing information. How do I fix this?

    Thank you!
    chuy

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    43
    Unfortunately I am not entirely sure what you mean in some areas of your post, however if you want to count the number of times a loop is executed, you are better off using a for than a while loop, so instead of:

    Code:
    while (condition)
    {
    stuff;
    }
    you could do

    Code:
    int i;
    for (i = 1; condition; i++)
    {
    stuff;
    }
    If you are hung up on using while though, you could do:

    Code:
    int i;
    i = 1;
    while (condition)
    {
    stuff;
    i++;
    }
    As for the "windows box terminates" problem, I presume you mean a DOS command window: try putting
    Code:
    getchar();
    just before the return line of the main function.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    thanks. i think i'll stick with the while loop for now. if i use your example of it can i take the incremented variable and use it outside the loop to manipulate? for instance take 'i' and multiply it by 2? then when displaying the statement at the end (printf) if i specify the variable 'i' it will print the manipulated value?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    to just count the no of inetration u can actually keep track on i in the follwing programme
    Code:
    #include<stdio.h>
    
    int main()
    {
        int i;
        
        for(i=0;i<10;i++)
           printf("Iteration no. %d\n",i+1);   // {  u can include some stuff which u want to repeat within the curly braces}       
        
        getchar();
    }
    /*myoutput
    Iteration no. 1
    Iteration no. 2
    Iteration no. 3
    Iteration no. 4
    Iteration no. 5
    Iteration no. 6
    Iteration no. 7
    Iteration no. 8
    Iteration no. 9
    Iteration no. 10
    */
    ssharish2005

  5. #5
    Registered User Baaaah!'s Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    23
    Quote Originally Posted by chuy
    thanks. i think i'll stick with the while loop for now. if i use your example of it can i take the incremented variable and use it outside the loop to manipulate? for instance take 'i' and multiply it by 2? then when displaying the statement at the end (printf) if i specify the variable 'i' it will print the manipulated value?
    If you declare the variable outside of the 'for' loop - which I assume you have done - then the altered value from the loop will be carried over to the next function/operation in your program, so you will be able to multiply it by, for example, 2. If you declared the variable inside the loop, then any value produced through the operation of the 'for' function would only exist within that particular loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Even / Odd Counting with Loops
    By chris515nuke in forum C Programming
    Replies: 7
    Last Post: 11-09-2008, 05:24 PM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. Counting with loops up, down, up, ... ets
    By at0m1sk in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2007, 08:01 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM