Sign in Join
Wiki copytemporary

Break/fi: what changed

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

Title: Break/fi

OldNewLine
1+# Break
2+
3+`Break` rutiini tosiasiallisesti tuhoaa [[Loop|silmukan (loopin)]]. Sen ensisijaisena sovelluksena on poistua silmukasta ennen suunniteltua loppua.
4+
5+`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.
6+
7+## Collatzin konjektuuri
8+
9+Esimerkki: 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ä).
10+
11+``` pascal
12+program collatz(input, output, stderr);
13+
14+procedure collatzIterative(n: qword);
15+var
16+ i: qword;
17+begin
18+ for i := 0 to high(i) do
19+ begin
20+ writeLn('step ', i:20, ': ', n);
21+
22+ // Collatz conjecture: sequence ends with 1
23+ if (n = 1) or (n > (high(n) / 3 - 1)) then
24+ begin
25+ // leave loop, as next value may get out of range
26+ break;
27+ end;
28+
29+ // n := ifThen(odd(n), 3 * n + 1, n div 2);
30+ if odd(n) then
31+ // n is odd
32+ begin
33+ n := 3 * n + 1;
34+ end
35+ // n is even
36+ else
37+ begin
38+ n := n div 2;
39+ end;
40+ end;
41+end;
42+
43+var
44+ n: longword;
45+begin
46+ readLn(n);
47+
48+ if n < 1 then
49+ begin
50+ writeLn(stderr, 'not a positive integer');
51+ halt(1);
52+ end;
53+
54+ collatzIterative(n);
55+end.
56+```
57+
58+`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`.
59+
60+## Muut huomiot
61+
62+Kuitenkin `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.
63+
64+[[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.
65+
66+## Katso myös
67+
68+- [`break`](https://www.freepascal.org/docs-html/rtl/system/break.html) in the `system` unit
69+- [[Exit|exit]] to return from routines
70+- [[Continue|continue]] ohittaa jäljellä olevan iteroinnin
71+- [[Goto|goto]]
72+
73+<small>
74+
75+## Lähteet
76+
77+1
78+compare 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>
81+
82+---
83+*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