Loops usually keep going until they reach their natural end - either a condition changes in a while loop, or the maximum count is reached in a for loop.
But sometimes there might be a situation where you want to change the program flow from within the loop. You can use break and continue for that.
A break statement causes the program to immediately exit a loop.
Here is a simple computing quiz. It asks you to name 5 programming languages It is a very easy quiz, because it doesn't even check the answers:
PRINT("Name 5 programming languages")
FOR i = 1 to 5
language = INPUT()
PRINT(language)
ENDFOR
PRINT("Done")
Each time you type in the name of a language, the program prints out what you typed, and then waits for you to type in another. After 5 goes, the program stops.
But what if you get bored before the end of the quiz? It would be nice if you could type "quit" and make it all stop. That is where break come in:
PRINT("Name 5 programming languages")
FOR i = 1 to 5
language = INPUT()
IF language=="quit" THEN
BREAK
ENDIF
PRINT(language)
ENDFOR
PRINT("Done")
In this case, the program checks what you typed, and if you type "quit" it executes a break statement. This immediately jumps out of the loop, and goes straight to the next line after the loop (printing "Done").
It doesn't matter if it is the first time through the loop, the second time, or even the fifth and final time - as soon as the break statement executes, the loop finishes immediately.
{{% blue-note %}} You can use break with while loops or for loops. {{% /blue-note %}}
A continue statement does something slightly different. It ends the current step in the loop, and jumps back to the start of the loop, ready for the next iteration.
Here is an example which prints every prime number between 2 and 1000. It uses the isPrime function, which return true if the number is prime, false if not.
{{% orange-note %}} Most languages don't actually have an isPrime function, but it is easy enough to write one. {{% /orange-note %}}
FOR i = 2 to 1000
IF NOT isPrime(i) THEN
CONTINUE
ENDIF
PRINT(i)
ENDFOR
Here is what happens:
The end result is that the loop only prints out the prime number values.
{{% green-note %}} You can use continue with while loops or for loops. {{% /green-note %}}
Now you might have noticed that we can do the same thing without using continue at all - we can just use an if statement to only print if the number is prime:
FOR i = 2 to 1000
IF isPrime(i) THEN
PRINT(i)
ENDIF
ENDFOR
That works fine, in fact in some ways it is simpler than using continue. So why would you use continue? There are 2 reasons:
Here is a little trick which sometimes comes in handy. Consider this do-while loop:
DO
dostuff()
WHILE condition
Here is another way to do it:
WHILE TRUE
dostuff()
IF NOT condition THEN
BREAK
ENDIF
ENDWHILE
In this case, our while loop appears to runs forever (it loops while ever TRUE is true, which it always is). But if the condition becomes false, the break statement ends the loop.
Why would we want to do this? Imagine that there are several possible different reasons to end the loop. The infinite while loop allows us to handle each case in a different if statement - we can even do something different as we exit the loop. For example:
WHILE TRUE
dostuff()
IF userQuit THEN
PRINT("Program halted by user")
BREAK
ENDIF
IF timeout THEN
PRINT("You ran out of time")
BREAK
ENDIF
IF fatalError THEN
PRINT("There was an error!")
BREAK
ENDIF
ENDWHILE
Copyright (c) Axlesoft Ltd 2021