@@ -20,14 +20,9 @@ a given number of elements, the **_for-loop_** is what we want to use. The for-l
20
20
syntax:
21
21
22
22
``` chpl
23
- // single-statement version
24
23
for index in iterand do
25
- instruction;
26
-
27
- // multi-statement version
28
- for index in iterand
29
24
{instructions}
30
- ```
25
+ ```
31
26
32
27
The * iterand* is a function or statement that expresses an iteration; it could be the range 1..15, for
33
28
example. * index* is a variable that exists only in the context of the for-loop, and that will be taking the
@@ -40,7 +35,7 @@ This `for` loop, for example
40
35
41
36
``` chpl
42
37
// calculate the new temperatures (temp_new) using the past temperatures (temp)
43
- for i in 1..rows
38
+ for i in 1..rows do
44
39
{
45
40
// do this for every row
46
41
}
@@ -52,10 +47,10 @@ this:
52
47
53
48
``` chpl
54
49
// calculate the new temperatures (temp_new) using the past temperatures (temp)
55
- for i in 1..rows
50
+ for i in 1..rows do
56
51
{
57
52
// do this for every row
58
- for j in 1..cols
53
+ for j in 1..cols do
59
54
{
60
55
// and this for every column in the row i
61
56
}
@@ -67,10 +62,10 @@ follows:
67
62
68
63
``` chpl
69
64
// calculate the new temperatures (temp_new) using the past temperatures (temp)
70
- for i in 1..rows
65
+ for i in 1..rows do
71
66
{
72
67
// do this for every row
73
- for j in 1..cols
68
+ for j in 1..cols do
74
69
{
75
70
// and this for every column in the row i
76
71
temp_new[i,j] = (temp[i-1,j] + temp[i+1,j] + temp[i,j-1] + temp[i,j+1]) / 4;
@@ -83,17 +78,6 @@ Note that at the end of the outer `for` loop, when all the elements in `temp_new
83
78
` temp ` with the values of ` temp_new ` ; this way everything is set up for the next iteration of the main ` while `
84
79
statement.
85
80
86
- We're ready to execute our code, but the conditions we have initially set up
87
- will not produce interesting output, because the plate has a temperature
88
- value of ` 25 ` everywhere. We can change the boundaries to have temperature ` 0 `
89
- so that the middle will start cooling down. To do this, we should change the
90
- declaration of ` temp ` to:
91
-
92
- ``` chpl
93
- var temp: [0..rows+1, 0..cols+1] real = 0; // the whole plate starts at 0
94
- temp[1..rows,1..cols] = 25; // set the non-boundary coordinates to 25
95
- ```
96
-
97
81
Now let's compile and execute our code again:
98
82
99
83
``` bash
@@ -255,9 +239,9 @@ the job:
255
239
``` chpl
256
240
// update delta, the greatest difference between temp_new and temp
257
241
delta=0;
258
- for i in 1..rows
242
+ for i in 1..rows do
259
243
{
260
- for j in 1..cols
244
+ for j in 1..cols do
261
245
{
262
246
tmp = abs(temp_new[i,j]-temp[i,j]);
263
247
if tmp > delta then delta = tmp;
@@ -353,8 +337,8 @@ The greatest difference in temperatures between the last two iterations was: 0.0
353
337
354
338
::::::::::::::::::::::::::::::::::::: keypoints
355
339
- "You can organize loops with ` for ` and ` while ` statements. Use a ` for ` loop to run over every element of the
356
- iterand, e.g. ` for i in 1..rows { ...} ` will run over all integers from 1 to ` rows ` . Use a ` while `
340
+ iterand, e.g. ` for i in 1..rows do { ...} ` will run over all integers from 1 to ` rows ` . Use a ` while `
357
341
statement to repeatedly execute a code block until the condition does not hold anymore, e.g. `while (c <
358
- niter && delta >= tolerance) {...}` will repeatedly execute the commands in curly braces until one of the
342
+ niter && delta >= tolerance) do {...}` will repeatedly execute the commands in curly braces until one of the
359
343
two conditions turns false."
360
344
::::::::::::::::::::::::::::::::::::::::::::::::
0 commit comments