Click to See Complete Forum and Search --> : break a loop when a variable reaches a certain value


cashton2k
10-06-2006, 07:56 AM
hi, ive been trying to break this loop when the variable rt is equal too variable it, but i keep getting errors, how is it done properly?


<%
rts=rts+ts
for e=1 to 50
rt=rt+ts*rk
rk=-k*(rt-at)
%>
<table border="1" width="100%">
<tr>
<td width="98"><%=rts%></td>
<td width="184"><%=rk%></td>
<td><%=rt%></td>
</tr>
</table>
<%rts=rts+ts
next %>



i tried if rt==it then break but maybe i didnt format it properly

thanks for any help

jvanamali
10-06-2006, 12:04 PM
try using

if rt=it then
exit for
End if

cashton2k
10-06-2006, 12:30 PM
i tired


<%
rts=rts+ts
if rt=it then
exit for e=1 to 250
end if
rt=rt+ts*rk
rk=-k*(rt-at)
%>
<table border="1" width="100%">
<tr>
<td width="98"><%=rts%></td>
<td width="184"><%=rk%></td>
<td><%=rt%></td>
</tr>
</table>


<%rts=rts+ts
next %>



but i get

Error Type:
Microsoft VBScript compilation (0x800A040F)
Invalid 'exit' statement
/work/asp/coolingformresult.asp, line 73, column 5
exit for
----^

jvanamali
10-06-2006, 12:44 PM
the code you are writing is wrong
Below is small example on how to use exit for
<%
for e=1 to 50
%>
<table border="1" width="100%">
<tr>
<td width="98"><%=e%></td>
<td width="184"><%=e+1%></td>
<td><%=e+2%></td>
</tr>
</table>
<%
if e=10 then
exit for
End if
next
%>
You can use this way in your code
<%
rts=rts+ts
for e=1 to 250
if rt=it then
exit for
end if
rt=rt+ts*rk
rk=-k*(rt-at)
%>
<table border="1" width="100%">
<tr>
<td width="98"><%=rts%></td>
<td width="184"><%=rk%></td>
<td><%=rt%></td>
</tr>
</table>


<%rts=rts+ts
next %>
i have put the if statement at the top , you put it according to your requirement

cashton2k
10-06-2006, 03:53 PM
cheers for the help, got rid of the errors and the code works when instead of "it" i put a number that occurs once in but when the at value starts reocurring when i want it stop it doesnt, any ideas?

jvanamali
10-06-2006, 04:06 PM
post your whole page code , so that i can understand your variables and your requirement more clearly, if you donot want to post it here mail it to me.

cashton2k
10-06-2006, 04:10 PM
<% option explicit %>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>results</title>
<% dim k,it,at,ts,rts,e,rt,rk
k=Request.Form("k")
it=Request.Form("i")
at=Request.Form("a")
ts=Request.Form("t")
rts=0
rk=-k*(it-at)
rt=it
%>

</head>
<body bgcolor="#B5C0E0">
<h1><img border="0" src="liquidtitle.jpg" width="325" height="50" align="right"></h1>
<h1>Results</h1>
&nbsp;<table border="1" width="100%">
<tr>
<td width="236" bgcolor="#000080"><font color="#C0C0C0">Inputted k value:</font></td>
<td width="127" bgcolor="#C0C0C0"><%=k%></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="236" bgcolor="#000080"><font color="#C0C0C0">Inputted Initial Temperature value:
</font> </td>
<td width="127" bgcolor="#C0C0C0"><%=it%>c</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="236" bgcolor="#000080"><font color="#C0C0C0">Inputted Atmospheric Temperature:
</font> </td>
<td width="127" bgcolor="#C0C0C0"><%=at%>c</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="236" bgcolor="#000080"><font color="#C0C0C0">Inputted Time Step:
</font> </td>
<td width="127" bgcolor="#C0C0C0"><%=ts%></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<p>
<br>
</p>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="98" bgcolor="#000080"><font color="#C0C0C0">Time</font></td>
<td width="184" bgcolor="#000080"><font color="#C0C0C0">Slope</font></td>
<td width="191" bgcolor="#000080"><font color="#C0C0C0">Water
Temperature </font></td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="98" bgcolor="#C0C0C0"><%=rts%></td>
<td width="184"><%=rk%></td>
<td width="191"><%=rt%></td>
<td>&nbsp;</td>
</tr>
</table>
<%
rts=rts+ts
for e=1 to 250
rt=rt+ts*rk
rk=-k*(rt-at)
if rt=at then exit for end if
%>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="98" bgcolor="#C0C0C0"><%=rts%></td>
<td width="184"><%=rk%></td>
<td><%=rt%></td>
</tr>
</table>


<%rts=rts+ts
next%>
<p><a href="coolingform.html">&lt;&lt; Go Back</a></p>


</body>

</html>

jvanamali
10-06-2006, 04:24 PM
can you give some values for the below variables and what should be the expected result

k=Request.Form("k")
it=Request.Form("i")
at=Request.Form("a")
ts=Request.Form("t")

cashton2k
10-06-2006, 04:32 PM
ive been using the following for testing:
k= 0.25
it = 120
at = 20
ts = 1

then in the results when ts = 123, "at" will equal 20 and from there after the at value will be the same

jvanamali
10-06-2006, 05:07 PM
hi cashton , actually the problem is not in the code

the problem is occuring because of the condition rt=at which aren't actually equal i believe although they both seem equal

it is never entering the if condition so it is not exiting for loop, may be there is some difference exponentially, i will look into it tmrw and tell you if i find any solution

cashton2k
10-06-2006, 05:29 PM
cheers, thanks for the help, i really appreciate it

jvanamali
10-07-2006, 07:19 AM
replace the condition if rt=at then exit for end if with
if cstr(rt)=cstr(at) then exit for end if
you might be confused a using astring to compare two numbers, i used string because there is no number datatype(as far as i now) to such large number.

This condition is actually stoppingthe loop at after 122nd row.

Hope this helps

cashton2k
10-07-2006, 12:43 PM
cheers works perfectly! thanks again