Fast guide to the Java while loop, break and continue – J019

by Jun 12, 2015




DeegeU Java Course

“The fast guide to Java while loop, break and continue” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

  1. Try writing different loops with the Java while loop, break, continue statements
  2. Create at least one loop as both a while and do-while loop
  3. Experiment with the placement of breaks and continues in your code
  4. Try creating errors in your program and compiling it

Transcript – The fast guide to Java while loop, break and continue

How do I run a Java code block until some later condition?

Growing up my family used to travel by car for vacations. Going from Phoenix to Denver could take 15 hours by car, or as it’s know by all kids, FOR-EVER. My job was to stay in the back seat, not pick on my sister, and quietly keep myself busy until we reached our destination. There’s a way to make Java programs do something like this. The program for me in the car would be something like, while not in Denver, keep busy. In this lesson I’m going to show you how to continually run a Java block, until some later condition using the while-loop.

We are going to look at control statements that cause our Java application to repeat, or loop. In this lesson our goals are to understand the while and do-while statements. We need to understand the difference between the two. We’ll touch upon infinite loops, and we will learn the difference between the keywords break and continue.

Fast Guide to Java While Loop w/ Break and Continue - Free Java Course Online - DeegeU

The Java while loop

A while statement says do this block while the condition is true. It starts with the keyword while. The while loop has a condition immediately following the keyword. The condition is tested each time it runs the Java block, but it will not stop repeating the Java block until the condition becomes false. So if a is 6, and a never changes, this will repeat the block forever. On the other hand, if a is 4, the block will never run. Typically there is code in the Java block that causes the condition to change from true to false to stop the loop. For example the condition might be while there are more words to print, run the block. When you run out of words, stop. Let’s look at the while loop in code.

  • demonstrate the while loop
  • demonstrate what happens when the condition isn’t met
  • demonstrate what happens when the condition is never false

A do-while statement says almost exactly the same thing. Do this Java block, while this statement is true. It starts with the keyword do. Immediately following the keyword is the Java block to loop. After the Java block is the keyword while. And finally after the while keyword, we specify the condition to continue looping. This is all terminated with a semicolon. That’s different from the other while loop. There was no need to terminate a statement, since it ended with a Java block. Again the condition tells Java when to stop repeating this Java block. Let’s play with the do-while in code.

  • demonstrate the do-while loop
  • demonstrate what happens when the condition isn’t met
  • demonstrate what happens when the condition is never false

You should easily spot the difference. When we ran the while statement, if the condition was false the code never ran. The do-while statement ran at least one time, even if the condition started as false. Most of the time people use the regular while loop. The do-while loop is rarely seen in Java code. Now we saw what happens when the condition never changes to false? Like if you forgot to increment the value.

An Infinite Loop - Free Java Course Online - DeegeU

An Infinite Loop

It runs forever. Till the end of time…

or until we get bored as we demonstrated earlier and just control C the program. Of course that’s not a good plan. We can’t expect a user to control C every time they run the application. You never want to have an infinite loop. You always want a way out of the loop.

We saw the keyword break in the last lesson. In the switch statement, break stopped a case from falling through. Breaks cause a Java block to stop executing, and move back to the enclosing block. It also stops the while loop. As far as Java is concerned, once it hits the break we’re done with looping. When Java reaches the break statement, it “breaks” out of the loop. Any code inside the block after the break will not be run.

If we moved the break to the top, the blue Java block would never run. The break stops it.
Usually there is some test inside the while loop. If you put the test at the beginning of the loop like this, it’s the same thing as putting the test inside your condition.

So this is saying the same thing.

Putting the test at the end of a while statement like this, is functionally the same as the do-while loop. That’s why you don’t often see do-while loops. You can just do this. A Do-While is more succinct, but this seems more familiar.

If you have true in your condition, you usually have something like this somewhere inside your while block. An example where you might do this is a game loop. You keep running the game until you run out of lives. Then you break out to a title screen before you render the next frame in your game.

Break and continue - Free Java Course Online - DeegeU

Continue stops the current iteration of your loop

Sometimes you want to stop the current iteration of the loop, but you don’t want to stop looping. Continue stops the current loop, and moves to the next iteration of your loop. Assume a equals 5 here. We’d run this block, but the second block would be skipped. It would continue looping. If it was a break, we’d stop looping. Let’s try this in code to see what’s happening.

  • demonstrate continue in while and do-while

To recap break and continue, both will break out of your current iteration of your loop. What happens next is the difference. Break breaks you out completely. You hit a break, and you’re done looping. Continue will just move on to the next iteration of the loop.

So that’s the while loops. These are great for times where you don’t know the total number of loop iterations. Next we’ll complete our look at Java fundamentals with the for-loop. Then we’ll move on to classes. See you in the lesson.







Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This