How to describe the execution of the loop for Javascript in Portuguese?

Asked

Viewed 52 times

2

I am a beginner in programming, and I am in the instruction module of the course to which I study and I came across this code below in the double instruction of for. I understood in part how this instruction is working (debugging), but how I could write in the outline of my studies this structure?

for (let i = 1; i <= 3; i++) {
  let linha = '';
  for (let j = 1; j <= 3; j++) {
    linha += `[${i}, ${j}]`;
  }
  console.log(linha);
}
  • opa Luiz Elipe was already editing more you edited there for me, thanks.

1 answer

2


Keep in mind that the code itself is already the most suitable description you can have.


Translating this code, in Javascript:

for (let i = 1; i <= 3; i++) {
  let linha = '';
  for (let j = 1; j <= 3; j++) {
    linha += `[${i}, ${j}]`;
  }
  console.log(linha);
}

For one of the infinite translations in Portuguese, we could do something like:

  • While i is less than or equal to 3 (adding 1 to i after each iteration), do:
    • Declare line as empty string.
    • While j is less than or equal to 3 (adding 1 to j after each iteration), do:
      • Assign to line the result of concatenating the strings: "[", i (string), ", ", j (string) and "]".
    • Invoke the function console log. passing as first argument, line.

It is one of the infinite possibilities of translation.


Note that although the functioning has been correctly translated from Javascript to a structure in Portuguese, assuming that we only have the Portuguese version of the algorithm, we would not be able to assume, with certainty, the syntactic structure of the original code. We could only reproduce the logic of the code.

Of course, taking the translation literally, we could end up using a for and come up with something very similar. But since, strictly speaking, the Portuguese language cannot correctly express all the syntactic structures of Javascript, we could end up writing something like:

let i = 1;
while (i <= 3) {
  let linha = '';
  let j = 1;
  while (j <= 3) {
    linha += '[' + i + ', ' + j + ']';
    j++;
  }
  console.log(linha);
  i++;
}

Or, if we only followed the functioning of the expressed algorithm, we could end up arriving at something even more different.


I ended up going beyond what I needed, but I think it’s important to highlight these points. In short, if the syntactic structure of the code is not important, translating it into a "human language" (such as English) may be a reasonable option depending on the situation. However, if this structure is important, writing the code in the original language is the only option I see as ideal.

As a curiosity, the Ecmascript standard (the specification that governs Javascript) does not describe the algorithms of built-ins of the Javascript language! A "English" version of the algorithm steps is used so that each implementation can assemble the code that suits it, looking for, for example, optimizations - that go, most of the time, beyond the scope of a pattern like this.

See for example the method specification Array.prototype.forEach in the section § 22.1.3.12.

  • 1

    Better explanation than this does not have another!!! Thanks Luiz... abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.