Java for Humans {Control Flow Statements: Loops}

Lincoln W Daniel
ModernNerd Code
Published in
8 min readJan 20, 2016

--

View Table of Contents | Download Supporting Code | Subscribe to ModernNerd Youtube Channel for Coding Videos | By Lincoln W Daniel

We’ve come a long way from the last time we learned about control flow statements in the Decisions chapter. Great work on making it this far. This chapter will cover loops, which allow you to execute blocks of code more than once.

In the Decisions chapter, we learned that Java executes our program line by line from top to bottom. To change that, we could use if statements to only execute a block of code when a condition is true and another block of code otherwise. That is the basis of control flow in Java. With decision-making statements, we can choose to skip some lines of code during execution, but what if we want to repeat a line of code multiple times while a condition is true? That’s where loops come into play.

Why Loops are Important & Useful

Loops in Java allow us to repeat some of our code a fixed number of times or so long as a condition remains true. There are three basic forms of loops in Java, but we will only be focusing on two of them here because they are the most important and useful ones for beginners. We will learn about the for statement and the while statement. Both statements allow us to loop over blocks of code so long as a condition is true, but they do so a little differently from one another.

For Loop

--

--

Lincoln W Daniel
ModernNerd Code

Chief Bull @ BullAcademy.org ® Elevating writers @ ManyStories.com. Author @JavaForHumans Ex: Editor in Chief MarkGrowth (acq.), Engineer @Medium @GoPuff