You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- - Tunnet käsitteen toistolause ja osaat luoda ohjelman, joka sisältää toistolauseen. -->
10
-
- You are familiar with while-loops and know how to utilize them in your program.
10
+
- You are familiar with loops and know how to utilize them in your program.
11
11
12
12
<!-- - Osaat käyttää `break`-komentoa toistolauseen suorituksen lopettamiseen ja toistolausetta seuraavaan käskyyn siirtymiseen. -->
13
-
- You know how to use the `break`-command in order to break out of the while-loop and onto the next statement.
13
+
- You know how to use the `break`-command in order to break out of the loop and onto the next statement.
14
14
15
15
<!-- - Osaat käyttää `continue`-komentoa toistolauseen alkuun palaamiseen. -->
16
-
- You know how to use `continue`-command to go to the beginning of the while-loop.
16
+
- You know how to use `continue`-command to go to the beginning of the loop.
17
17
18
18
<!-- - Osaat luoda ohjelman, joka lukee käyttäjältä syötettä kunnes käyttäjä syöttää tietynlaisen syötteen -- esim luku 0 tai merkkijono "loppu", jonka jälkeen ohjelma kertoo ennen lopettamista syötetyistä syötteistä (esim. syötteiden lukumäärä, lukujen tapauksessa summa ja keskiarvo). -->
19
19
- You are able to create a program that reads user inputs until a specific input is given, for example number 0 or a string "end", after which the program prints something about the given inputs (if the inputs were numbers, then the sum and the average of the numbers)
<!-- Tietokoneen sisältämä käskyjen suorittamiseen erikoistunut prosessori pystyy -- moderneissa tietokoneissa -- suorittamaan yli miljardi (konekielistä) käskyä sekunnissa. Tässä osassa tutustumme toistettavan ohjelmakoodin määrittelyyn toistolauseiden avulla. -->
24
24
<!-- TODO Missä muodossa tulisi ilmaista? -->
25
-
Computer contains a processor that can, in modern computers, process over billions of (machinecode) commands per second. In this section we will get familiar with writing program code that's going to be repeated with while-loops.
25
+
Computer contains a processor that can, in modern computers, process over billions of (machinecode) commands per second. In this section we will get familiar with writing program code that's going to be repeated with loops.
26
26
27
27
<!-- Motivoidaan toistolauseiden käyttöä hieman. Alla on esimerkki ohjelmasta, missä kysytään käyttäjältä viisi lukua ja lasketaan niiden summa. -->
28
-
As a motivation to using while-loops, there's an example code bellow which asks five numbers from the user and calculates their sum.
28
+
As a motivation to using loops, there's an example code bellow which asks five numbers from the user and calculates their sum.
29
29
30
30
```java
31
31
Scanner scanner =newScanner(System.in);
@@ -53,7 +53,7 @@ System.out.println("The sum of the numbers is " + sum);
53
53
It does the trick, but not quite elegantly. What if the program had to read one hundred or perhaphs one thousand numbers and print their sum? What if the program had to read only three numbers?
54
54
55
55
<!-- Tämän ongelman voi ratkaista toistolauseella, joka pitää kirjaa sekä summasta että lukemiskerroista. Viiden luvun summan tulostava toistolauseella toteutettava ohjelma näyttää seuraavalta. -->
56
-
This problem can be solved with a while-loop, which keeps track of both the sum and the amount of read numbers. The program that prints the sum of five numbers looks now as follows
56
+
This problem can be solved with a loop, which keeps track of both the sum and the amount of read numbers. The program that prints the sum of five numbers looks now as follows
57
57
58
58
```java
59
59
Scanner scanner =newScanner(System.in);
@@ -75,12 +75,12 @@ System.out.println("The sum of the numbers is " + sum);
75
75
```
76
76
77
77
<!-- Tutustutaan seuraavaksi toistolauseisiin. -->
78
-
Next, we will get familiar with while-loops.
78
+
Next, we will get familiar with loops.
79
79
80
80
<!-- ## Toistolause ja ikuinen toisto -->
81
-
## While-loop and infinite loop
81
+
## loop and infinite loop
82
82
<!-- Toistolause sisältää lausekkeen, jonka perusteella päätellään jatketaanko toistoa, sekä lohkon, joka sisältää toistettavan lähdekoodin. Toistolauseen muoto on seuraava. -->
83
-
The while-loop contains a statement which determines if the code within the loop should be repeated. The form of a while-loop is as follows
83
+
The loop contains a statement which determines if the code within the loop should be repeated. The form of a loop is as follows
84
84
85
85
```java
86
86
while (_statement_) {
@@ -96,10 +96,10 @@ while (_statement_) {
96
96
97
97
<!-- Käytämme toistaiseksi lausekkeena `true`-arvoa, eli boolean-tyyppista arvoa "totta". Tämä tarkoittaa sitä, että toistolauseen toistamista jatketaan aina kun ohjelma on tilantessa, missä selvitetään tuleeko toistolauseen suoritusta jatkaa. Tämä tapahtuu sekä silloin kun ohjelman suoritus päätyy toistolauseeseen ensimmäistä kertaa että silloin kun ohjelman suoritus päätyy toistolauseen lohkon loppuun. -->
98
98
<!-- TODO Muokkaa "execution of the program first ..." parempaan muotoon -->
99
-
We will use the value `true` as the while-loop's statement. This means that the while-loop always decides to repeat when it enters the statement. This happens when the execution of the program first arrives to the while-loop statement and also when it's at the end of the while-loop block.
99
+
We will use the value `true` as the loop's statement. This means that the loop always decides to repeat when it enters the statement. This happens when the execution of the program first arrives to the loop statement and also when it's at the end of the loop block.
100
100
101
101
<!-- Toistolauseen suoritus etenee askeleittain lause kerrallaan. Seuraava ohjelma tulostaa merkkijonoa _osaan ohjelmoida!_ ikuisesti eli "äärettömän monta kertaa": -->
102
-
The execution of while-loop proceeds line by line. The following outputs _I can program_ infite amount of time.
102
+
The execution of loop proceeds line by line. The following program outputs _I can program_ infite amount of time.
103
103
```java
104
104
while (true) {
105
105
System.out.println("I can program!");
@@ -110,25 +110,28 @@ while (true) {
110
110
The program that runs infinitely does not close on its own. The program can be closed from the red square button located in the Netbean's output window.
111
111
112
112
113
-
## Toistolauseen päättäminen
113
+
<!-- ## Toistolauseen päättäminen -->
114
+
# Ending a loop
114
115
115
-
Toistolauseen saa päätettyä komennolla `break`. Kun tietokone suorittaa komennon `break`, siirtyy ohjelman suoritus toistolauseen lohkoa seuraavaan komentoon.
116
+
<!-- Toistolauseen saa päätettyä komennolla `break`. Kun tietokone suorittaa komennon `break`, siirtyy ohjelman suoritus toistolauseen lohkoa seuraavaan komentoon. -->
117
+
The loop statement can be broken out of with command 'break'. When a computer executes command 'break', the program moves from the loop section onto the next command.
116
118
117
-
Alla olevassa esimerkissä on ohjelma, joka tulostaa luvut yhdestä viiteen. Ohjelmassa määritellään toistolauseen sisällä käsiteltävä luku ennen toistolauseen lohkoa. Tällöin muuttujan kasvatus onnistuu.
119
+
<!-- Alla olevassa esimerkissä on ohjelma, joka tulostaa luvut yhdestä viiteen. Ohjelmassa määritellään toistolauseen sisällä käsiteltävä luku ennen toistolauseen lohkoa. Tällöin muuttujan kasvatus onnistuu. -->
120
+
In the example below is a program that prints numbers from one to five. In the program, a variable used within the loop is defined before the loop. This allows incrementation of the variable.
Toistolauseesta poistutaan esimerkiksi kun käyttäjä syöttää tietynlaisen syötteen tai mikäli toistolauseessa tehtävä laskenta päätyy haluttuun lopputulokseen. Tällaiset ohjelmat sisältävät sekä toistolauseen, jota käytetään toistettavan ohjelman määrittelyyn, että toistolauseen sisällä olevan ehtolauseen, jota käytetään toistolauseesta poistumiseen käytettävän ehdon täyttymisen tarkasteluun.
148
+
<!-- Toistolauseesta poistutaan esimerkiksi kun käyttäjä syöttää tietynlaisen syötteen tai mikäli toistolauseessa tehtävä laskenta päätyy haluttuun lopputulokseen. Tällaiset ohjelmat sisältävät sekä toistolauseen, jota käytetään toistettavan ohjelman määrittelyyn, että toistolauseen sisällä olevan ehtolauseen, jota käytetään toistolauseesta poistumiseen käytettävän ehdon täyttymisen tarkasteluun. -->
149
+
Breaking out of the loop occurs when a user enters a specific input or whether the loop's calculations ends in wanted result. These kinds of programs contains a loop, which is used in
150
+
defining a code which is going to be repeated, and the condition for the loop that is used to check whether or not to exit the loop.
146
151
147
-
Toistolauseessa voidaan myös kysyä käyttäjältä syötettä. Toistolauseessa useasti käytettävät muuttujat (kuten Scanner-lukija) määritellään ennen toistolausetta, toistokohtaiset muuttujat (kuten luettu arvo) määritellään toistolauseessa.
152
+
<!-- Toistolauseessa voidaan myös kysyä käyttäjältä syötettä. Toistolauseessa useasti käytettävät muuttujat (kuten Scanner-lukija) määritellään ennen toistolausetta, toistokohtaiset muuttujat (kuten luettu arvo) määritellään toistolauseessa. -->
153
+
In the loop, user can be asked for an input. The variables that are used many times (such as Scanner) are defined before the loop, and the loop specific variables (such as read value) is defined in the loop.
148
154
149
-
Alla olevassa esimerkissä ohjelma kysyy käyttäjältä pitäisikö toistolauseesta poistua. Mikäli käyttäjä syöttää merkkijonon "k", ohjelman suoritus siirtyy toistolausetta seuraavaan komentoon, jonka suorittamisen jälkeen ohjelman suoritus päättyy.
155
+
<!-- Alla olevassa esimerkissä ohjelma kysyy käyttäjältä pitäisikö toistolauseesta poistua. Mikäli käyttäjä syöttää merkkijonon "k", ohjelman suoritus siirtyy toistolausetta seuraavaan komentoon, jonka suorittamisen jälkeen ohjelman suoritus päättyy. -->
156
+
In the example below, program asks the user whether or not to exit from the loop. If the user inputs a string "k", the executuion of program moves to command after the loop, after which the execution of the program ceases.
150
157
151
158
```java
152
-
Scannerlukija=newScanner(System.in);
159
+
Scannerscanner=newScanner(System.in);
153
160
154
161
while (true) {
155
-
System.out.println("Poistutaanko? (k lopettaa)");
156
-
Stringsyote=lukija.nextLine();
157
-
if (syote.equals("k")) {
162
+
System.out.println("Exit? (y exits)");
163
+
Stringinput=scanner.nextLine();
164
+
if (input.equals("y")) {
158
165
break;
159
166
}
160
167
161
-
System.out.println("Ok! Jatketaan!");
168
+
System.out.println("Ok! Let's carry on!");
162
169
}
163
170
164
-
System.out.println("Valmista!");
171
+
System.out.println("Ready!");
165
172
```
166
173
167
-
Ohjelma toimii esimerkiksi seuraavasti. Alla käyttäjän syötteet ovat merkitty punaisella.
168
-
174
+
<!--Ohjelma toimii esimerkiksi seuraavasti. Alla käyttäjän syötteet ovat merkitty punaisella.-->
175
+
The program in the example works as follows. The users inputs are in red color.
Kirjoita edellä olevaa toistolause-esimerkkiä mukaillen ohjelma, joka kysyy käyttäjältä "Jatketaanko?" kunnes käyttäjä syöttää merkkijonon "ei".
186
-
192
+
<!--Kirjoita edellä olevaa toistolause-esimerkkiä mukaillen ohjelma, joka kysyy käyttäjältä "Jatketaanko?" kunnes käyttäjä syöttää merkkijonon "ei".-->
193
+
Write a program by using the loop-example that asks "Shall we carry on?" until the user inputs a string "no".
187
194
<sample-output>
188
195
189
-
Jatketaanko?
190
-
**kyllä**
191
-
Jatketaanko?
192
-
**kyl**
193
-
Jatketaanko?
194
-
**k**
195
-
Jatketaanko?
196
-
**ei**
196
+
Shall we carry on?
197
+
**yes**
198
+
Shall we carry on?
199
+
**ye**
200
+
Shall we carry on?
201
+
**y**
202
+
Shall we carry on?
203
+
**no**
197
204
198
205
</sample-output>
199
206
200
207
</programming-exercise>
201
208
202
209
203
-
Edellisessä esimerkissä ohjelma lukee käyttäjältä merkkijonomuotoisia syötteitä. Vastaavanlaisen ohjelman toteutus onnistuu myös muilla muuttujatyypeillä. Alla olevassa esimerkissä käyttäjältä pyydetään lukuja kunnes käyttäjä syöttää luvun nolla.
210
+
<!-- Edellisessä esimerkissä ohjelma lukee käyttäjältä merkkijonomuotoisia syötteitä. Vastaavanlaisen ohjelman toteutus onnistuu myös muilla muuttujatyypeillä. Alla olevassa esimerkissä käyttäjältä pyydetään lukuja kunnes käyttäjä syöttää luvun nolla. -->
211
+
In the previous example, the program reads string type inputs. Similarly, the program can be done with other types of variables. The program below asks numbers from the user until the user inputs a zero.
0 commit comments