Thread: helpless with while loop.

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    helpless with while loop.

    Hi guys, the loop is really easy, and so simple I have no idea why I do not see what is wrong with it.
    OK, it looks like this:
    Code:
    while (counter = 2, counter < entered_prime)
    {
    	modulus = entered_prime % counter;
    	printf("Modulus %d, Times %d. ", modulus, counter);
    	++counter;
    }
    I tried to insert printf inside the loop to see what is happening. The result was that the loop is infinite and the counter never increments.
    Could someone be so nice and tell me what I am doing wrong?
    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    place "counter = 2" before the while loop, instead of in the while argument, that should stop the infinite loop.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're setting counter to 2 every time your loop checks its value. Also, are you sure you mean to be using the comma operator there? Or do you mean AND?

    = is an assignment
    == is a test for equality

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Oh, right. The counter = 2 (assignment) should be outside. I thought there is no problem with these two arguments in while loop... Thanks, you're great! I think I messed it up with for loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM