Chapter 9. Go with the flow, man! Controlling the flow of your app

 

This chapter covers

  • How to control the flow of your app
  • The if statement
  • The while statement

You learned back in chapter 5 that the flow of an application—how an application reacts to different events—can be changed when a user interacts with a control, such as by tapping a button. But there are other ways that the flow can be altered that don’t require user intervention like tapping buttons. This chapter introduces some of these ways.

9.1. Control your flow

Controlling the flow of your app is similar to controlling the flow of traffic in the real world. When you’re driving to school or work, you either know where you’re going or you’re following directions of some sort. You may know that you need to turn left at the stop sign, or go through the traffic light and turn right at the second traffic light. If you didn’t have directions or if your car only went straight, you wouldn’t get to where you needed to be and your ride would be pretty boring. In order to get to where you want to be, you’re controlling the direction or flow of your car. Applications are the same: they can be written to flow in a seemingly straight line, but that would be pretty uninteresting, wouldn’t it?

9.2. If you do that again, I’m going to...

9.3. If you do that OR if you... then I’m going to...

9.4. If you do that AND you do this, I will...

9.5. If you do this, else if you do this, else if you do this...

9.6. If you do that, otherwise...

9.7. Summary