6. Loops: Doing Things Over and Over
Learn Loops: Doing Things Over and Over through beginner-friendly explanations, working GOCO examples, and their expected output.
6.1 Basic Loop
Loops are a great way to repeat yourself without writing the same line twice. A simple Loop will keep going as long as its rule is True.
goco
Number count = 0.Loop (count < 3) {Displaynl(count).count = count + 1.}
6.2 The Till Loop
You can write all loop instructions in a single line using Till. You give it a start, a rule to end, and a way to climb up.
goco
Loop (Number step = 0 Till step < 3, step++) {Displaynl("Walking step: " + step).}
6.3 Stopping Early
You can jump out of a loop at any time using Break. If you want to skip the current step but keep looping, use Continue.
goco
Loop (Number i = 1 Till i < 5, i++) {If (i == 2) {Continue.}If (i == 4) {Break.}Displaynl(i).}