# Thread: do while loop keeps looping

1. ## do while loop keeps looping

Hi, Im working on a program and im trying to use a do while loop in it. My only issue is that the loop never ends, it keeps going and going and I thought I had it right. Can anyone see a problem with it? The first time the loop goes around and I get the answer I want, but it just keeps spitting out that answer until I force quit the program.

Code:
```	do
{
removed = number2/10;
digit = removed % 10;
product = digit*number1;
printf("+ %d", product);
} while (digit != '\0');```
Im not sure if I need to post the rest of the code to diagnose, but let me know if thats needed. 2. Suppose number2 has the value of 10. On the first iteration of the loop,
removed = number2 / 20 = 10 / 10 = 1.
digit = removed % 10 = 1 % 10 = 1
...
digit != '\0' = 1 != 0 = 1
so we go on to the next iteration. In the next iteration,
removed = number2 / 20 = 10 / 10 = 1.
digit = removed % 10 = 1 % 10 = 1
At this point we can easily conclude that since number2 is never changed, the loop will loop infinitely. 3. Originally Posted by laserlight Suppose number2 has the value of 10. On the first iteration of the loop,
removed = number2 / 20 = 10 / 10 = 1.
digit = removed % 10 = 1 % 10 = 1
...
digit != '\0' = 1 != 0 = 1
so we go on to the next iteration. In the next iteration,
removed = number2 / 20 = 10 / 10 = 1.
digit = removed % 10 = 1 % 10 = 1
At this point we can easily conclude that since number2 is never changed, the loop will loop infinitely.
Ah I see. That was really stupid of me. So basically I need to give number2 the removed value so it keeps going down by 1 digit each time. Let me give this a shot, thanks laserlight  4. Originally Posted by wankel Ah I see. That was really stupid of me. So basically I need to give number2 the removed value so it keeps going down by 1 digit each time. Let me give this a shot, thanks laserlight Got it working. I just used this:

Code:
```number2 = number2/10;
etc```
Everything is working awesome, only problem is that i get a zero at the end. for example:
1
x 1234
--------
4
3
2
1
0

Would there be a more efficient way to loop this so that I dont always get a zero at the end? 5. >> Would there be a more efficient way to loop this so that I dont always get a zero at the end?

Just test the condition in the middle of the loop, eg:

Code:
```for( ;; )
{
// calculations
if( condition )
break;
// finalize results
}``` 6. Originally Posted by Sebastiani >> Would there be a more efficient way to loop this so that I dont always get a zero at the end?

Just test the condition in the middle of the loop, eg:

Code:
```for( ;; )
{
// calculations
if( condition )
break;
// finalize results
}```
I used an if else statement:

Code:
```if (product = 0)
{
break;
}

else {
printf("+ %d\n", product);
}```
but it gives me"

1
x1234
-------
4
0

so it skips everything from the first number until zero. Can I not use an if else statement for this? 7. Sure you can. You just have to do it properly. Note that = and == do not mean the same thing. 8. Originally Posted by tabstop Sure you can. You just have to do it properly. Note that = and == do not mean the same thing.
touche. I need to use "==", which means equal to. However, using that, I get this:

1
x1234
-------
4
3

So it just ends at 3, which is weird. 9. Originally Posted by wankel touche. I need to use "==", which means equal to. However, using that, I get this:

1
x1234
-------
4
3

So it just ends at 3, which is weird.
Show your code. It should have a + in front of it, shouldn't it? Or else it's being printed by something later on (just as your 0 wasn't printed by this code, but by your print statements after it). 10. hmm...post the code as it exists now again.

btw, you don't really need an if...else when the fulfilment of the "if" is a break or a return. You can just use the "if", if you follow the logic there. 11. Originally Posted by tabstop Show your code. It should have a + in front of it, shouldn't it? Or else it's being printed by something later on (just as your 0 wasn't printed by this code, but by your print statements after it).
It does have the + before it, I was just simplifying it to show whats going on. Heres the full code:

Code:
```#include <stdio.h>

int main ()
{
int number1;
int number2;
int sum;
int product;
int digit;
int final_product;
int removed;

printf("Enter first number:");
scanf("%d", &number1);
printf("Enter second number:");
scanf("%d", &number2);

if (number2 < 10)
{
final_product=number1*number2;
printf("  %d\n", number1);
printf("x %d\n", number2);
printf("------\n");
printf("  %d\n", final_product);
}

else
{
//number2 = number2/10;
digit = number2 % 10;
product = digit*number1;
printf("%d\n", product);

do
{
number2 = number2/10;
digit = number2 % 10;
product = digit*number1;
if (product == 0)
{
break;
}

else {
printf("+ %d\n", product);
}

} while (digit = '\0');

}

//printf("%d", final_product);

return 0;
}``` 12. while (digit = '\0')
hmmmmmmmmmmmmmmmmmmmmm. 13. Originally Posted by tabstop while (digit = '\0')
hmmmmmmmmmmmmmmmmmmmmm.
Good catch! Its supposed to be "!=". Everything works great now, thank you so much for all youre help   14. Doesn't look like your finished...still need to print the final product result...which you do not seem to have... 15. Originally Posted by ninety3gd Doesn't look like your finished...still need to print the final product result...which you do not seem to have...
Sounds like you want his answers... :| Popular pages Recent additions 