@@ -20,9 +20,14 @@ a given number of elements, the **_for-loop_** is what we want to use. The for-l
2020syntax:
2121
2222``` chpl
23+ // single-statement version
2324for index in iterand do
25+ instruction;
26+
27+ // multi-statement version
28+ for index in iterand
2429{instructions}
25- ```
30+ ```
2631
2732The * iterand* is a function or statement that expresses an iteration; it could be the range 1..15, for
2833example. * index* is a variable that exists only in the context of the for-loop, and that will be taking the
@@ -35,7 +40,7 @@ This `for` loop, for example
3540
3641``` chpl
3742// calculate the new temperatures (temp_new) using the past temperatures (temp)
38- for i in 1..rows do
43+ for i in 1..rows
3944{
4045 // do this for every row
4146}
@@ -47,10 +52,10 @@ this:
4752
4853``` chpl
4954// calculate the new temperatures (temp_new) using the past temperatures (temp)
50- for i in 1..rows do
55+ for i in 1..rows
5156{
5257 // do this for every row
53- for j in 1..cols do
58+ for j in 1..cols
5459 {
5560 // and this for every column in the row i
5661 }
@@ -62,10 +67,10 @@ follows:
6267
6368``` chpl
6469// calculate the new temperatures (temp_new) using the past temperatures (temp)
65- for i in 1..rows do
70+ for i in 1..rows
6671{
6772 // do this for every row
68- for j in 1..cols do
73+ for j in 1..cols
6974 {
7075 // and this for every column in the row i
7176 temp_new[i,j] = (temp[i-1,j] + temp[i+1,j] + temp[i,j-1] + temp[i,j+1]) / 4;
@@ -78,6 +83,17 @@ Note that at the end of the outer `for` loop, when all the elements in `temp_new
7883` temp ` with the values of ` temp_new ` ; this way everything is set up for the next iteration of the main ` while `
7984statement.
8085
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+
8197Now let's compile and execute our code again:
8298
8399``` bash
@@ -239,9 +255,9 @@ the job:
239255``` chpl
240256// update delta, the greatest difference between temp_new and temp
241257delta=0;
242- for i in 1..rows do
258+ for i in 1..rows
243259{
244- for j in 1..cols do
260+ for j in 1..cols
245261 {
246262 tmp = abs(temp_new[i,j]-temp[i,j]);
247263 if tmp > delta then delta = tmp;
@@ -337,8 +353,8 @@ The greatest difference in temperatures between the last two iterations was: 0.0
337353
338354::::::::::::::::::::::::::::::::::::: keypoints
339355- "You can organize loops with ` for ` and ` while ` statements. Use a ` for ` loop to run over every element of the
340- iterand, e.g. ` for i in 1..rows do { ...} ` will run over all integers from 1 to ` rows ` . Use a ` while `
356+ iterand, e.g. ` for i in 1..rows { ...} ` will run over all integers from 1 to ` rows ` . Use a ` while `
341357 statement to repeatedly execute a code block until the condition does not hold anymore, e.g. `while (c <
342- niter && delta >= tolerance) do {...}` will repeatedly execute the commands in curly braces until one of the
358+ niter && delta >= tolerance) {...}` will repeatedly execute the commands in curly braces until one of the
343359 two conditions turns false."
344360::::::::::::::::::::::::::::::::::::::::::::::::
0 commit comments