# Thread: while loop giving me a hard time

1. ## while loop giving me a hard time

Hi I'm new to C and programming generally and have run into an obstacle with an easy while loop.

I want to add the same value to the variable spoj1min until I get the remainder of division 7.

spoj1min variable has an initial value of 17
spoj2min variable has an inital value of 12

could you tell me what is wrong with the below code that it doesn't increment the value by 17 for each cycle but keeps outputing 0 infinetly?

Thanks a lot for any help

Code:
while(x != 7){
x = spoj1min % spoj2min;
spoj1min = spoj1min + spoj1min;
printf("%d\n", spoj1min,);

2. The value that is stored in spoj1min is modified during each iteration, so the third line of your code snippet is equivalent to "spoj1min = spoj1min * 2", rather than the desired "spoj1min = spoj1min + 17".

3. First, your code is not complete. I assume that it look like this:
Code:
#include <stdio.h>

int main (void) {

int spoj1min = 17;
int spoj2min = 12;
int x = 0;

while(x != 7) {
x = spoj1min % spoj2min;
spoj1min = spoj1min + spoj1min;
printf("%d\n", spoj1min);
}

return 0;
}
The problem is in this line:
Code:
spoj1min = spoj1min + spoj1min;
This will double the value of spoj1min in every iteration and this will result in an overflow.
You can check this with an emergency break:

Code:
#include <stdio.h>

int main (void) {

int spoj1min = 17;
int spoj2min = 12;
int x = 0;
int em_brk = 0;

while(x != 7) {
x = spoj1min % spoj2min;
spoj1min = spoj1min + spoj1min;
printf("%d\n", spoj1min);
if (++em_brk > 50) break;
}

return 0;
}
The program will stop and you can scroll up and see was happens.
I think you want increase spoj1min by 17 in every iteration.
The best way is to assing a new variable (call it 'add_on') and give it the value (in your case: 17).
Then calculate the reminder, print booth out and finish.
If the reminder (x) is 7, the loop will stop.

Leave the emergency break in plaece. It could be that you give numbers that never fulfilled the break contion of the loop.

4. I'm not going to tell you why, but show how to figure out why.
First, if you have an infinite loop, use something else that is finite
so you can slowly increment the loop your self to see what is happening.
x is zero, but not for the very first few iterations.
Then throw in some printf's to see what is happening to your variables.
Here is the code to do so.
After running this, you should be able to figure out why.

Code:
int x, y=0, spoj1min = 17, spoj2min = 12;
while(y != 35){
x = spoj1min % spoj2min;
printf("x is %d\n", x);
spoj1min = spoj1min + spoj1min;
printf("spoj1min is %d\n", spoj1min);
y++;
}

5. Hi everyone,

thanks a lot everyone for the help, now the code looks like this:
Code:
while(x != spoj2min - spoj2jelmin -1)
{
x = spoj1min % spoj2min;

}
printf("%d.", spoj1min);
And the issue is when the condition is met ( the value of x is 7) it runs the loop one more time giving me my desired result plus 17 (not the 187 I want but 204).
Could you tell me why that is?

Thanks a lot

6. ## figured it out

sorry, ignore the question figured it out by switching the addon lines with the dividing (obviously ) thanks again guys, you have been tremendously helpfull

7. Unrelated question , which is faster
X += X;
Or X = 2*X;

8. Originally Posted by RagingGrim
Unrelated question , which is faster
X += X;
Or X = 2*X;
Next time post in a new thread.

Anyway, after optimisations have been applied by the compiler, it is plausible that the resulting executable code might be exactly the same.

9. Code:
for(spoj1min = 17; (spoj1min % spooj2min) != 7; spoj1min += 17)
It was hard for me to manage what you actually wanted, but I guess its that ^

PHP Code:
spoj1min17 spoj1min 123
spoj1min
34 spoj1min 126
spoj1min
51 spoj1min 122
spoj1min
68 spoj1min 125
spoj1min
85 spoj1min 121
spoj1min
102 spoj1min 124
spoj1min
119 spoj1min 120
spoj1min
136 spoj1min 123
spoj1min
153 spoj1min 126
spoj1min
170 spoj1min 12