Java For Beginners: While, Do While & For Loops (7/10)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the sixth video in this Java series for beginners in this tutorial we're going to be looking at loops loops is where Java starts becoming really dynamic and we can actually control the code that we're using and we can make it repeat itself and do different things on each iteration of it so the first thing we're going to be looking at is the while loop and the while look look looks like this you just write the word while and then you have the open normal brackets much like the if statement and then you have the spiking brackets like so you can see it looks exactly the same as an if statement except we were in the word while now the difference between this while a the while loop and a what we were doing with the if statement is with both of them take the boolean condition inside these brackets here except the while loop what it's going to do if this condition is true it's going to run the code inside here much like the if statement except it's not just going to run it once it's going to keep running it over and over again until this condition in here somehow meets a false so each iteration it's going to check the condition see if it's still true and if it's still true it's going to run it over and over again it's basically a repeating if statement so let's go ahead and write while true in here and then we're going to say system.out.print line and we'll write hello world in there and what this is going to do is it's going to say okay let's check the condition that can did the boolean condition is true obviously that is the condition is true so it's going to run the code here then it's going to check if the statements still true obviously it's still true and this will this true statement here would never become false because this isn't a variable it's just the literal value of true so basically what this program is going to do is it's going to infinitely print out a hello world to the console and we can see this if we hit run you can see the scroll bar here just going further and further down and this is just going to print out indefinitely until we go ahead and manually hit stop so obviously that's not really practical in an actual program because you usually you want it to stop at some point you don't want it to just be infinitely running so let's do a let's do an example here let's make an integer called a and we're going to set it equal to zero now the condition we're going to make we're going to say well a is less than 100 so that's our boolean condition whilst a is less than 100 we're going to print out something let's go ahead and print out the value for a here so a is obviously less than 100 so if we print this out we're just going to see zero printed out infinitely much like the hello world thing that's because this a variable is never changing now that we've introduced a variable into the condition of the while loop we can actually change the while loop and what it does so what we'll do is we'll print out the value of a each time and then we're going to add one to it and I haven't introduced this this concept yet so to add numbers to increment a number of the same amount you can say a is equal to a plus 1 for example and that's perfectly valid we're saying set the variable a equal to itself just plus 1 and that basically increments it 1 there's an easier way to do it than that we can simply just put 2 plus signs and then finish off with a semicolon this basically just says add 1 to a that's the simple way of doing it so now this well loop has actually become more dynamic because each iteration we're going to add 1 to a and each time it's going to check if a is equal to 100 so this loop is actually going to run a hundred times until finally when we're on the when a is equal to 99 we're going to add 1 to 100 then 100 isn't less than 100 which means that the loop will have finished let's go ahead and add a system dot out dot print line down here and then we'll just write loop finished so when we hit run here it did that pretty quickly obviously so we can see in the console we've printed out 0 1 2 3 4 etc and we've printed this out all the way up to 99 and then the loop finishes here so you can see how we can make our programs dynamic using these loops you can make it a bigger number printout obviously up to that amount there include all the numbers in between and you're not limited to adding just one you can add whatever amount so you can see how using these loops the programs are starting to become more dynamic so there's actually an easier way to do this thing we've done here for example while a number condition is met and then add one or add to add 3 each time and then do something there's actually an easier way to do that it's called a for loop so let's go ahead and we'll just remove this what we've done so far so we'll keep this variable a here equal to 0 actually we'll delete that a variable because what we're going to do is we're going to do we're going to sub the for loop the for loop looks like so we write 4 with the brackets and then do the spike brackets like that and then this the difference of the for loop is it doesn't actually take a boolean value in here the for loop needs to take three parameters each separated by semicolons so the first parameter it's going to take is either an existing variable and an initial value or we can initialize a variable inside the for loop so we can make an integer called a and set it equal to 0 then we'll put a semicolon that's the first condition we need to setup so the first condition inside the for loop is setting up a variable and making it equal to something the second condition inside a for loop is setting the condition for the loop to run for example in that while loop just now we said whilst a is less than 100 and then we did a is less than 1000 we can go ahead and write whilst a is less than 100 for example and then another semicolon and then the final parameter we have to do is not it's not necessary but obviously if we don't want the loop to run infinitely the final parameter is in what way we want to increment or decrement the value that we've initialized here so we're going to say a plus plus so this is our fully functioning for loop so the first thing we do is initialize a variable give it a value the second thing we do is give a condition for the loop to run and the third thing we do is increment or decrement the number so let's go ahead and just system.out.print line print out the value of a and you'll see we don't need to increment it manually because it's doing in the for loop each iteration of the for loop it's going to check for this condition here and then at the end of each loop at the end of the code running in here it's just going to increment it the way we've told it to so you can see it did the exact same thing but just in a different form of syntax here and there's four loops useful for when we start looking at arrays which we will do soon but for now this example is good enough we initialize a variable we give it a condition and then we can plus or minus values to it and you're not limited to adding one there's another way you can increment numbers if you just want to increment one you do the plus plus if you want to increment a certain value to it you can put plus equals and then a number and that's basically this plus equals here you can also do minus equals the plus equals basically says adds the number on the Left add the number of the right sorry to the number led to the variable on the left and if we run this here adding two at a time you'll see we only get the even numbers here because we're adding two at time obviously so that's how we do the for loop and there's one final loop going back to the first while loop that we had there's one final loop I want to show you and that is the do-while loop and the do-while loop is slightly different from the I loop the dowhile loop basically make sure the code inside the while loop is run at least once regardless of whether the condition is met or not so let's go ahead and make an integer a equal to ten and let's go ahead and set that do-while loop so we can say do with note the the word do doesn't need any parameters here we just have the spiked brackets but then at the end of the spiked brackets here we do need to setup the wild condition so after this ending spoke bracket we can write while and then give a condition so we can say while a is less than ten and then we can put a semicolon so you can read this as do the code inside the brackets here while this condition is equal to true so why is this one useful compared to the normal while loop or like I said it's because the code is run regardless of whether the initial condition was met or not so let's go ahead an add system.out.print line would write hello world let's remove this loop finish down here so as you can see if this was a normal while loop the condition a is less than ten would be false because a is equal to 10 and 10 is not less than 10 so if this was a normal while loop if this was up here for example let's go ahead and run that you can see that nothing appears because while this condition is met and this condition isn't met because 10 is not less than 10 this code never runs here so if we just move that back down there the do part of the loop basically ensures this code is run at least once before checking this it basically means the code is run and then the condition is checked as opposed to the condition checked and then the code is run so if we run this we'll see we get the hello world printed out so we say do the hello world statement here and now check if this statement is true and then it says okay this condition isn't met we're not going to do the loop again and then we move to the code down here so that is the prints between the while loop and the duo loop and also the for loop
Info
Channel: Codecourse
Views: 377,758
Rating: undefined out of 5
Keywords: java for beginners, While Loop, java, Do While Loop, learn to program, tutorials, learn java, learn to code, Java (Software), phpacademy, java tutorials, jre, codecourse, basic java, For Loop, install java, java runtime envionment, programming, web development, jvm
Id: 6djggrlkHY8
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Fri Jun 13 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.