Chapter 6. Repeating Blocks of Code with Loops

In this brief chapter, we will learn about Java loops that enable us to repeat sections of our code in a controlled manner. Loops in Java take a few different forms and we will learn how to use them all and throughout the rest of the book we will put each of them to good use.

In this chapter, we will look at:

  • while loops
  • do-while loops
  • for loops
  • How to draw the grid lines of the Sub' Hunter game (using loops)

This is the second to last chapter before we have completed the first game and move on to a more advanced project.

Decisions, decisions

Our Java code will constantly be making decisions. For example, we might need to know if the player has been hit or if they have a certain number of power-ups. We need to be able to test our variables to see if they meet certain conditions and then execute a certain section of code depending upon whether it did or not.

This chapter and the next will look at controlling the flow of execution of our game's code. This chapter, as the name suggests will discuss fine control over repeating sections of code based on predetermined conditions and the next chapter will look at branching to different sections of code, also based on predetermined conditions.

In the next two chapters, our code gets more in-depth and it helps to present it in a way that makes it more readable. Let's take a look at code indenting to make our discussion easier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.207