Lesson 7Controlling Program Flow

In this lesson, we are going to begin to look at how we can control the flow of a program. Specifically, we look at how we can make decisions and change the flow based on those decisions. To do that, you will be introduced to conditional statements and boolean expressions.

WHAT CAN OUR CODE DO?

So far, our programs have simply executed from beginning to end, one statement after another. We've added some code that allows us to get input from the user, but even with that, our code executes in a straight line—the same way every time. You would think there has to be more that can be done than this, and you'd be right. There is more but, surprisingly, not too much more. Here is what we can do in code:

  • Execute statements one after another in a straight line
  • Make decisions to execute one block of code instead of another based on some criteria
  • Repeat a set of statements a certain number of times based on some criteria

That's it—there is no more. All programs are built from these building blocks. We've already covered creating code in straight lines. ...

Get Job Ready Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.