Sign in Join
Wiki copytemporary

Break/fi: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Break
22
33`Break` rutiini tosiasiallisesti tuhoaa [[Loop|silmukan (loopin)]]. Sen ensisijaisena sovelluksena on poistua silmukasta ennen suunniteltua loppua.
44
55`Break` rutiinilla on erityinen merkitys silmukasta poistumisesta, voidaan kirjoittaa *vain* silmukoissa (loopeissa). Se ei ole varattu sana¹, joten sitä voi käyttää, tällöin siihen päästään kirjoittamalla [`system.break`](https://www.freepascal.org/docs-html/rtl/system/break.html) milloin tahansa.
66
77## Collatzin konjektuuri
88
99Esimerkki: Seuraava ohjelma käsittelee [Collatzin konjektuuria (Collatz problem)](https://fi.wikipedia.org/wiki/Collatzin_konjektuuri). [[For|For-silmukka]] aliohjelmassa `collatzIterative` käyttää `break`:ä a) tarkistamaan lopetustilan Collatz-ongelman mukaan, b) keskeyttää ennen kuin tietotyypin rajat ylittyvät ja c) Samalla kun käytetään edelleen `for` -rakennetta (muuttuu automaattisesti [[Inc|lisäten]] muuttujaa määrätyn alueen sisällä).
1010
1111``` pascal
1212program collatz(input, output, stderr);
1313
1414procedure collatzIterative(n: qword);
1515var
1616 i: qword;
1717begin
1818 for i := 0 to high(i) do
1919 begin
2020 writeLn('step ', i:20, ': ', n);
2121
2222 // Collatz conjecture: sequence ends with 1
2323 if (n = 1) or (n > (high(n) / 3 - 1)) then
2424 begin
2525 // leave loop, as next value may get out of range
2626 break;
2727 end;
2828
2929 // n := ifThen(odd(n), 3 * n + 1, n div 2);
3030 if odd(n) then
3131 // n is odd
3232 begin
3333 n := 3 * n + 1;
3434 end
3535 // n is even
3636 else
3737 begin
3838 n := n div 2;
3939 end;
4040 end;
4141end;
4242
4343var
4444 n: longword;
4545begin
4646 readLn(n);
4747
4848 if n < 1 then
4949 begin
5050 writeLn(stderr, 'not a positive integer');
5151 halt(1);
5252 end;
5353
5454 collatzIterative(n);
5555end.
5656```
5757
5858`for`-silmukan valitseminen yhdessä `break`:n kanssa on riittävä, koska Collatzin olettamus olettaa, että kuvattu toiminto lopulta päättyy `1`: n, mutta ei kerro sitä varmasti. Siksi - matemaattisesti - käyttämällä ehtoa `while n <> 1 do` ei ota huomioon olosuhteita, että ongelma on oletus, mutta ehdottaa, että se päätetään lopulta johtaa `n = 1`.
5959
6060## Muut huomiot
6161
6262Kuitenkin `break`:n käyttö on yleensä huonoa tyyliä, koska se "siirtyy" silmukan ehtoon. Ohjelmoijan täytyy tietää, että silmukan lohkossa jossa on `break` määritellään kaikki keskeytysolosuhteet.
6363
6464[[GNU Pascal]]:n käsikirjan mukaan `break` on [[Borland Pascal]]:n laajennus, kun taas [[Mac Pascal]]:ssa se oli `leave`. [[FPC]]:n [[Mode MacPas|{$mode macpas}]] tietää ainoastaan `break`:n.
6565
6666## Katso myös
6767
6868- [`break`](https://www.freepascal.org/docs-html/rtl/system/break.html) in the `system` unit
6969- [[Exit|exit]] to return from routines
7070- [[Continue|continue]] ohittaa jäljellä olevan iteroinnin
7171- [[Goto|goto]]
7272
73-<small>
74-
7573## Lähteet
7674
77751
7876compare remarks in [the reference manual § “The `For..to`/`downto..do` statement”](https://www.freepascal.org/docs-html/ref/refsu58.html) and [§ “reserved words”](https://www.freepascal.org/docs-html/ref/refsu3.html)
79-
80-</small>
8177
8278---
8379*From the Free Pascal wiki, page "Break/fi", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history