1
master:
   switch(chipType)
   {
      case "ICs":
         for (var i = 0; i < ICs.length; i++)
         {
            if (ICs[i].name == chipName)
            {
               outField.value = ICs[i].price;
               break master;
            }
}

can anyone explain to me what is master doing in the above code snippets?

I am referring javascript Bible book 7th edition

2 Answers2

0

The symbol master is a label. The break statement can take as a sort of "argument" a label, which must be on another statement that lexically encloses the break. The meaning of that is to "jump" (what used to be called "go to" basically) to the statement following the labeled statement.

That's useful here because without the label, the break would only apply to its enclosing for loop.

Any type of statement can have a label, but it's really only useful for statement types that may have a break statement. Note that although it's OK (but weird) to label a function declaration statement, it can't be used to "break out" of the function from a break statement inside the function. Probably obvious, as that's what return is for.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

When the "break" statement is used with a label ("master" in this case), it causes the statement immediately following the label (in this case the switch statement) to be exited, with program flow skipping immediately to the following statement. So "break master" here causes the program to stop executing code inside the switch statement and go directly to whatever statement (if any) comes directly after the switch statement.

Using "break" without a label causes the innermost for, while, do-while or switch statement (i.e. the most nested loop or switch containing the break statement) to be exited, with program flow skipping immediately to the statement following the loop. This is by far the more typical case of the break statement in Java code.