From the course: Learning Java 11

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

While loops in Java

While loops in Java - Java Tutorial

From the course: Learning Java 11

While loops in Java

- [Instructor] Let's create a while loop in Java. In the previous lesson, we diagrammed a while loop to represent a song playing on repeat. With this diagram, while the current song is on repeat, we play the song again. If the current song is not on repeat when we check the while loop condition, we exit the loop and play the next song. If the user never takes the song off of repeat, then the current song will play forever. We only exit the loop if the user takes the song off of repeat. Let's jump to the code. The first thing we do is create a scanner to get us set up for user input. We also have a Boolean variable that represents if the current song is on repeat and it equals true. Now, for the while loop. We want to say while our song is on repeat, meaning isOnRepeat is true, we want to run a series of statements. To do this we'll use the keyword while and then our condition in parentheses. Then we'll use…

Contents