Combining different control flow blocks
As you might have been able to guess, we can combine any of the decision-making tools such as if
, else
, and switch
within our while
loops, and the rest of the loops too. Take a look at the following example:
int x = 0; int tooBig = 10; while(true){ x++; // I am going to get mighty big! if(x == tooBig){ break; } // No you're not // code reaches here only until x = 10 }
It would be simple to go on for many more pages demonstrating the versatility of control flow structures, but at some point, we want to get back to finishing the game.
Now that we are confident with if
and else
, let's have a look at one more concept to do with loops.
Using the continue keyword
The continue keyword acts in a similar way to break
,...