YouTube2Text

#17 While Loop in Java — Transcript

by Telusko · 2,570 words · 321 segments · language en · Watch on YouTube

Full transcript

  1. 0:00so in the last video we have talked
  2. 0:01about the need for Loops right and so we
  3. 0:04have decided that if you want to repeat
  4. 0:06something Loops can be a good thing and
  5. 0:09we also talked about there are different
  6. 0:10types of Loops right we have while loop
  7. 0:12do while loop for Loop in this video
  8. 0:13let's try to understand the first Loop
  9. 0:15which is while loop so we'll go over
  10. 0:18this go for the same statement here if
  11. 0:19you can see we are Printing High and
  12. 0:21again as I mentioned before ignore this
  13. 0:23x this is a suggestion given by your vs
  14. 0:25code uh this is what we are typing just
  15. 0:28high and what I want to do is I want to
  16. 0:30print this High multiple times so what
  17. 0:34we can do is we uh we can say Hey you
  18. 0:36know Java I want to print this multiple
  19. 0:39times Java will say okay
  20. 0:41the first thing you do is you put this
  21. 0:43in a block of course it's not compulsory
  22. 0:45when you have one statement but just to
  23. 0:47understand just to get started we are
  24. 0:48putting that in a block now and then of
  25. 0:51course we have to follow into
  26. 0:51indentation as well and we are doing
  27. 0:53that now what we can do is we can say
  28. 0:55Hey you know Java I want to run this
  29. 0:58five times so Java will say okay if you
  30. 1:01want to repeat you have to use a keyword
  31. 1:03called while now in English as well we
  32. 1:06normally use while to say repeat
  33. 1:08something or while it is true do that so
  34. 1:11here we can say while it is what in this
  35. 1:15bracket you have to mention is it true
  36. 1:17or false now basically that's how Wild
  37. 1:19works so if you say true it basically it
  38. 1:22will just keep printing this High
  39. 1:24because we are saying true it says okay
  40. 1:26while I'm true I will keep printing it
  41. 1:28so that's why if you just run this code
  42. 1:30by just by mentioning the
  43. 1:33while
  44. 1:35you can see we are getting high I just
  45. 1:37kept printing okay it's it's not
  46. 1:39stopping if you can see if I try to
  47. 1:40scroll down it is continuously happening
  48. 1:43and maybe it will uh fill my Ram it will
  49. 1:47crash okay I don't think so but let me
  50. 1:49stop this execution otherwise there will
  51. 1:51be an issue so what we can do is I don't
  52. 1:53want to continuous this for a long time
  53. 1:56I want to do that for a particular
  54. 1:58condition maybe I want to say I want a
  55. 2:01counter which will start from one and it
  56. 2:03will stop at five oh when I say counter
  57. 2:06it means I need a variable well I can
  58. 2:08store the value so what we can do is
  59. 2:10here I can just come up and I can say
  60. 2:12int I is equal to let's say one now most
  61. 2:16of the time when you use Loops you will
  62. 2:17be using a variable name I it's not
  63. 2:20compensative we can use any variable
  64. 2:21name but it makes sense right because
  65. 2:23normally when you go inside the while
  66. 2:26you are executing something you are
  67. 2:28changing the values of the eye
  68. 2:31you basically increment it right so it
  69. 2:34makes sense to call it as a iterator
  70. 2:35which will iterate so I iterator makes
  71. 2:39sense and that's why most of the time
  72. 2:40you say i j but as I mentioned you can
  73. 2:43use any variable of your choice so we
  74. 2:45are using I here which is I is equal to
  75. 2:471 and then what we can do is every time
  76. 2:51you run this loop after Printing High
  77. 2:53you can just increment the value of I
  78. 2:56oh but then how do we increment and we
  79. 2:58have seen that right we have a concept
  80. 3:00of I plus plus so I plus plus will
  81. 3:02simply increment the value of I okay
  82. 3:05that makes sense and oh how do I check
  83. 3:10for the condition see it makes sense now
  84. 3:12because I is 1 and every time you run
  85. 3:15this Loop the in the I value will
  86. 3:16increase right so if you run this while
  87. 3:18for let's say five times it will
  88. 3:20increment the value of I let me show you
  89. 3:23what I will do is I will print hi as
  90. 3:25well as I will print uh maybe you can
  91. 3:28you can print both the things I say on
  92. 3:30the same line you just have to give a
  93. 3:32space after high and I can give a plus
  94. 3:34symbol so when you want to concatenate
  95. 3:36two different things High and a number
  96. 3:39we can use a plus operator yeah plus
  97. 3:41Works in that way it works for addition
  98. 3:44as well as it works to concatenate two
  99. 3:46things
  100. 3:47and here I'm writing I now let's see
  101. 3:51what's the output is again I have not
  102. 3:52changed the condition so it will print
  103. 3:54infinite times
  104. 3:55but I just want to see what happens if I
  105. 3:57compile this code
  106. 3:59and run you can see the value of I is
  107. 4:02incrementing continuously
  108. 4:04and you can see it so fast depend upon
  109. 4:06your machine speed as well and let me
  110. 4:07know when you try on your machine how
  111. 4:10fast it is so what I want to do is I
  112. 4:12want to stop this somewhere so we have
  113. 4:14to apply a condition how do I stop it so
  114. 4:16we have to say if I is less than equal
  115. 4:19to 4 so what it will do is it will check
  116. 4:22for the condition remember we have
  117. 4:23talked about this in relational
  118. 4:24operators it will continuously check for
  119. 4:27the condition so what how it runs is
  120. 4:29initially it will set the value of i as
  121. 4:311 and then in the while loop it will
  122. 4:33check if the value of I which is 1 is
  123. 4:37less than 4 yes it will continue it will
  124. 4:40go inside the loop it will print high it
  125. 4:43will also print the number which is 1
  126. 4:45and then it will increment the value of
  127. 4:47I what next it will go back to while
  128. 4:50loop because and again it will check for
  129. 4:52the condition because that's a while
  130. 4:53right it will continuously happening it
  131. 4:55will again check for the condition where
  132. 4:57it says okay I value now it's 2 so 2 is
  133. 5:00less than 4 yes and it will continue at
  134. 5:02the end when the I value becomes 4 it
  135. 5:06will check 4 is less than equal to 4 yes
  136. 5:08it will print High and 4 as well so we
  137. 5:10got four iterations and then it will
  138. 5:13increment the value of I which is it
  139. 5:15will make it five it will go back here
  140. 5:18and it will check is 5 less than equal
  141. 5:21to 4 false and that's where it will jump
  142. 5:24out of the loop so remember this thing
  143. 5:27the moment your while gets a false here
  144. 5:29it will not execute this block cool
  145. 5:31right now after saying all those things
  146. 5:33let's try to compile and run and you can
  147. 5:36see we got high one high two High three
  148. 5:38and high four that's how while loop
  149. 5:40works now with this I have a question
  150. 5:42for everyone here and the question is
  151. 5:44what if at the end I'm just saying as
  152. 5:48out and I'm saying buy and with this by
  153. 5:52I'm printing the value of I what do you
  154. 5:53think what will be the output of I
  155. 5:55okay try to guess it I'm just running
  156. 5:57this code to show you the output compile
  157. 6:00run you can see we got all the values
  158. 6:02here but with by you got five you know
  159. 6:05why is because
  160. 6:07we got false here when the value of I is
  161. 6:115 and that's why you got the value here
  162. 6:14now since I is declared outside the loop
  163. 6:17the value will be maintained okay now
  164. 6:20one more thing here I want to run this
  165. 6:22in a debug mode let me show you how it
  166. 6:23works in debug mode so what I can do is
  167. 6:25I can go to run
  168. 6:27and I can say start debugging okay so
  169. 6:30there are some options okay for
  170. 6:31debugging you have to basically you know
  171. 6:33uh you have to set the checkpoints what
  172. 6:36I will do is I will just set the
  173. 6:37checkpoint here so basically debug is an
  174. 6:40awesome feature in programming where you
  175. 6:42can set the checkpoint this is where you
  176. 6:44want to iterate one by one so normally
  177. 6:46when you run the code it will directly
  178. 6:48give all the output right what if you
  179. 6:50want to see the steps so it makes sense
  180. 6:52let me just clear this and again I will
  181. 6:54go into run
  182. 6:55and get into the debug mode okay so you
  183. 6:58can see uh we are in debug mode now and
  184. 7:02here on the left hand side we got
  185. 7:03variables we got local a and I we have
  186. 7:06two variables you know why we got a is
  187. 7:08because of this a variable here but it
  188. 7:10is at this point we want to ignore that
  189. 7:11we want to focus only on I variable and
  190. 7:14the value of I at this point is one you
  191. 7:17can see that I is one but now if I say
  192. 7:21step into you can see we got
  193. 7:23system.winder it will print the I and I
  194. 7:25the value of I which is one uh okay I
  195. 7:28want to go step into now it will
  196. 7:30increment the value of I you can see we
  197. 7:32got the output as well we got high one
  198. 7:34and then when I say step over it will go
  199. 7:38for the next step and you can see before
  200. 7:40executing it the value of I is 1 but
  201. 7:42once I execute that statement the value
  202. 7:44of I now becomes 2 okay so that it went
  203. 7:47back to while loop and this will keep
  204. 7:49happening for I value 2 I value 3 I
  205. 7:52value 4 as well the moment your value of
  206. 7:55I because comes 5 here you can see the
  207. 7:57value of I is 5 now it is checking and
  208. 7:59it falls it it will jump out of the loop
  209. 8:02it will print by with the value 5. so
  210. 8:05that's how your while loop Works in fact
  211. 8:07what you can also do is we can run a
  212. 8:10nested Loop here now how an estate Loop
  213. 8:12looks looks like let's say I want to
  214. 8:14print i1 and then I want to do something
  215. 8:17else maybe I want to print hello three
  216. 8:20times after every high so let's say I'm
  217. 8:23Printing High here and then I want to
  218. 8:25print hello as well
  219. 8:26and I don't want to print the value of I
  220. 8:28anymore
  221. 8:29or maybe let's print the value of I
  222. 8:30let's see what happens if you keep the
  223. 8:32value of I here and then incrementing it
  224. 8:35here as well that makes sense but now
  225. 8:37what I want to do is I want to
  226. 8:40after printing this High I want to print
  227. 8:42hello five or three times now in that
  228. 8:44case I can simply say hello and I can
  229. 8:47write the same statement multiple times
  230. 8:48right that's how the loop works right so
  231. 8:51I will say hello hello and you can see
  232. 8:53we got hello three times but don't you
  233. 8:55think we are doing the same thing
  234. 8:56multiple times so it makes sense to put
  235. 9:00that in a loop so I can simply say while
  236. 9:03oh but what should be the condition can
  237. 9:06I say I is less than equal to 3 no right
  238. 9:09because see I want this hello three
  239. 9:12times for every high so high we are
  240. 9:15getting to four times and for every High
  241. 9:17I want Hello three times
  242. 9:20so the same value will not work in that
  243. 9:22case what you have to do is you need to
  244. 9:24work with a different variable and I
  245. 9:27will create one more variable here which
  246. 9:28is called J and the value of J initially
  247. 9:30will be 1
  248. 9:31and then every time you execute this
  249. 9:34while loop where it prints High
  250. 9:36it will jump inside here it will set the
  251. 9:39value of J as 1 it will set the value it
  252. 9:41will check for the condition where J
  253. 9:43should be less than equal to
  254. 9:453 and then every time you do that let me
  255. 9:48open the curly brackets
  256. 9:50and we can put this hello statement in
  257. 9:53here so we don't have to put this thing
  258. 9:56now I can just simply say hello and of
  259. 9:59course I want to print J plus plus as
  260. 10:01well and remember this thing we are
  261. 10:04saying J plus plus in the inner while
  262. 10:06loop and then we are saying the I plus
  263. 10:09plus in the outer loop okay if you want
  264. 10:12to understand this with an analogy uh
  265. 10:15just go with an example of days in a
  266. 10:18week so we have multiple days in a week
  267. 10:20right so we have Sunday Monday Tuesday
  268. 10:21Wednesday Thursday Friday Saturday
  269. 10:23Sunday I've already mentioned you know
  270. 10:26we we basically want Sunday twice a week
  271. 10:29but anyway so if we talk about these
  272. 10:30days every day we have 24 hours right so
  273. 10:33if you say this is 1 pm on Monday again
  274. 10:36the Monday gets over the clock will
  275. 10:38start at zero right then zero one two
  276. 10:40three four five six and then it
  277. 10:42continues right as the date changes the
  278. 10:45clock repeats in the same way once your
  279. 10:47eye is incrementing it's like a days
  280. 10:49Monday Tuesday the inner loop you can
  281. 10:52imagine that's as a number of hours it
  282. 10:54will keep incrementing but after three
  283. 10:56it will go back to one because it will
  284. 10:58complete the inner loop it will go back
  285. 11:00to Outer Loop and again again the value
  286. 11:02of J will become one let me show you how
  287. 11:04it works let me just compile this code
  288. 11:06compile and
  289. 11:09run you can see we got high uh we are
  290. 11:12still printing one here you know what I
  291. 11:14will do I will also print J so that you
  292. 11:16can see the value of J as incrementing
  293. 11:19it
  294. 11:20and let me give a space here as well let
  295. 11:22me say clear
  296. 11:24compile run so you can see we got high
  297. 11:27one and then it is going for the inner
  298. 11:29loop now so once you execute this High
  299. 11:31it will go for the inner loop where we
  300. 11:33are saying J is less than 3 so basically
  301. 11:36it will print this statement three times
  302. 11:38because the value of J is less than
  303. 11:41equal to 3 so it will iterate three
  304. 11:42times
  305. 11:43and then again you have to go for the
  306. 11:45outer loop so the the moment this is
  307. 11:47false it will execute I plus plus where
  308. 11:49it will make I2 it will go back to the
  309. 11:52upper loop again it will say okay now
  310. 11:55the value of I is 2 is 2 less than equal
  311. 11:58to 4 yes it will go inside it will print
  312. 12:01the value of high which is high 2.
  313. 12:04again it will make the value of J as 1
  314. 12:06and that's why it is incrementing again
  315. 12:10cool right so this is basically called a
  316. 12:13nested while loop so that's it from this
  317. 12:16video where we have talked about uh how
  318. 12:19to create a while loop how to use a
  319. 12:21nested Loops using uh while loop so I
  320. 12:24hope you enjoyed in the next video we'll
  321. 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.