ECMA-262: Breaking Out of Nested Loop Control Structures

PHP: Optional Numeric Argument for break

My first dynamic language was PHP and with that came the break statement which ends the execution of a control structure (e.g. for, while or case). It also “accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.” This optional argument is beneficial when there are nested loops that need to be terminated prematurely without adding extra logic and without halting the entire script execution (e.g. exit).

In the following example the second/nested for loop will be terminated during the first iteration, but the outer-most loop will execute tens times.

…but in the next example it will break out of both for loops resulting in $i never incrementing from 0 to 1.

JS/AS: LabelledStatement (TL;DR)

What about ECMAScript Languages (ECMA-262)? I struggled with the absence of this feature for languages based on the ECMA-262 standard like JavaScript and ActionScript. These languages (as far as I knew) have the break statement which works just like it does in PHP, but without the optional numeric argument. This made stopping nested control structures a PITA.

Well, maybe if I RTFM I would’ve been acquainted with the LabelledStatement Statement and saved myself the heartache. This can be observed in section 12 of the ECMA-262 Langauge Specification, Edition 5.1. Basically the syntax goes something like this:

Note “theLabelName” on lines 1 and 3. This is the bread’n’butter. All that needs to happen is to label the control structure using the standard variable naming convention and post-fix it with a semicolon (:). This becomes the “calling card” of that specific construct. Lastly, the break statement has an optional argument where the previously established label can be applied.

Further Reading

See ECMA-262 5.1 Edition ECMAScript Language Specification, §12, p86.

Explode RGB Unsigned Integer into Individual R, G and B Values + RGBA

At irregular intervals I need to take a RGB uint – e.g. 0 is rgb(0,0,0) and 16711680 is red (#ff0000) and convert it to an unsigned integer and visa-versa.
I need this from time-to-time and always forget. That, and somehow I misplace the code or leave it behind with my employer. I will now forget no more thanks to “Splitting an RGB uint into its separate R G B components” question on stackoverflow and this post.


Here’s the run down (in AS3)…

Convert RGB to uint (decimal):

Convert uint (decimal) to RGB:


So after implementing some of this, I thought… "what about alpha channel support?" Well keep in mind, alpha is a graceful implementation/add-on and does not obstruct (or shift) the original data of the RGB value.

With this in mind, note that alpha is in the upper bit range (24); in hexadecimal format that is #AARRGGBB. It is doubly important to make sure that an unsigned integer is being used. With just normal RGB you can get away with a signed integer (−2,147,483,648 to 2,147,483,647) since you don’t need higher values, but you need unsigned for RGBA (0 to 4,294,967,295). For example, #FFFFFF for RGB merely equates to 16,777,215 in decimal format which is well under the 32-bit signed integer maximum (one-twenty-eight to be exact, think about it), however #FFFFFFFF for RGBA equates to 4,294,967,295 which is the exact maximum of a signed 32-bit integer (e.g. uint.MAX_VALUE).

Moral of the story: don’t use datatypes of “Number” or “int,” just “uint.”

Anyways, here’r the goods, going from uint (decimal) to RGBA (errr, ARGB?):

And going from RGBA to uint (decimal, base 10):

Further Reading