Jumping to the more hard-to-understand part. And in the final lace is return to the beginning and even that will be explained, this aproach is needed for that code, of other way is almost an empty exercise.
This is for JS and not of the particular nature of that code that introduces an innovative subject, So console.log() you expect that is written '...' and + but is write '...' and ,
Is nothing difficult but until now isn't something that was used for the 1st time, like now you give up: in some situations, you can use the (,) that is you are in the same comand, like this:
you can write:
let x = 9;
let y = 12;
or:
let x= 9 , y = 12;
So I used a comma, is that all the two are 'let' so is a comma and not a semicolon if I use a semicolon is two statements that one has nothing about with the first. So isn't hard to understand what is written in the console.log, I could use 2 console.log one for the string and the other for the x and z but using a comma one console.log was enough.
the 2nd:
We have 2 while one is near the label(the innovating subject) and the second is inside of the first while, is very need to program you to understand the hierarchy of the commands, if there has a hierarchy or if there is not.
So the inner while will run all the time till the nine and the else if (the only if and if ilse are inside the inner while) so the control is operated inside the inner while.
So every time the program will run, and will look if z = 10 and if the x = 10 that only gonna happens when even the outer while be accomplish, so for 9 times the outer will run the inner 9 times for each outer one is nine and the inner is 9x9.
So the else if will kick the control to the top when the z came to 10 but will be printed only till 9, because when be ad the 10 that will kick the operation before be printed the console.
But why one if is for the outer and the other is for the inner while? Because the if is write:
break lavelCancelLoops;
so that case is a break that operate the outer while that has the same label, so is a break that stops and thinks about, and the other is one that doesn't think really stop but how is inside the inner while will kick for the top and not for out of the outer while.
So the label do that make the 1st while controlled for the control mechanism that is in the inner while.
Probably without the label, we gonna have to write other code different and with a result that could be close but would be more common to have a result little different.
let x = 0;
let z = 0;
labelCancelLoops: while (true) {
console.log('Outer loops: ', x);
x += 1;
z = 1;
while (true) {
console.log('Inner loops: ', z);
z += 1;
if (z === 10 && x === 10) {
break labelCancelLoops;
} else if (z === 10) {
break;
}
}
}
Rute Bezerra de Menezes Gondim