condition problem

This is a discussion on condition problem within the C Programming forums, part of the General Programming Boards category; I have a while statement that is breaking when only one of the two conditions is true. The while loop ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Question condition problem

    I have a while statement that is breaking when only one of the two conditions is true.
    The while loop suppose to be breaking when the two conditions are met and not only one.


    suppose hp_p= 1 ,2 ,3
    hp_q= 1,2, null

    when hp_p = 3 and hp_q = null it suppose to keep going, but instead it breaks. WHY?

    Code:
     while((hp_p!=NULL) && (hp_q!=NULL))
        {
            current = (struct integer *) malloc (sizeof( struct integer ));
            if(hp_p!=NULL && hp_q==NULL)
              {
                 d= hp_p->digit + r;
                 if(d>9){d=d-10;r=1;}else{r=0;}
                 hp_p = hp_p->next;
              }
            if(hp_p==NULL && hp_q!=NULL)
              {
                 d= hp_q->digit + r;
                 if(d>9){d=d-10;r=1;}else{r=0;}
                 hp_q = hp_q->next;
              }
            if(hp_p!=NULL && hp_q!=NULL)
              {
                 d= hp_p->digit + hp_q->digit + r;
                 if(d>9){d=d-10;r=1;}else{r=0;}
                 hp_q = hp_q->next;
                 hp_p = hp_p->next;
              }
              
          current->digit = d;
          current->next = result;
          result = current;
        }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by cubanoemg View Post
    when hp_p = 3 and hp_q = null it suppose to keep going, but instead it breaks. WHY?
    Not according to your loop condition. Your condition says "while hp_p is not null and hp_q is not null, keep going". hp_q is null, so the second half fails and your loop stops. See:

    Code:
     while((hp_p!=NULL) && (hp_q!=NULL))

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    So the exit condition is

    hp_p==NULL && hp_q==NULL

    So take the NOT of all of that, and make that your loop expression.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Unhappy

    Quote Originally Posted by Salem View Post
    So the exit condition is

    hp_p==NULL && hp_q==NULL

    So take the NOT of all of that, and make that your loop expression.
    If I take the not from the conditions, it will never step into the while because at the beginning there not null.

    What I want is even though the first condition or the second condition is met, keep running until both of them are true or met.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Try a do-while. It's always guaranteed to execute once, so you will step into the loop, do your thing, and check the condition to continue/terminate at the end.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Ok lets pretend I do a (do while).

    For something like this will work:

    Code:
    Just pretend a and b are linked list or arrays.
    a = 1, 2, 3
    b = 1, 2, null
    
    do{
         
        stuff
    }while(a!=null && b!=null)
    
    it will execute at least once(true)
    but what about this case

    Code:
    Just pretend a and b are linked list or arrays.
    a = 1, 2, 3, 4, 5, 6
    b = 1, 2, null, null
    
    do{
         
        stuff
    }while(a!=null && b!=null)
    it will execute at least once(true) when a=3 b=null will run, but then a= 4 and b=null will not hit.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    How can I make the function continue only if the two conditions are not met and the order could be different. Ex: a could be null first or b could be null first, but only when a and b are null at the same time the program will stop.

    Code:
    while((a!=NULL) && (b!=NULL))
        {
            stuff
        }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    So you want to stop when a is null and b is null. Read up on DeMorgan's Law.

    Then, since the do-while loop condition tells you when to keep going, write your stop condition, which is more obvious, then negate it using DeMorgan's law.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Why does your 'a' list appear to have no null at the end?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Talking

    Quote Originally Posted by Salem View Post
    Why does your 'a' list appear to have no null at the end?
    Yes I was missing an a NUll at the end of a.

    Now I think I came up with a working solution. Since I'm doing this using linked lists I change the conditions to:

    Code:
    while(hp_p != hp_q)
    {
      do stuff
    }
    I don't know if I mentioned I was using Linked Lists.

    One of their memory address could be pointing to null like hp_p->next=null, but until both of them are not actually (equal) hp_p->next=hp_q->next=null the while will keep going. Is working so far.

    Thanks for all the ideas and/or suggestions.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     while((hp_p!=NULL) || (hp_q!=NULL))
    That will keep your loop alive... but it's likely to cause some other problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

Tags for this Thread


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