| 1 | module Breaks; |
| 2 | |
| 3 | (* Leaving loops by break: on conditions of and and or, before elsif |
| 4 | and else, and with a deferred statement to run on the way out. *) |
| 5 | import Out; |
| 6 | var log: INT; |
| 7 | proc note; begin INC(log); end; |
| 8 | proc main*; |
| 9 | var i, s: INT; b: BOOL; |
| 10 | begin |
| 11 | for i := 0 to 100 do if i > 3 and i # 4 then break; end; s := s + i; end; |
| 12 | Out.int(s); Out.ln; (* 0+1+2+3+4 = 10 *) |
| 13 | s := 0; |
| 14 | for i := 0 to 100 do if i = 7 or i * i > 30 then break; elsif i = 2 then s := s + 100; else s := s + i; end; end; |
| 15 | Out.int(s); Out.ln; (* 0+1+100+3+4+5 = 113 *) |
| 16 | s := 0; i := 0; |
| 17 | while true do b := i = 5; if b then break; end; INC(i); end; |
| 18 | Out.int(i); Out.ln; (* 5 *) |
| 19 | for i := 0 to 3 do defer note; if i = 2 then break; end; end; |
| 20 | Out.int(log); Out.ln; (* 3 *) |
| 21 | end; |
| 22 | end Breaks. |