Lesson 8Looping Program Flow
In this lesson, we continue to learn how to control the flow of a program. In Lesson 7, “Controlling Program Flow,” we learned how to control flow through making decisions. In this lesson, we'll expand upon that to learn how we can use our code to repeat ourselves using looping expressions.
LOOPS
In the previous lesson, you learned about controlling program flow using conditions. These conditions allowed you to branch your code flow based on decisions made within the code. In today's lesson we continue learning about how to control the flow of our programs by using loops.
Loops are absolutely critical in any nontrivial program. They allow you to repeat a sequence as many times as you need, or sometimes you want to just keep going until a condition is met.
Should your phone ring five times before it goes to voicemail? There's a loop for that. Want to process payroll checks for all employees? You just keep going until there are no more employees. Well, there is a loop for that, too. The two examples shown in Figure 8.1 encapsulate the key question we need to ask before creating a loop:
- Do I know in advance how many times to loop?
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.