Thread: While Loop

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    While Loop

    I am trying to iterate through two list objects representing polynomials to perform a multiplication operation. But I have been unable to fix the problem with the while loop because it never enters the nested for loops and size never gets decremented and I get an infinite loop. Can someone point me in the right direction? Thanks.

    Code:
     int size = b.PolyTerms.size();
     while(!size < 0)
     {
      for(It = tmpPoly.PolyTerms.begin(); It != tmpPoly.PolyTerms.end(); ++It)
      {       
       for(It2 = tmpPoly2.PolyTerms.begin(); It2 != tmpPoly2.PolyTerms.end(); ++It2)       
       {
        exp = (*It) + (*It2);
        size--;
        
        ++It;
        ++It2; 
       
        coeff = (*It) * (*It2);
         
        tPoly.PolyTerms.push_back(exp);
        tPoly.PolyTerms.push_back(coeff);
        
        size--;  
        
        //reset It iterator to first term since there are still more terms to multiply
        //in the second polynomial
        It = tmpPoly.PolyTerms.begin();    
       }
      }
      
     }//end while
      //Post-Condition: size < 0 so all of the terms in the second polynomial have  
      //been multiplied by the first term of the first polynomial. Repeat the 
      //process for the next term.             
     
    ...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    By this
    Code:
    while(!size < 0)
    I'm guessing you mean 'while size is not less than zero'. This is normally written as
    Code:
    while(size >= 0)
    That is, "while size is greater than or equal to zero".

    !size will either evaluate to 1 or 0, neither of which will ever be less than zero.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Oh yes. But I did that to test to see If I could determine what was being executed and what was not. I even tried while (size >0) and for some reason, the for loops are not being executed.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Output b.PolyTerms.size(), tmpPoly.PolyTerms.size(), and tmpPoly2.PolyTerms.size() before the while loop. One of them will probably be zero. If all are greater than zero (and you use while (size > 0)) then you should be at least entering the nested for loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM