For loop help...

This is a discussion on For loop help... within the C Programming forums, part of the General Programming Boards category; Need a quick advice on the FOR loop (im new)... I need my loop to, basically, loop until a certain ...

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

    Question For loop help...

    Need a quick advice on the FOR loop (im new)...
    I need my loop to, basically, loop until a certain number/character is entered (let's assume it's "0" ).
    My immediate thought was to do it this way:

    Code:
    #include <stdio.h>
    
    int main ( )
    {
      int i, choice;
      choice = 1;
    
      for (i = 0; choice != 0; i++)
      {
        printf("Enter a number: ");
        scanf("%d", &choice);
      }
      printf("You've entered a 0. Exiting the loop...");
    }
    The loop does work, but while compiling the program i get a warning that i is assigned a value that is never used. No matter what i try i just cant find a way around it, although i have this feeling its quite simple...
    Please help....

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try a do while loop instead. for loops are usually used for iterations, like counting.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Yaps, just like i thought - the solution was quite simple... I just didnt think "outside the box" (was trying to make it work with the for loop)

    Thanx

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    also:
    Code:
      for (; choice != 0;)
      {
        printf("Enter a number: ");
        scanf("%d", &choice);
      }

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Whoa, didnt know this little trick... Thanx a lot... I like the for loop a lot more than the while loop for some reason, so its good news

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Code:
    > for (; choice != 0;)
    I never knew you could do this . Mind explaining what the ; does ? This could be a replacement for all while loops than.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It has no real advantage over while loops, ; does nothing, the typical for loop is:
    Code:
    for(<initialization> ; <condition> ; <per loop execution>)
        <statement>
    Omit the '<initialization>' and '<per loop execution>' and you get:
    Code:
    for(; <condition> ;)
        <statement>

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by ICool View Post
    Mind explaining what the ; does ?
    it's part of the syntax for the for loop. it's needed to delimit the different areas of the for loop.

    this is an infinite loop:

    Code:
    for(;;) { do something }
    This could be a replacement for all while loops than.
    yes. the while loop and for loop and do while loops are there so that the code can be more readable. but you can make code that acts like one loop but uses another.

Popular pages Recent additions subscribe to a feed

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