Looping with loops
It would be completely reasonable to ask what loops have to do with programming, but they are exactly what the name implies. They are a way of performing the same part of the code more than once, or looping over the same part of code, but potentially for a different outcome each time.
This can simply mean doing the same thing until the code being looped over prompts the loop to end. It could prompt the loop after a predetermined number of times as specified by the loop code itself. It could also prompt the loop when a predetermined situation or condition is met. Or there could be a combination of more than one of these ways of prompting the loop to end. Along with if
, else
, and switch
, loops are part of the Java control flow statements.
We will look at all the major types of loop that Java offers us to control our code, and after we have looked at methods, we will use some of them to implement the enhancements to our math game. Let's proceed to our first type of loop...