#3.3 Java Tutorial | While | For | Iteration Statement | Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] once we have done with this selection statement let's talk about betrayin not exactly iteration means when you want to repeat something repeatedly we can use iteration example what we'll do here is let's say I want to print a statement I want to print a statement like hello that's it so I can simply type system dot out dot println I can type hello there and if I run this code you can see we got the output as hello so let's say I want to execute this statement multiple times now so in shop only once let me type it five times so if you want to print hello five times you have to write this code five times right so if I write this five times now you can see we got five times hello here in the console window but this one issue here we have this statement repeated five times right and when you repeat any code it is called last code redundancy so when you wanna repeat any code it is called as code redundancy and code we tendency is a try so you have to make sure that you will not repeat any statement because if you repeat a statement you are doing a crime there how do you make sure that you print five statements without repeating the statement we can write hello only one and we can say I want to execute this statement multiple time so we can put that thing in a block so I want to execute this block multiple times but when you say multiple times how many times one two hundred 500 1000 so to specify the number of times it will get executed right so to specify that what we can do is we can specify some looping statement so you have to use a loop so it exert a loop a loop will loop will execute continuously so it will go it will execute this block it will execute this statement it will go back to the loop it will execute the statement it will come to the curly brackets it will go back here it will execute the block but when it comes to loop and you'll be just a concept the actual implementation of loop is with the help of while loop so either we can use a while loop or we can use a do-while loop so we have do-while loop otherwise we can use a for loop and then we have the advanced loop well as for each loop so now this forest loop is also called as enhanced for loop so what we'll do is let's replace this loop keyword let's go with wild now how while works is inside your while we can specify the condition so we can say what it is true so when you say well it is true it will execute this statement time's example if I on this port you can see just and I just continue printing it and how to stop that by clicking on this stop button you can stop it which is printing infinite times right I want to make sure that it is going finite so when you say you want to make it finite so let's I want to print only five times so you have to you need a counter who will count it so to use a counter we'll define a variable we'll say this is I the initial value of I will keep it as one it's my I value or while my I value is less than equal to five so if the value of I is less than five and then that's okay you can go with it so example when is when it comes to one one is less than five yes it will execute the block then same as - same goes for three same goes for four same goes for five what about about six as well so I become six it will check okay I six is not less than five that means it will not execute the block it will go out way to make sure that we are also incrementing the value of I so as we have seen we can also use I plus plus to show the increment so the initial value of I is 1 and then we are checking if I less than equal to five and then we are saying I plus plus my friend this code you can see the output is where we are getting hello five times so that's the magic of a loop where you want to execute a block for multiple times and we have to use three statements here so the first statement here will be called as initialization statement because this is the line where you're putting the initial value of I then this is the condition and this is your increment so basically when you want to use a counter for a loop we can use it three steps the initial value the condition and the increment or decrement operator looks cool right so this is how I loop now we have one more loop to look at we can also use do-while loop now what is difference between while and do-while let's say I want to execute this statement now is it just not depend upon the increment value I want to execute this statement multiple times so let's I am taking the input from the user and if user is giving the number like fifteen twenty any number I just have to take that number and I have to print it my user says five if I use assess and if I uses is fifteen I have to take the input from the user I just have to print it that's it nothing else but let's say if I user says hundred now if a number goes beyond 99 I don't want to print that I want to stop the execution now in that's in our you're taking the input from the user right at least once even if a user says hundred you are taking the input at least once so that means when you use a while loop even if your condition is false it will not execute this block but what do I look does is we can write do and while we're the condition is checked later even if a user in terms of 100 imagine we're to take an example of user input and then depend upon that I want to clean something this is not big this is not that example which we are talking about but let's say in that scenario we will be using a do-while because even if you are even if a user enters 100 it will execute the block at least once so if you want to execute the block at least once even if your condition is false we can just do one example if I than this code you can see this printing the same Strout statements five times but let's say if I make the value of I as 9 now a 9 is greater than greater than 5 right and even if I run this code it will execute hello at least once is because the condition is checked later now this concept is same as C programming so if you are coming from C and C++ you know what I'm talking about all right so this is happy this is what you do in C and C++ in Java also we can achieve the same stuff ok so that's your do window we can also use for loop here because in case of while or do-while what we do is we write in this 3 statement which is initial value condition and increment in 3 different line using a for loop what we can do is we can write everything in one lines example you can I can say int I equal to 1 then we can give a semicolon we can specify I plus plus so you can see I'm using one line but we have we do have three statements first statement this is second statement and this is third statement so we can write all this 3 statement in one line if you are using a for loop and if we run this code you are getting the same output we are printing hello five times so again we'll talk about while loop do I look at the hook to all the course there will be lots of conditions where you have to use this so that's how your the for loop works okay so when you have iteration you when you want to iterate from our values we can use a for loop now when to use a while loop when to use a do-while loop when to use a for loop so when you know that the starting point and the ending point always go for loop example we know that we have to start with one and we have to end that at 5:00 so we know it always go for for loop but let's say when you don't know the starting point ending point sometimes you know we have to iterate in loop depend upon some some other factors example you are creating a chat application and you want to take the in from the user continuously till the user saying says exit so if user is in putting a value like exit you need to make sure that you are closing the chat window so now you don't know right how many times our user will type the message how many times you will be chatting it all depend upon the exit keyboard as soon as the user enters exit you have to stop it so in the while condition you have to specify the condition that if user says exit then close the while loop otherwise skip it continuous so you don't know the number of times our user will type the message in that scenario all this before file do when to use do I look even if your conditions fall so you want to execute that statement once before do-while loop now we have one more which is for each loop we will be talking about 40s no once we talked about array so we have a concept of array so once we have done with add a concept we'll talk about for each because for the it is a special type of loop specially made for arrays and collections so once we have done with adding that will this fall it will make more sense there but remember when you talk about iteration or loops we have while loop we have do-while loop we have for loop and we have for each loop so that's it from this video that's how you use looping statements that's it thank you
Info
Channel: Telusko
Views: 119,911
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, for, while, do while, loops, iteration, statement
Id: oAl0umTfW4U
Channel Id: undefined
Length: 8min 5sec (485 seconds)
Published: Wed Jan 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.