Thread: Beginning question about While loops

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    23

    Beginning question about While loops

    Hello! So I'm currently trying to teach myself C via the Internet, and I'm stuck on the While loop. Here's what I'm trying to do: the user should input a non-negative number no greater than 23; if the number is greater than 23, then the prompt should let the user try again and then re-evaluate the new number to see if it's a non-negative number.

    Here's what I have:

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        printf("Insert a non-negative integer no greater than 23: ");
        
        int x = GetInt();
        while (x > 23)
            printf("Insert a non-negative integer LESS than 24!: ");
            x = GetInt();
    
         while (x < 0)
            printf("Insert a NON-NEGATIVE integer less than 24!: ");
            x = GetInt();
    
         return 0;
    }
    However, when I run the program and type in 28 for example, it's an infinite loop and does NOT ask for another integer (no GetInt(); activated), which I don't understand because in my while loop, I state that the prompt should allow the user to type in another number and then evaluate to see if it should get out of the while loop.

    What am I doing wrong? Thank you!
    Last edited by Rubik; 02-16-2013 at 07:50 PM. Reason: Wrong code

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1. Your first problem is that you need braces around your loop. Omitting braces means the next single statement is looped over.

    What happens right now is equivalent to this:

    Code:
    int main(void)
    {
        printf("Insert a non-negative integer no greater than 23: ");
         
        int x = GetInt();
        while (x > 23)
        {
            printf("Insert a non-negative integer LESS than 24!: ");
        }
        x = GetInt();
     
        while (x < 0)
        {
            printf("Insert a NON-NEGATIVE integer less than 24!: ");
        }
        x = GetInt();
     
        return 0;
    }
    In the actual body of the loop, GetInt() is never called - x never changes throughout the while loop, so it infinitely loops because it never gets a new x to evaluate.

    2. You also have a second problem - you want to check both conditions in a single loop, not in two separate loops. Consider the || operator, which takes two boolean (conditional) statements and combines them such that the result is true if either of the conditions is true.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    Also, I forgot to include this, but: I know you can easily execute this code using the do-while loop, but I want to do it using the While loop. Thank you.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    Got it! Thank you so much, Cat! That makes so much more sense. You're freaking awesome.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    C does not care about indentation, so you need to add braces to do what you want

    Code:
    while (...) {
       statement1;
       statement2;
       ...
       statement_n;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question concerning for loops
    By Programmer_P in forum C++ Programming
    Replies: 36
    Last Post: 05-26-2009, 05:11 PM
  2. question on GCD and for loops
    By dals2002 in forum C Programming
    Replies: 9
    Last Post: 03-15-2008, 01:59 AM
  3. beginning question
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2007, 03:57 PM
  4. Question about loops.
    By Hulag in forum C Programming
    Replies: 4
    Last Post: 02-17-2005, 05:37 AM
  5. beginning array question
    By Noobie in forum C Programming
    Replies: 22
    Last Post: 05-01-2003, 03:18 AM