====== 1.3 Named Loops Proposal ====== [[http://forum.lolcode.com/viewtopic.php?pid=2547|Discussion ]] ==== Overview ==== For maximum flexibility this proposal separates the loop declaration from the loop-exiting keywords, which may appear //anywhere// and in //any number// within the loop. The simple label declaration of the 1.2 is extended to form the basis of other structures. ==== Named Loops ==== A named loop is opened with ''IM IN [YR]'' and closed with ''IM OUTTA [YR]'' Anything declared within a named loop is not accessible outside it. Named loops may be nested, but may not overlap. IM IN YR HOUSE IM IN YR BED IM OUTTA YR BED IM OUTTA YR HOUSE is ok. IM IN YR HOUSE IM IN YR BED IM OUTTA YR HOUSE IM OUTTA YR BED is not ok. ====Identifiers==== An identifier may be an already declared variable. or object. If the latter, any reference to the object within the scope need not be prefixed by the object name; the scope acts as a WITH block. If the identifer is preceded by ''YR'' then it is declared using the same rules as ''I HAS A'' except that its scope is confined to the block. For example: IM IN YR BED ITZ "COMFY" or IM IN YR BED ITZ LIEK MAHBED are both o.k. ====Execution==== The statements in a named loop, apart from the initial declaration, are repeated indefinitely until explicitly exited. If a loop is declared outside a function declaration, none of statements are executed until the scope is closed. IM IN YR BED ITZ "COMFY" VISIBLE BED BED R "HARD" IM OUTTA YR BED Will print "COMFY" on the first iteration and "HARD" repeatedly thereafter. ====Exiting a Loop==== ''GTFO'' exits the innermost named loop unconditionally. ''GTFO '' exits the identified outer loop. This form is also needed to exit a loop from within a case statement. Example: IM IN YR HUNTIN GIMMEH ANIMAL ANIMAL, WTF? OMG "Hedgehog" VISIBLE "Prickly!!", GTFO OMG "Dog" VISIBLE "Halp!!!", GTFO HUNTIN OMG "Maus" VISIBLE "NOM NOM NOM", GTFO OMGWTF VISIBLE "Wat iz Dat?" OIC VISIBLE "Still Huntin!!" IM OUTA YR HUNTIN ====Conditional Exits==== WILE exits the innermost named scope if is FAIL. It is equivalent to ''NOT , O RLY?, YARLY, GTFO, OIC''. For example, if you have declared an object //filezorz// that responds to the methods: filezorz!!moar BTW return WIN if not EOF filezorz!!gimmeh BTW return next line IM IN YR zorz ITZ filezorz WILE zorz!!moar process zorz!!gimmeh IM OUTTA zorz TIL exits the innermost named loop if the current value of the loop variable is equal to . It is equivalent to ''WILE DIFFRINT AN ''. This is most useful for counted loops: IM IN YR INDEX ITZ 0 BTW counting up TIL SIZEOF ABUKKIT process ABUKKIT!!INDEX UPPIN INDEX IM OUTTA YR INDEX IM IN YR INDEX ITZ SIZEOF ABUKKIT BTW counting down TIL 0 NERFIN INDEX process ABUKKIT!!INDEX IM OUTTA YR INDEX Rationale: ''WILE''// would be sufficient on its own but counted loops are common, and comparisons in LOLCODE are verbose. Testing for equality is easily understandable, works both counting up and down, and avoids the common beginner's iterating over a zero-based range once too many. It doesn't work for increments other than one, but they are much less common, and can be expressed using// ''WILE''.