It seems like it's just now, but when I first started programming, I'm sure everyone was wondering how to use for statements and while statements properly.
It seems that the general literature in the world often says that ** for statements are used when the number of repetitions is clear **.
However, for me, this explanation didn't come to my mind.
As I was writing the program today, I finally found one point in myself about this difference, so I'll record it here.
Below, it is described in JavaScript.
class Iterator {
index = 0;
constructor(list) {
this.list = list;
}
next() {
return this.list[this.index++];
}
}
const iterator = new Iterator([1, 2, 3, 4, 5]);
Prepare the ʻIterator` class with the above implementation (actually, you can imagine the file list etc.). Now, suppose you want to describe the process of counting the number of elements while outputting all the elements. Initially, I wrote the following code [^ 1].
let count = 0, item;
while (item = iterator.next()) {
console.log(item);
count++;
}
console.log(`Quantity: ${count}`);
To make the code more readable, try rewriting it as follows:
let count, item;
for (count = 0; item = iterator.next(); count++) {
console.log(item);
}
console.log(`Quantity: ${count}`);
I've rewritten it ... This code is certainly shorter, but doesn't it feel very hard to read?
Perhaps the reason for this is that we
for (A; B; C) {}
Unconsciously when I saw the sentence
For A, while B, do C.
In other words, I think it's because I read ** A, C during B **. For this reason, when you apply the actual formula here, you will feel a strong sense of discomfort if it does not hold as a sentence.
If you want to go a little further, you need to be aware of the statement A, that is, the initialization formula **. As long as the word "for" (about =) is used, B, C, and the sentences in the block are bound by this A part even if you don't like it.
The strange thing about the for statement example above is that the continuation conditional expression and the statements in the block have nothing to do with count
, even though the initialization expression is used to initialize the variable count
. It was because there was no such thing.
Of the repetitions that can be expressed by the while statement, the ones that should be expressed by the for statement
I think it can be said that it satisfies.
To put it the other way around, as long as the above rules are met, there are things that can be written using the for statement even if the generally-known "number of iterations is known" is not met. What about the example below?
class Iterator {
index = 0;
constructor(list) {
this.list = list;
}
get currentItem() {
return this.list[this.index];
}
next() {
this.index++;
}
}
let count = 0;
for (
const iterator = new Iterator([1, 2, 3, 4, 5]);
iterator.currentItem;
iterator.next()
) {
console.log(iterator.currentItem);
count++;
}
console.log(`Quantity: ${count}`);
It doesn't feel too strange, right? ?? ??
[^ 1]: In JavaScript, if an index outside the range of the array is accessed, no error will occur and a falsy value of ʻundefinedwill be returned. For this reason, the
next method returns one element each time it is executed, and returns ʻundefined
when all are done.
Recommended Posts