...

View Full Version : half logic, half syntax



Spudhead
08-20-2002, 02:31 PM
I don't think my brain is working right today, this is fairly straightforward but I can't get my head round it.

In an array, I have a list of costcodes that the currently logged-in user is associated with: they can sign off any orders that have that costcode.

I'm looping through a recordset of orders. Inside that loop, I then loop through the array, looking for a match between that order's costcode and one of the user's costcodes. If I find a match, I want to write an "Authorise" button" - if not, I don't.

It's not working because it doesn't break out of the if statement correctly. I'm aware of labeling statements and breaking out of them like that, and I think that's what I need to do - but can't find much reference on it. I'm doing this in JSCRIPT, by the way.

Here's what I've got, if anyone can help with the logic or pointing out where I'm going wrong with the syntax, I'd be grateful.

Cheers.




<%
for(i=0;i<userCodes.length;i++){
Inner:if((String(userCodes[i]))==(String(rs.Fields("costcode")))){
%>
<td class="bodytext" valign="top"><input type="button" class="actionbutton" value="Authorise" onClick="authOrder('<%=rs.Fields("orderID")%>')"></td>
<%
break Inner;
}
else{
%>
<td class="bodytext" valign="top">&nbsp;</td>
<%
break Inner;
}
}
%>

RadarBob
08-20-2002, 03:08 PM
Not sure 'zacly what you see your code doing but here's my 2 cents worth...

no, no, no, no... Don't convolute the code by forcing your way out - essentially a goto in the middle of a loop. Instead, build your loops so they control themselves. Some will say it's a matter of preference, but having dealt with this sort of thing in a major program re-write I did, I have to say from my experience it just makes code maintenance, modification, etc. much, much easier.


<%
var foundOne = new Boolean (false);
var i = 0;
while (i<userCodes.length || !foundOne){
if((String(userCodes[i]))==(String(rs.Fields("costcode")))){
foundOne = true;
%>
<td class="bodytext" valign="top">
<input type="button" class="actionbutton" value="Authorise"
onClick="authOrder('<%=rs.Fields("orderID")%>')"></td>
<%
}else{
%>
<td class="bodytext" valign="top"> </td>
<%
}
i++;
}// while()
%>


This code conveys much better what you are really trying to do vis-a-vis a doctored-up for loop. AND, it conforms to a basic tenant of structured programming - "enter at the top, exit at the bottom" (as opposed to breaking out somewhere in the middle)

whammy
08-22-2002, 01:48 AM
RadarBob, I didn't know there was any other way! I guess that's a good thing... lol (although I did cheat about 20 years ago in BASIC like that when I was a kid... probably not good practice); however select statements in ASP and switch() statements in other languages seem to be ok with breaking out of them... but of course that's not a loop - more like an if/then...

* Waiting for the REAL programmers to laugh at me now! *

;)

RadarBob
08-22-2002, 04:31 PM
however select statements in ASP and switch() statements in other languages seem to be ok with breaking out of them...

One does not get off on false technicalities! The design intent of the switch() is to jump around the remaining cases and on to the end (closing bracket) of the switch(). And you're right, it's not a loop. You changed the subject.

I'm talking about forcing your way out of a loop rather than having the loop logic do it. It's like saying (in code) "I'm doing this.." and in the middle of this, changing your mind. All your control logic should be stated up front as part of the IF(...), (chained together by &&, || as needed); not hidden inside various parts of the loop itself....

If you find yourself wanting to do that again, just created a well-named boolean that you can set, and test for that condition UP FRONT in the looping control. "Hear me now and believe me later", when you're dealing with a page-long loop you'll be glad you did. And you make testing a magitude easier. Trust me on this:thumbsup:

BTW, after some reading on Boolean objects I'd change the while() like this:

while (i<userCodes.length || foundOne==false){



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum