#17 While Loop in Java — Transcript
Full transcript
- 0:00so in the last video we have talked
- 0:01about the need for Loops right and so we
- 0:04have decided that if you want to repeat
- 0:06something Loops can be a good thing and
- 0:09we also talked about there are different
- 0:10types of Loops right we have while loop
- 0:12do while loop for Loop in this video
- 0:13let's try to understand the first Loop
- 0:15which is while loop so we'll go over
- 0:18this go for the same statement here if
- 0:19you can see we are Printing High and
- 0:21again as I mentioned before ignore this
- 0:23x this is a suggestion given by your vs
- 0:25code uh this is what we are typing just
- 0:28high and what I want to do is I want to
- 0:30print this High multiple times so what
- 0:34we can do is we uh we can say Hey you
- 0:36know Java I want to print this multiple
- 0:39times Java will say okay
- 0:41the first thing you do is you put this
- 0:43in a block of course it's not compulsory
- 0:45when you have one statement but just to
- 0:47understand just to get started we are
- 0:48putting that in a block now and then of
- 0:51course we have to follow into
- 0:51indentation as well and we are doing
- 0:53that now what we can do is we can say
- 0:55Hey you know Java I want to run this
- 0:58five times so Java will say okay if you
- 1:01want to repeat you have to use a keyword
- 1:03called while now in English as well we
- 1:06normally use while to say repeat
- 1:08something or while it is true do that so
- 1:11here we can say while it is what in this
- 1:15bracket you have to mention is it true
- 1:17or false now basically that's how Wild
- 1:19works so if you say true it basically it
- 1:22will just keep printing this High
- 1:24because we are saying true it says okay
- 1:26while I'm true I will keep printing it
- 1:28so that's why if you just run this code
- 1:30by just by mentioning the
- 1:33while
- 1:35you can see we are getting high I just
- 1:37kept printing okay it's it's not
- 1:39stopping if you can see if I try to
- 1:40scroll down it is continuously happening
- 1:43and maybe it will uh fill my Ram it will
- 1:47crash okay I don't think so but let me
- 1:49stop this execution otherwise there will
- 1:51be an issue so what we can do is I don't
- 1:53want to continuous this for a long time
- 1:56I want to do that for a particular
- 1:58condition maybe I want to say I want a
- 2:01counter which will start from one and it
- 2:03will stop at five oh when I say counter
- 2:06it means I need a variable well I can
- 2:08store the value so what we can do is
- 2:10here I can just come up and I can say
- 2:12int I is equal to let's say one now most
- 2:16of the time when you use Loops you will
- 2:17be using a variable name I it's not
- 2:20compensative we can use any variable
- 2:21name but it makes sense right because
- 2:23normally when you go inside the while
- 2:26you are executing something you are
- 2:28changing the values of the eye
- 2:31you basically increment it right so it
- 2:34makes sense to call it as a iterator
- 2:35which will iterate so I iterator makes
- 2:39sense and that's why most of the time
- 2:40you say i j but as I mentioned you can
- 2:43use any variable of your choice so we
- 2:45are using I here which is I is equal to
- 2:471 and then what we can do is every time
- 2:51you run this loop after Printing High
- 2:53you can just increment the value of I
- 2:56oh but then how do we increment and we
- 2:58have seen that right we have a concept
- 3:00of I plus plus so I plus plus will
- 3:02simply increment the value of I okay
- 3:05that makes sense and oh how do I check
- 3:10for the condition see it makes sense now
- 3:12because I is 1 and every time you run
- 3:15this Loop the in the I value will
- 3:16increase right so if you run this while
- 3:18for let's say five times it will
- 3:20increment the value of I let me show you
- 3:23what I will do is I will print hi as
- 3:25well as I will print uh maybe you can
- 3:28you can print both the things I say on
- 3:30the same line you just have to give a
- 3:32space after high and I can give a plus
- 3:34symbol so when you want to concatenate
- 3:36two different things High and a number
- 3:39we can use a plus operator yeah plus
- 3:41Works in that way it works for addition
- 3:44as well as it works to concatenate two
- 3:46things
- 3:47and here I'm writing I now let's see
- 3:51what's the output is again I have not
- 3:52changed the condition so it will print
- 3:54infinite times
- 3:55but I just want to see what happens if I
- 3:57compile this code
- 3:59and run you can see the value of I is
- 4:02incrementing continuously
- 4:04and you can see it so fast depend upon
- 4:06your machine speed as well and let me
- 4:07know when you try on your machine how
- 4:10fast it is so what I want to do is I
- 4:12want to stop this somewhere so we have
- 4:14to apply a condition how do I stop it so
- 4:16we have to say if I is less than equal
- 4:19to 4 so what it will do is it will check
- 4:22for the condition remember we have
- 4:23talked about this in relational
- 4:24operators it will continuously check for
- 4:27the condition so what how it runs is
- 4:29initially it will set the value of i as
- 4:311 and then in the while loop it will
- 4:33check if the value of I which is 1 is
- 4:37less than 4 yes it will continue it will
- 4:40go inside the loop it will print high it
- 4:43will also print the number which is 1
- 4:45and then it will increment the value of
- 4:47I what next it will go back to while
- 4:50loop because and again it will check for
- 4:52the condition because that's a while
- 4:53right it will continuously happening it
- 4:55will again check for the condition where
- 4:57it says okay I value now it's 2 so 2 is
- 5:00less than 4 yes and it will continue at
- 5:02the end when the I value becomes 4 it
- 5:06will check 4 is less than equal to 4 yes
- 5:08it will print High and 4 as well so we
- 5:10got four iterations and then it will
- 5:13increment the value of I which is it
- 5:15will make it five it will go back here
- 5:18and it will check is 5 less than equal
- 5:21to 4 false and that's where it will jump
- 5:24out of the loop so remember this thing
- 5:27the moment your while gets a false here
- 5:29it will not execute this block cool
- 5:31right now after saying all those things
- 5:33let's try to compile and run and you can
- 5:36see we got high one high two High three
- 5:38and high four that's how while loop
- 5:40works now with this I have a question
- 5:42for everyone here and the question is
- 5:44what if at the end I'm just saying as
- 5:48out and I'm saying buy and with this by
- 5:52I'm printing the value of I what do you
- 5:53think what will be the output of I
- 5:55okay try to guess it I'm just running
- 5:57this code to show you the output compile
- 6:00run you can see we got all the values
- 6:02here but with by you got five you know
- 6:05why is because
- 6:07we got false here when the value of I is
- 6:115 and that's why you got the value here
- 6:14now since I is declared outside the loop
- 6:17the value will be maintained okay now
- 6:20one more thing here I want to run this
- 6:22in a debug mode let me show you how it
- 6:23works in debug mode so what I can do is
- 6:25I can go to run
- 6:27and I can say start debugging okay so
- 6:30there are some options okay for
- 6:31debugging you have to basically you know
- 6:33uh you have to set the checkpoints what
- 6:36I will do is I will just set the
- 6:37checkpoint here so basically debug is an
- 6:40awesome feature in programming where you
- 6:42can set the checkpoint this is where you
- 6:44want to iterate one by one so normally
- 6:46when you run the code it will directly
- 6:48give all the output right what if you
- 6:50want to see the steps so it makes sense
- 6:52let me just clear this and again I will
- 6:54go into run
- 6:55and get into the debug mode okay so you
- 6:58can see uh we are in debug mode now and
- 7:02here on the left hand side we got
- 7:03variables we got local a and I we have
- 7:06two variables you know why we got a is
- 7:08because of this a variable here but it
- 7:10is at this point we want to ignore that
- 7:11we want to focus only on I variable and
- 7:14the value of I at this point is one you
- 7:17can see that I is one but now if I say
- 7:21step into you can see we got
- 7:23system.winder it will print the I and I
- 7:25the value of I which is one uh okay I
- 7:28want to go step into now it will
- 7:30increment the value of I you can see we
- 7:32got the output as well we got high one
- 7:34and then when I say step over it will go
- 7:38for the next step and you can see before
- 7:40executing it the value of I is 1 but
- 7:42once I execute that statement the value
- 7:44of I now becomes 2 okay so that it went
- 7:47back to while loop and this will keep
- 7:49happening for I value 2 I value 3 I
- 7:52value 4 as well the moment your value of
- 7:55I because comes 5 here you can see the
- 7:57value of I is 5 now it is checking and
- 7:59it falls it it will jump out of the loop
- 8:02it will print by with the value 5. so
- 8:05that's how your while loop Works in fact
- 8:07what you can also do is we can run a
- 8:10nested Loop here now how an estate Loop
- 8:12looks looks like let's say I want to
- 8:14print i1 and then I want to do something
- 8:17else maybe I want to print hello three
- 8:20times after every high so let's say I'm
- 8:23Printing High here and then I want to
- 8:25print hello as well
- 8:26and I don't want to print the value of I
- 8:28anymore
- 8:29or maybe let's print the value of I
- 8:30let's see what happens if you keep the
- 8:32value of I here and then incrementing it
- 8:35here as well that makes sense but now
- 8:37what I want to do is I want to
- 8:40after printing this High I want to print
- 8:42hello five or three times now in that
- 8:44case I can simply say hello and I can
- 8:47write the same statement multiple times
- 8:48right that's how the loop works right so
- 8:51I will say hello hello and you can see
- 8:53we got hello three times but don't you
- 8:55think we are doing the same thing
- 8:56multiple times so it makes sense to put
- 9:00that in a loop so I can simply say while
- 9:03oh but what should be the condition can
- 9:06I say I is less than equal to 3 no right
- 9:09because see I want this hello three
- 9:12times for every high so high we are
- 9:15getting to four times and for every High
- 9:17I want Hello three times
- 9:20so the same value will not work in that
- 9:22case what you have to do is you need to
- 9:24work with a different variable and I
- 9:27will create one more variable here which
- 9:28is called J and the value of J initially
- 9:30will be 1
- 9:31and then every time you execute this
- 9:34while loop where it prints High
- 9:36it will jump inside here it will set the
- 9:39value of J as 1 it will set the value it
- 9:41will check for the condition where J
- 9:43should be less than equal to
- 9:453 and then every time you do that let me
- 9:48open the curly brackets
- 9:50and we can put this hello statement in
- 9:53here so we don't have to put this thing
- 9:56now I can just simply say hello and of
- 9:59course I want to print J plus plus as
- 10:01well and remember this thing we are
- 10:04saying J plus plus in the inner while
- 10:06loop and then we are saying the I plus
- 10:09plus in the outer loop okay if you want
- 10:12to understand this with an analogy uh
- 10:15just go with an example of days in a
- 10:18week so we have multiple days in a week
- 10:20right so we have Sunday Monday Tuesday
- 10:21Wednesday Thursday Friday Saturday
- 10:23Sunday I've already mentioned you know
- 10:26we we basically want Sunday twice a week
- 10:29but anyway so if we talk about these
- 10:30days every day we have 24 hours right so
- 10:33if you say this is 1 pm on Monday again
- 10:36the Monday gets over the clock will
- 10:38start at zero right then zero one two
- 10:40three four five six and then it
- 10:42continues right as the date changes the
- 10:45clock repeats in the same way once your
- 10:47eye is incrementing it's like a days
- 10:49Monday Tuesday the inner loop you can
- 10:52imagine that's as a number of hours it
- 10:54will keep incrementing but after three
- 10:56it will go back to one because it will
- 10:58complete the inner loop it will go back
- 11:00to Outer Loop and again again the value
- 11:02of J will become one let me show you how
- 11:04it works let me just compile this code
- 11:06compile and
- 11:09run you can see we got high uh we are
- 11:12still printing one here you know what I
- 11:14will do I will also print J so that you
- 11:16can see the value of J as incrementing
- 11:19it
- 11:20and let me give a space here as well let
- 11:22me say clear
- 11:24compile run so you can see we got high
- 11:27one and then it is going for the inner
- 11:29loop now so once you execute this High
- 11:31it will go for the inner loop where we
- 11:33are saying J is less than 3 so basically
- 11:36it will print this statement three times
- 11:38because the value of J is less than
- 11:41equal to 3 so it will iterate three
- 11:42times
- 11:43and then again you have to go for the
- 11:45outer loop so the the moment this is
- 11:47false it will execute I plus plus where
- 11:49it will make I2 it will go back to the
- 11:52upper loop again it will say okay now
- 11:55the value of I is 2 is 2 less than equal
- 11:58to 4 yes it will go inside it will print
- 12:01the value of high which is high 2.
- 12:04again it will make the value of J as 1
- 12:06and that's why it is incrementing again
- 12:10cool right so this is basically called a
- 12:13nested while loop so that's it from this
- 12:16video where we have talked about uh how
- 12:19to create a while loop how to use a
- 12:21nested Loops using uh while loop so I
- 12:24hope you enjoyed in the next video we'll
- 12:26talk about do while loop
About this transcript
This page contains the full transcript of #17 While Loop in Java by Telusko, generated from the public captions YouTube serves with the video. The transcript has 2,570 words across 321 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.