Thread: Need help with "while" statement

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    14

    Need help with "while" statement

    Using two types of loops (for and while) the program is supposed to count from 1 to 5, but I cannot get the while statement to work.

    Code:
    #include <stdio.h>
     int main (void) 
    {    
     unsigned int i;
     
         printf ("This program counts from 1 to 5.\n");
         printf ("Using a for loop:\n");
    
    for (i=1; i <= 5, ++ 1) {
        printf ( "%u...", i);
    }
        printf ("\n");
        printf ("Using a while loop:\n");
    
    while ( i <= 5) {
        printf ("%u...", i);
        ++ 1;
    }
    
        printf ("done.\n");
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, this code shouldn't even compile, as it appears you're using '1' instead of 'i' in a few places.

    Hint: Try printing the value of 'i' after the "for()" loop and before the "while()" loop.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Oh, I actually typed the wrong thing in those areas. The actual code has ++i instead.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should copy/paste your code as it is, so people don't waste time tracking down typos that aren't actually in the code (no big deal this time, though).

    Did you try printing the value of "i" between the loops?

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    I dont understand your hint.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Sorry about that. I couldnt figure out how to copy and paste for some reason it wouldnt let me so I had to retype it. I am very new at writing programs so please bear with me.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That's okay, as I mentioned, no big deal.

    You're already printing the value of 'i' within each loop. Now try printing the value of 'i' between the loops (that is, after the "for()" loop ends and before the "while()" loop starts).

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    That didnt work. It printed a 6 before done.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    That wasn't meant to fix it - it was meant to show you the problem.

    The "for()" loop ends when "i <= 5" is no longer true. Since the "for()" loop increments 'i' each time, then "i <= 5" becomes false when 'i' becomes 6.

    'i' is still 6 when it gets to the "while()" loop. As a result, "while ( i <= 5)" is false right away, meaning the code within that second loop is never executed.

    So how do you think you would fix this?

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Ok! maybe a return 0;

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It's the value of 'i' that is causing your problem. "return 0;" would have no effect on the value of 'i'.

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    so I need to make sure the value is 1 again right?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Try it out and see what happens!

  14. #14
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Ok I put i=1 before the while statement and it worked!! Thanks!

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Now if i can just get the next program...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to add a little extra to a "Switch Statement" assignment.
    By Cameron Taylor in forum C Programming
    Replies: 5
    Last Post: 06-14-2013, 12:43 AM
  2. question about the "fd_set *writefds" parameter in the select() statement
    By django in forum Networking/Device Communication
    Replies: 5
    Last Post: 09-09-2011, 04:16 PM
  3. Replies: 1
    Last Post: 08-06-2007, 10:09 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread