2

I'm looking at the following piece of code in which the for loops don't have bracket. I have a hard time figuring how that's supposed to work. I come from a background in C#, Java and some C++. All those language tend to be stricter than javascript regarding brackets usage.

function StupidMesh(volume, dims) {
  var vertices = [], faces = [], x = [0,0,0], n = 0;
  for(x[2]=0; x[2]<dims[2]; ++x[2])
  for(x[1]=0; x[1]<dims[1]; ++x[1])
  for(x[0]=0; x[0]<dims[0]; ++x[0], ++n)
  if(!!volume[n]) {
    for(var d=0; d<3; ++d) {
      var t = [x[0], x[1], x[2]]
        , u = [0,0,0]
        , v = [0,0,0];
      u[(d+1)%3] = 1;
      v[(d+2)%3] = 1;
      for(var s=0; s<2; ++s) {
        t[d] = x[d] + s;
        var tmp = u;
        u = v;
        v = tmp;
        var vertex_count = vertices.length;
        vertices.push([t[0],           t[1],           t[2]          ]);
        vertices.push([t[0]+u[0],      t[1]+u[1],      t[2]+u[2]     ]);
        vertices.push([t[0]+u[0]+v[0], t[1]+u[1]+v[1], t[2]+u[2]+v[2]]);
        vertices.push([t[0]     +v[0], t[1]     +v[1], t[2]     +v[2]]);
        faces.push([vertex_count, vertex_count+1, vertex_count+2, vertex_count+3, volume[n]]);
      }
    }
  }
  return { vertices:vertices, faces:faces };
}
Driky
  • 197
  • 12
  • 1
    Possible duplicate of [A for loop without any {}](http://stackoverflow.com/questions/11008030/a-for-loop-without-any) – tanjir Apr 10 '17 at 15:46
  • 1
    Possible duplicate of [Are curly braces necessary in one-line statements in JavaScript?](http://stackoverflow.com/questions/4797286/are-curly-braces-necessary-in-one-line-statements-in-javascript) – austin wernli Apr 10 '17 at 15:47

2 Answers2

2

If the loop or the if block will have only one statement we can always get rid of the brackets.

So to explain the following code:

for (x[2] = 0; x[2] < dims[2]; ++x[2]) //There will be only one statement which is the next for loop
  for (x[1] = 0; x[1] < dims[1]; ++x[1])  //There will be only one statement which is the next for loop
    for (x[0] = 0; x[0] < dims[0]; ++x[0], ++n) //There will be only one statement which is the if statement
      if (!!volume[n]) { //There will be multiple statements, so we use brackets
        for (var d = 0; d < 3; ++d) {
          var t = [x[0], x[1], x[2]],
            u = [0, 0, 0],
            v = [0, 0, 0];
          u[(d + 1) % 3] = 1;
          v[(d + 2) % 3] = 1;
          for (var s = 0; s < 2; ++s) {
            t[d] = x[d] + s;
            var tmp = u;
            u = v;
            v = tmp;
            var vertex_count = vertices.length;
            vertices.push([t[0], t[1], t[2]]);
            vertices.push([t[0] + u[0], t[1] + u[1], t[2] + u[2]]);
            vertices.push([t[0] + u[0] + v[0], t[1] + u[1] + v[1], t[2] + u[2] + v[2]]);
            vertices.push([t[0] + v[0], t[1] + v[1], t[2] + v[2]]);
            faces.push([vertex_count, vertex_count + 1, vertex_count + 2, vertex_count + 3, volume[n]]);
          }
        }
      }

That's because each for and if blocks are considered like a one line statement here.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thank you, i knew you could do this with `if` statement but didn't know about that one. – Driky Apr 10 '17 at 16:22
0

The syntax is th following:

while (expression) do statement

statement must be simple, but if you want more than one statement to be executed, you should use curly braces: {}. In this case statements are grouped together and from syntax point of view are treated as a single statement:

while (smthIsTrue) {
    console.log("hi!");
    console.log("there're many statements grouped with {}");
}

The same fact is true about such statements as for... and even if-statement:

if (smthIsTrue) {
    statement1;
    statement2;
    ...
    statementN;
}

I'd recommend to carefully choose when and why you use or don't use {}. When you explicitly use {} you avoid some mistakes. Look:

if (smthIsTrue)
    sayHello();
informUsers();

informUsers(); will run anyway even if you suppose it to run if smthIsTrue == true. Why? No curly braces, and only the next single statement is executed: sayHello();

The following piece of code works too! if(expression) ;

;

Is an empty statement so to js it looks correct. But how anyone else understands whether you've made a mistake or specially put this code somewhere? The best approach is to comment such a place so noone has any other way to understand the code:

if (youCanDoNothing) /* There shouldn't be anything, please dont' treat it as a mistake */;

In such a way if you look at the code in some time after you wrote it you have no chance to forget that you ommited smth specially.

Dzmtrs
  • 426
  • 3
  • 5