23.07.2014, 05:35
I need to use this IsNumeric code in my script and it works, but I like to avoid using code created by other unless it's in the form of an include library, such as YSI and if I MUST borrow some code, I always like to understand exactly how it works.
I understand that it's comparing characters. The part I don't understand is the "(j = string[i])" part. I've seen a few of these IsNumeric functions with "j" being set to the string length and the loop ending once "i" is equal to "j", or the string length but I don't see how "(j = string[i])" tells the loop when to stop. It's setting "j" to each character in the string and checking if they are numeric, so if the string is numeric, does the loop simply stop when it reaches the end of the string then?
The other thing I don't get is why "(j = string[i])" needs brackets. If you remove the brackets it gives a warning saying that it may be an unintentional assignment statement. It is an assignment statement and you usually don't assign a value in that part of the loop so, it right in assuming that it's unintentional, but that's what's got me confused.
In short, I don't understand how assigning "j" the each character in the string tells the loop when to stop.
Код:
for(new i = 0, j; (j = string[i]); i++) { if (j < '0' || j > '9') { return 0; } } return 1;
The other thing I don't get is why "(j = string[i])" needs brackets. If you remove the brackets it gives a warning saying that it may be an unintentional assignment statement. It is an assignment statement and you usually don't assign a value in that part of the loop so, it right in assuming that it's unintentional, but that's what's got me confused.
In short, I don't understand how assigning "j" the each character in the string tells the loop when to stop.