# condition problem

• 02-07-2011
cubanoemg
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;     }```
• 02-07-2011
anduril462
Quote:

Originally Posted by cubanoemg
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))`
• 02-07-2011
Salem
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.
• 02-07-2011
cubanoemg
Quote:

Originally Posted by Salem
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.
• 02-07-2011
anduril462
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.
• 02-07-2011
cubanoemg
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)```

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.
• 02-07-2011
cubanoemg
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     }```
• 02-07-2011
anduril462
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.
• 02-07-2011
Salem
Why does your 'a' list appear to have no null at the end?
• 02-07-2011
cubanoemg
Quote:

Originally Posted by Salem
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.
• 02-07-2011
CommonTater
Code:

` while((hp_p!=NULL) || (hp_q!=NULL))`
That will keep your loop alive... but it's likely to cause some other problems.