;------------------------------------------------------------------------------- ; ; ;-------------------------------; ; ; Loops ; ; ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,; ; ; Lots of sections in this part. ; Simply copy and paste the sections you want to try out. ; ;------------------------------------------------------------------------------- ; Repeat Forever ; ; If you run this program right now, it will be stuck forever. ; So make sure Debug mode is enabled in case you're crazy enough to ; press the Run button :) ; ; Syntax: ; ; Repeat ; (action) ; (action) ; Forever ; ;------------------------------------------------------------------------------- ; ; Let's make the endless loop at least a little bit more useful. ; Value = 0 Repeat Value = Value + 1 Write Value + " " If Value = 10 Then Exit Forever ; ; Try pasting the code above to a new program, and run it. ; You should see something like: ; ; 1 2 3 4 5 6 7 8 9 10 ; ; The Exit command will escape the currently executing loop. ; So the 'program cursor' will skip to the first (action) ; under the Forever statement. ; No, there ain't any at the moment :P ; ; The Write command prints something on the screen ; only without moving the cursor to the next line. ; ; Here's the same thing, only using a While..Wend. ; Value = 0 While Value < 10 Value = Value + 1 Write Value + " " Wend ; ; Syntax: ; ; While (condition) ; (action) ; (action) ; Wend ; ; Note the way the (condition) works. ; When the (condition) is equal to False, the loop is aborted. ; ; Back to the Repeat, but with a condition; a Repeat..Until. ; Value = 0 Repeat Value = Value + 1 Write Value + " " Until Value = 10 ; ; Syntax: ; ; Repeat ; (action) ; (action) ; Until (condition) ; ; Again, note the way the (condition) works. ; When the (condition) is equal to False, the loop continues. ; So actually the reversed of the While..Wend. ; Also reversed is the point where the condition is re-evaluated, ; e.g. where Blitz checks if the loop should be aborted. ; ; In anyway, whether you use While..Wend, ; Repeat..Until or Repeat..Forever is, again, ; just a matter of personal preference :) ; ; And finally.. ; For Value = 1 To 10 Write Value + " " Next ; ; Only 3 lines and it still does the exact same thing :) ; A For..Next loop: ; ; Syntax: ; ; For (variable) = (minimum) To (maximum) ; (action) ; (action) ; Next ; ; You can even specify a stepping value, ; although it has to be constant. (eh?) ; ; Example: ; For Floating# = 0 To 1 Step 0.1 Write Value + " " Next ; ; Because the stepping value has to be constant, ; you can't use a variable for a stepping value ; so you'll have to use another loop for that. ; Local SteppingValue = 3 ; Value = 0 Repeat Value = Value + SteppingValue Until Value > 10 ; ; Using a stepping value in a For..Next ; you can also count backwards. ; For Value = 10 To 1 Step -1 Write Value + " " Next ; ; If you try to count backwards without a stepping value, ; the actions in the loop simply won't be executed. ; For Value = 10 To 1 Print "You can put a lot of crazy stuff in here" Print "because it won't be placed on the screen anyway" Next ; ;------------------------------------------------------------------------------- ; ; There is one last type of loop. ; But it can turn your code easily into spaghetti. ; .Label Goto Label ; ; Oh no, another endless loop. ; You can use Goto to jump to a specific point in your program. ; A point, marked by a Label (note the dot). ; ;------------------------------------------------------------------------------- ; ; Even the most simple Goto structures can lead to spaghetti: ; Value = False .Test If Value Goto Done Else Goto Invert .Invert Value = Not Value : Goto Test .Done Print Value ; Value will equal 1 e.g. True. End ; ; Ah, well, the colon (:) means code glue. ; You can connect code together like that. ; ;------------------------------------------------------------------------------- ; ; The code glue doesn't always ; work the way you want it to though. ; And it can make reading code really hard. ; And you can even glue code without glue sometimes! ; Repeat Forever .Start Goto Start ; ;------------------------------------------------------------------------------- ; ; No, we're not done yet. ; Level 2, the nestanoids :P ; ; You can combine as many loops ; of the same (or different) kind ; as you want. ; For Outer = 1 To 2 For Inner = 1 To 2 ; <--- Next Next ; ; Can you tell me how many times ; the marked spot (with the arrow) ; will be executed? ; ; Here's the answer: ; Value = 0 For Outer = 1 To 2 For Inner = 1 To 2 Value = Value + 1 Write Value + " " Next Next ; ; Now let's add some maths. ; For Outer = 1 To 2 For Inner = 1 To 2 Value = Outer * Inner Next Next ; ; Can you tell me what Value will be ; each time the Inner loop is executed? ; ; Let's display it again. ; For Outer = 1 To 2 For Inner = 1 To 2 Value = Outer * Inner Write Value + " " Next Next ; ; I'll show you what happens in the program. ; ; (Outer) (Inner) (Value) ; 1 * 1 = 1 ; 1 * 2 = 2 ; 2 * 1 = 2 ; 2 * 2 = 4 ; ;------------------------------------------------------------------------------- ; ; To make it a little more challenging ; and not to mention more interesting, ; let's expand the range of and add ; a stepping value to the inner loop, ; as well as apply some glue. ; For Outer = 1 To 2 For Inner = 1 To 5 Step 2 Value = Outer * Inner Print Outer + " * " + Inner + " = " + Outer * Inner Next Next ; ; Which will result in: ; ; (Outer) (Inner) (Value) ; 1 * 1 = 1 ; 1 * 3 = 3 ; 1 * 5 = 5 ; 2 * 1 = 2 ; 2 * 3 = 6 ; 2 * 5 = 10 ; ; Okay, maybe not so interesting yet. ; But with some modification.. ; Print " | 1 3 5" Print "-+-------" For Outer = 1 To 2 Write Outer + "| " For Inner = 1 To 5 Step 2 Write ( Outer * Inner ) + " " Next Print Next ; ; Look! It's a grid! ; ; Remember that types of glue ; have a certain order ; in which they work. ; ; For example: ; Value1 = 10 Value2 = 10 ; Print "|" + Value1 + Value2 + "|" ; ; is different from: ; Print "|" + ( Value1 + Value2 ) + "|" ; ; You could do this: ; Result = Value1 + Value2 Print "|" + Result + "|" ; ; just to be sure ; all you are glueing ; is strictly text. ; ; But it's all up to you ;) ; Like, you could put brackets everywhere. ; Result = ( (Value1) + (Value2) ) Print ( ("|") + (Result) + ("|") ) ; ;------------------------------------------------------------------------------- ; ; Let's try displaying a 10x10 grid. ; For Y = 1 To 10 For X = 1 To 10 Write "*" Next Print Next ; ; Although, that looks more like a simple rectangle. ; ;------------------------------------------------------------------------------- ; ; So let's do something about that. ; For Y = 1 To 10 Print "+-+-+-+-+-+-+-+-+-+-+" For X = 1 To 10 Write "| " Next Print "|" Next Print "+-+-+-+-+-+-+-+-+-+-+" ; ; Ah, there we go. ; ; If you want to repeat text a number of times ; you can use the String command. ; ; Example: ; Print "Muhu" + String ( "ha" , 4 ) ; ;------------------------------------------------------------------------------- ; ; Now you can make the grid ; totally dependent on one value. ; Const GridSize = 5 ; For Y = 1 To GridSize Print String ( "+-" , GridSize ) For X = 1 To GridSize Write "| " Next Print "|" Next Print String ( "+-" , GridSize ) ; ; If you're still a little bit ; confused by all of this, ; don't worry, the answers are coming =) ; ;------------------------------------------------------------------------------- ; ; Now, how about a checkerboard? ; ; Start simple. ; For Y = 1 To 10 For X = 1 To 10 Next Next ; ;------------------------------------------------------------------------------- ; ; Add some display. ; Print "+----------+" For Y = 1 To 10 Write "|" For X = 1 To 10 Write " " Next Print "|" Next Print "+----------+" ; ;------------------------------------------------------------------------------- ; ; Add some variables ; and a condition. ; Print "+----------+" For Y = 1 To 10 Even = False Write "|" For X = 1 To 10 Even = Not Even If Even Then Write "O" Else Write "·" Next Print "|" Next Print "+----------+" ; ;------------------------------------------------------------------------------- ; ; Add, move and modify code. ; Even = False Print "+----------+" For Y = 1 To 10 Write "|" For X = 1 To 10 Even = Not Even If Even Then Write "O" Else Write "·" Next Even = Not Even Print "|" Next Print "+----------+" ; ; And voila, a checkboard. ; What do you want more? :P ; ; Try fiddle around with the code ; and see what you can come up with :) ; ;------------------------------------------------------------------------------- ; ; Remember that you can combine any loops you want. ; Const WhatWeWant = 5 Local Done = False Repeat For Value = 1 To 10 Write Value + " " If Value = WhatWeWant Done = True Exit End If Next Until Done WaitKey End ; ; The complexity is starting to creep in. ; So many lines for such a small task. ; Your screen should read: ; ; 1 2 3 4 5 ; ; Allow me to explain what happens. ; The Repeat..Until would be the outer loop. ; And the For..Next the inner loop. ; Done is set to True and the inner loop is aborted when Value equals 5. ; The outer loop continues until Done is True. ; ;-------------------------------------------------------------------------------