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

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


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 the keywords break and continue. 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. So what I’ve done here is create a little program that reports the temperatures are cool +
whatever temperature is, and then adds one to the temperature. If the temperature becomes too hot it prints out the temperature is not
comfortable and stops. So we started it at 21 Celsius and
while it’s less than 30 Celsius we add one degree. Let’s see what
happens. And as you can see it prints out every temperature,
temperature is a cool 21 temperature is a cool 22, all the way up to temperature is a cool 29. When it hits 30, it says
temperature is not comfortable, and stops the while loop. Let’s see what
happens if we add a bad condition. Let’s assume that maybe our temperature started out as 30. So in this case we’ll hit the while loop where the temperature actually equals 30 and we’ll see what happens. And it just prints out temperature is not comfortable. Nothing inside the while loop block was run. Now what happens if we decided that we still started 21 but we never increment the temperature? What happens in that case? Well, we run it and as you can see it just keeps printing temperature is a cool 21 and its not stopping. It’s in an infinite
loo. To stop the process running, we have to hit the stop currently executing build button.
And that will stop the Maven part, but you probably have to go and kill the
process on your computer. A do-while statement says almost exactly the same thing. Do this
Java block while the statement is true. It starts
with the keyword do. Immediately following the keyword is the Java block to loop. After the Java block is a keyword while. And finally after the while keyword, we
specify the condition to continue looping. This is all
terminated with the 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. For a do-while statement, our code looks almost identical. We start with the do. Then we have our Java block. And then we do it while temperature is
less than 30. So starting at 21, let’s run it and see what happens. And again it prints out temperature is a cool 21 down to 29, and then says the
temperature is not comfortable. Now earlier we changed it to where we had a temperature of 30. In this case, our temperature has
already met the criteria in our condition. So let’s see what happens here when we run it. And notice it prints out temperature is
a cool 30, and then it prints out temperature is not comfortable. So with the do-while, it ran this at least
once before checking our condition. That’s the difference between the do-while and the while loop. Do-while runs at least once before checking. While checks before
running. And again finally let’s see what happens if we do not increment our value. Set it to 21. We run it, again we have to kill the
process and exit out of it. You should easily spot the difference. When we ran the while statement, if the
conditionals 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. 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 a lot 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. Sometimes you want to stop the current iteration of the loop, but 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. Qkay let’s take a look
at the difference between continue and break. Okay first let’s put in something like if temperature equals 23, break. So let’s run it and see what happens. What happened is we went 21, 22, 23, and then it broke out of the loop. We didn’t even get to 30. Okay let’s change our code to look like
this. We’re gonna increment the temperature at the beginning of our loop. If the temperature is 23 we’ll just continue, otherwise we’ll print
out the temperature is a cool whatever the temperature is. Let’s see what happens. Run it and you can see that 23 is skipped over, but we continue
looping up until 30, and then we break out of our while loop. So in this case continue continued with
looping, it just stopped running the Java block for 23. So 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. Hey thanks for watching the video. There is a quick quiz for this on DeegeU.com if you would like to gauge how much you learned. If you like the videos you are seeing, please let me know by liking the video and hitting the subscribe button for the DeegeU channel on YouTube. I’d really
appreciate that. If you have concerns or questions please leave them in the
comments below, or on DeegeU.com. There is a poll on the front page of DeegeU.com so you can let me know the topic that is covered next. Thanks for watching, and see you in the next video.

14 thoughts on “Fast guide to the Java while loop, break and continue – 019”

  1. OH MY GOD ARE YOU Ken Domik? I'm not even kidding, I legitimately can't tell… If you don't know who he is look up KBDProductions on YouTube to see your doppelgaenger haha

  2. Hey there Deege U I really enjoyed this video but I am curious on how to re print a ASCII table from 0 – 127 with a user input between 0 – 10, depending on user input it would repeat as many times as the user given variable. so for example

    if the user types in 1it would say "hello" once where if they typed in 5 it would print "hello" repeatedly for a total of 5 times before stopping I already have everything printing and the user input but I am unable to figure out the repetition part and was just curious on how a return statement would work in a for loop

  3. this is the most helpful java tuts i've ever watched. Easy to understand, straight to the point. kudos to you sir. worth subscribing.

Leave a Reply

Your email address will not be published. Required fields are marked *