Arduino

Why the FUCK is this an infinite loop? I'm converting natural numbers to binary numbers. It displays the following:

0
1
10
10
...

instead of giving me the binary representation of 11 for 3. It works when I manually define numb t be 3 but breaks when I try to loop and make it count

Attached: killme.jpg (331x431, 33K)

You enter a while loop in which numb is always greater than one with no exit condition

C++ has a function for that, so why would you write your own?

Wrong.

You're modifying numb which is the iterator for your for loop like a dumbass.

When numb == 2 it gets floored in while loop into 1 which causes infinite loop. Btw that if in while is 100% useless. Arduino is just C so learn C

Because I'm trying to learn the language
Thanks, I'll check it out. I just realized that if in while is useless. I did this code in 15 minutes in MATLAB and it works perfectly, now I've spent 2 hours trying to make it work in Arduino

Line 10, you integer divide numb by 2. This line is first hit on the iteration when numb = 2. After line 10, numb is now 1. Numb goes through the rest of the iteration unchanged, then the numb++ in the for loop header puts numb back to 2. This process gets repeated again. The for loop’s break condition, numb >= 3, is never reached. This doesn’t happen on the first two iterations of the for loop because the while loop at lines 6-12 doesn’t run until numb >= 2. Fix this by using a dummy variable like a or c for the for loop counter in line 2, and immediately after line 2 write int numb = a; This copies the value of the for loop variable, which I’m assuming represents the number you’re converting to binary, so that you have a variable to manipulate in the body of the loop without affecting the loop variables.

Long story short, don’t fuck with the variable in the for header if you don’t know what you’re doing. Also, number your fucking lines.

low iq post

actually, turns out I need that if statement for whatever reason.

Fucking thank you, man, I got it working. And thank you for the advice.

Reinventing the wheel isn't always necessary.