Coding problem with 'while' statement

Hi All, There seems to be a problem with this piece of code;
while (us<=9004 && us>=100){
us= (424*v-216564);
'if' statement continues here
The value of 'v' has been determined earlier in the program. I added the serial print because I suspected I wasn't in the while loop. This proved it because I didn't get the print out. Can anybody suggest what may be wrong with this piece of code.
The while statement is testing the variable 'us'. I should be in the while loop with the value of v.

This will help.


But what's the value of "us" before you get to the while loop? If I were you I would have the same serial print as you have in the while, just before it.

Presumably you intialised "us" before that, and perhaps calculated "us" somewhere else and it's not between the limits of 100 and 9004.

Don't forget a while is only entered if the condition is passed; perhaps you're thinking of a do..while which always enters and the condition is to do with the exit criterion.

edit: But yes I guess you should do what CodingBadly was getting at, and post the complete sketch?

The value of ‘us’ is calculated in the ‘while’ loop from the value of ‘v’ acquired previously in the program. I think this is correct from the definition of ‘while’. The while statement looks at the value of the variable derived from ‘v’ and sees if this value is within the limits of the while statement. If yes the while loop continues. If not the while loop is bypassed.

Anyway I am going to test this with a smaller program where the value of ‘v’ is set earlier in the program and then see if I get into the while loop with the print statement. You may be right.

I think this is correct from the definition of ‘while’.

No, I don't think so....

A while loop only enters if the condition is met at the top, ie "us" initially between 100 and 9004. If that condition isn't met, the while will not enter, not even once. It will stay in there (assuming it went in in the first place) as long as "us" remains in those limits. It will only exit if something inside causes it to change, which would mean that "v" needs to be changing in the while as well, to give a new "us".

The do..while alternative to while will always go in once, then it checks to see if it should exit at the bottom.

Print "us" before your while, and it's probably not inside the limits.

Hi Magentaline, You are right. The value of 'us' as well as 'v' has to be entered before the while statement. What a bummer. Didn't think the statement worked that way. Now I think my complete program will work. So much for snippets being no good. They can solve the problem.

They can yes, but with a bit of speculation on my part. You might have got a definitive answer sooner if you posted the whole sketch. In my experience on other fora, it's always a good idea to post more rather than less...

1 Like

and my crystal ball is in the dish washer.

1 Like

The ‘while’ loop checks condition BEFORE entering the loop. ‘do while’ loop on the other hand enters the loop first then checks condition.

Yes I can see what my mistake was now. Thanks guys.

The coding can be the subject of copyright or a patent application. You would not be able to get this if your sketch was in the public domain. Thus the reason for not showing the total sketch.

Which is exactly why it suggests in the page linked by CodingBadly:

If that is too much (or too confidential) make a "demo" program that demonstrates the problem.

Then it goes on to say:

If your "demo" program does not reveal the problem, you have a clue as to where it really is.

1 Like

Please remember that this is a public forum primarily intended for hobbyists and people interested in learning about micro-controllers and electronics, with answers provided by volunteers. If you cannot post full information for whatever reason then the forum is probably not the best place to ask questions. Do consider that just as you might not want to post full information the people here trying to help might not want to help you without that information. There is a section of the forum Jobs and Paid Consultancy - Arduino Forum where you can ask for someone to help, agree terms with them, including a confidentiality agreement if you need one, and then pay them to do what you need.


Code is not legally patent-able.

1 Like

I believe coding is indeed patentable upon the the prerequisite it is qualified to be just that.
It will then be treated as a piece of "intellectual property".

Either what you believe is wrong, or the US courts, including the Supreme Court, have been getting it wrong for decades. In the US, code CANNOT be patented. It can only be copyrighted. The underlying algorithm, if any, MAY be patentable, but NOT the code itself.

1 Like

If you want to patent some code, you better be sure it's the best that can possibly work. Because, any improvement upon it constitutes a novel invention. I only have to cite the original work. Look at any patent, it has a long list of citations of prior art.

Also sometimes the efficacy of a patent or copyright, is determined by the wallet size of the patent holder. Frivolous patents can apply pressure through "patent chill", basically saying we have more lawyers and money than you, you can't afford to fight it.

Sensible entrepreneurs in the computer business, are hunter gatherers that just leverage an idea, consume the meal and move on... not scavengers that descend on a carcass and urinate on it to keep other scavengers away. :slight_smile:

My nephew Conrad Bebbington is a professional software writer and I asked him about this, his comment is:

I think there is almost an unspoken pact in software that we don't do law. There would probably be a lot less software if we did.


History has shown that "probably" is incorrect.

(Conrad must be a youngun.)

1 Like

Ok. I can't answer for what is legal in the US but it might very well be the same here in Europe!
I guess i'm obliged to find out!
In the meantime I hereby put my previous post into the "disputed" bucket!

1 Like