asp 跳出迴圈 避開陷阱

來源:互聯網
上載者:User

for迴圈嵌套的時候一定要及時跳出,否則就會出現莫名其妙的問題,就像我開始的時候這樣:

寫一個線上購彩程式,當使用者在購彩頁面選擇了複選框的值後,程式會根據這些值產生可能的注數,並輸出。
for each b in iddI
for each c in iddII
for each d in iddIII
for each e in iddIV
for each f in iddV
for each g in iddVI
for each h in iddVII

'if b="" then
'response.write("資料位元數不完全,請您重新選擇")
'response.end()
'end if
if caizhong="福彩3D" or caizhong="排列3" then
n=trim(b)&trim(c)&trim(d)
str=str&n&" "
elseif caizhong="排列5" then
n=trim(b)&trim(c)&trim(d)&trim(e)&trim(f)
str=str&n&" "
elseif caizhong="七星彩" then
n=trim(b)&trim(c)&trim(d)&trim(e)&trim(f)&trim(g)&trim(h)
str=str&n&" "
end if
next
next
next
next
next
next
next

輸出時用的是

<%=str%>
奇怪的是只有當caizhong="七星彩"的時候才能正常輸出,其他情況下str的值都為空白。誰知道怎麼回事兒,給幫下忙。多謝了先。

到處求教而不得其解,鬱悶的我都快要撞牆了,最後查for迴圈的時候看到跳出迴圈的語句 exit for,靈感突發,心想,是不是沒有跳出迴圈的原因呢?修改程式為如下代碼:
for each b in iddI
for each c in iddII
for each d in iddIII
if caizhong="福彩3D" or caizhong="排列3" then
n=trim(b)&trim(c)&trim(d)
str=str&n&"&nbsp;"
exit for
end if
for each e in iddIV
for each f in iddV
if caizhong="排列5" then
n=trim(b)&trim(c)&trim(d)&trim(e)&trim(f)
str=str&n&"&nbsp;"
exit for
end if
for each g in iddVI
for each h in iddVII
if caizhong="七星彩" then
n=trim(b)&trim(c)&trim(d)&trim(e)&trim(f)&trim(g)&trim(h)
str=str&n&"&nbsp;"
end if

next
next
next
next
next
next
next

儲存,運行,哈哈終於按我的意思顯示了,激動ing。
大家以後再使用for迴圈嵌套的時候要小心嘍,該跳出的時候一定要跳出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.