Java Essentials - Do while loop in java
2K views
Nov 18, 2024
Java Essentials - Do while loop in java
View Video Transcript
0:00
In this video we are going to discuss do while loop in Java
0:05
In the previous video we have discussed while loop in Java. So it is a new one that is a do while loop in Java and also we shall discuss what is the
0:14
difference between these two. The do while loop is similar to the while loop but in this case the statements will be
0:22
executed at least once. In case of do while what is happening the condition is going to get checked at the
0:29
end of the conditional code. So, that's why the code can get executed at least once
0:36
But in case of while do, as the condition is coming at front, so when the condition is
0:41
false, the code may not get its execution. The basic syntax is like this. So, do, then
0:48
curly breasts. Then here you'll be having the set of statements which will be there within the
0:53
do while construct body, and then while and the bullion expression. And when the bullion expression will
0:59
be false, then after executing these statements, the control will come out from this
1:04
due while construct and executing this one once only But when this condition is true then it will go on executing for multiple number of times until the bullion expression is turned to false the loop
1:17
will execute and will be continued so here is a very easy flowchart for us so control is coming
1:24
from here so the code block will get executed then the condition will be checked and if the
1:30
condition is found true then code block will have its re-executive execution but if the condition is false then control will come out and ultimately the program
1:40
will get terminated so let us go for one demonstration for the easy understanding of this do while
1:46
loop in java in this demonstration we are discussing do while loop in java in case of do while loop
1:55
at first we'll be writing a do and then carly presses will indicate the do block then following that
2:00
we'll be writing while and while we'll be having one condition and don't forget to
2:04
give this semicolon at the end so in other video we have discussed the while loop
2:09
and this video we're discussing the do while loop the basic difference between
2:13
do while and while do is that in case of do while at first the block will get executed and then while will check the condition that means initially if the condition is false then also the block
2:24
will get its execution at least for once but in case of while what happens while
2:30
will be there with the condition so that means when the condition is initially false
2:35
then the control cannot come into the while block so the block may have execution
2:40
for zero number of times also but in case of do while the do block
2:44
must get executed at least for once and it may get iterated for multiple
2:49
number of times when the condition will remain true so that is a basic
2:53
difference between do while and while do okay here we have initialized a with
2:58
zero and sum will be equal to zero a and some both of the type integer variable
3:03
so system dot out dot print ellen the value of a I printed the value of a here
3:08
and then sum is equal to some plus a so at first the value of a is zero so
3:13
0 has to be printed sum is equal to sum plus a 0 will be added with the sum and a has got
3:18
incremented so now a has become 1 as 1 is less than 10 the condition is true so again it will go into the do block and it will go on executing until the value of a is becoming 10 so what will happen when the nine
3:35
will be the value of a will be in a then nine will get added so nine will get
3:40
incremented by one now it will become ten so ten less than ten the condition
3:44
will be false it will come out from the while do while and then the last line is
3:49
getting printed that is a sum of the numbers from zero to nine will be some so
3:54
now let me go for the execution you can find that a is ranging from zero to nine
3:58
and the sum is getting obtained as 45 so I am expecting this condition is
4:03
becoming false when value of a is becoming 10 so if I print the value of a here
4:11
if I print the value of a here so if I write a is equal to then obviously I'm
4:17
expecting that this value of a will be printed as 10 here you can find that yes
4:22
the value of a is becoming 10 so 10 less than 10 the condition is getting false and it is the control is coming out from this do while construct
4:30
So in this demonstration you are getting idea that how to use do while construct in our Java coding
4:37
Thanks for watching this video
#Computers & Electronics
#Java (Programming Language)
#Programming