關於跳脫迴圈的問題?? |
答題得分者是:shaofu
|
sanhang
一般會員 ![]() ![]() 發表:12 回覆:25 積分:17 註冊:2002-08-31 發送簡訊給我 |
大概的程式碼如下:
while MyQuery.Eof do begin for i:=0 to Father.Sons.Count - 1 do begin for j:=0 to Father.Sons[i].NamesList.Count - 1 do begin if (Father.Sons[i].NamesList.String[j]='Jack') then begin break; end; end; if (Father.Sons[i].NamesList.String[j]='Jack') then begin break; end; end; if (Father.Sons[i].NamesList.String[j]='Jack') then begin Continue; end; ....... ....... end;我要在兩個for迴圈中判斷是否在while迴圈中下Continue 而上面的if條件式(Father.Sons[i].NamesList.String[j]='Jack')都是一樣的敘述 這樣寫起來很長重複地方有很多,有沒有可以改善的空間呢? |
Chance36
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:31 回覆:1033 積分:792 註冊:2002-12-31 發送簡訊給我 |
sanhang 你好 大概的修改一下程式碼:
Var lBreak : Boolean; Begin while MyQuery.Eof do // ???? 應該是 Not MyQuery.Eof 才對吧 begin lBreak := False; for i:=0 to Father.Sons.Count - 1 do begin for j:=0 to Father.Sons[i].NamesList.Count - 1 do begin if (Father.Sons[i].NamesList.String[j]='Jack') then begin lBreak := True; break; end; end; if lBreak then begin break; end; end; if lBreak then begin Continue; end; ....... ....... end; |
shaofu
高階會員 ![]() ![]() ![]() ![]() 發表:5 回覆:136 積分:103 註冊:2003-01-07 發送簡訊給我 |
1. Use goto
while MyQuery.Eof do begin for i:=0 to Father.Sons.Count - 1 do begin for j:=0 to Father.Sons[i].NamesList.Count - 1 do begin if (Father.Sons[i].NamesList.String[j]='Jack') then goto JackFound; end; if (Father.Sons[i].NamesList.String[j]='Jack') then goto JackFound; end; ....... ....... continue; JackFound: end;2. Use exception while MyQuery.Eof do begin try for i:=0 to Father.Sons.Count - 1 do begin for j:=0 to Father.Sons[i].NamesList.Count - 1 do begin if (Father.Sons[i].NamesList.String[j]='Jack') then raise Exception.Create('Jack Found'); end; if (Father.Sons[i].NamesList.String[j]='Jack') then raise Exception.Create('Jack Found'); end; ....... ....... except end; end;不過一般來說, 盡量不要把 code 寫得太深比較好 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |