okay so we discussing about uh control statements
in Java in the last class we have seen conditional statements if condition if FS condition
Ned if fs and then switch case statement so they are all comes under conditional
statement so based on the condition we can execute set of group of statements now
today we are going to discuss another type of control statements we call them as
looping statements looping or iterative statement looping or iterative statements so
first we need to understand what is a loop and what is iteration so Loop is nothing but set of
statements or group of statements which we are going to execute multiple times or repetitively
we are executing and that is basically called as a loop suppose you have written program for 10
lines or 50 lines and I want to execute certain number of lines or multiple times I I want to
repeat so without writing the same statement again and again duplicated so same statements
I will write only once but I want to repeat multiple times so in those cases we can try to use
looping statements okay there are three different uh Loops are available in Java while loop is one
type of Loop and do while and then follow Loop so these are the three different types of Loops
are available in Java and there is one more Loop is also there which is also called enhanced F
Loop enhanced enhanced for Loop this is normal for Loop and this is do while loop and this is
while loop so there are totally four types of Loops but this enhanced for Loop is also called as
for each Loop and this is especially designed for collections whenever you're working with arrays
concept or array list or hashmap so those type of Concepts whenever you're using so we have
to prefer this Loop enhanc it for Loop or for each is Loop okay so we will discuss this later
so when you're discussing about array concept I will introduce you how we can work with enhancer
for Loop now today we are going to discuss about three different Loops while loop do while loop
and then for Loop okay so first of all what is the use of loop why we need to use this looping
statement let me just give you a simple example suppose through program I want to print one to 10
numbers let's say I want to print 10 to 10 numbers through Java program so normally how we will write
you have to write 10 print statements right system do do print tell one then again system. pintel two
then again system. out. pintel 3 so like this you need to write up to 10 statements system. print
and then 10 so like this you need to write 10 statements so if you just want to print one to
10 10 numbers you have to write 10 statements in your Java program so instead of writing these
many statements what we can do is we just write only one statement and that I will repeat 10 times
okay I will write only one statement then I will repeat 10 times instead of writing 10 statements I
will write the statement only once and then I will repeat the same statement 10 times each time
I'll increase this value by one okay so each time I will increase this value by one so then
I will get 1 2 3 4 up to 10 so that is a usage of using looping statement instead of writing
multiple statements you can simply write one single statement and that you can repeat multiple
times n number of times you can repeat so because of that what is an advantage is it will reduce
the code we can reduce the code and we can avoid a duplication we can avoid the duplication and
it will automatically reduce the code so that is the main usage of using looping statement
but whenever you're writing when we using any looping statement it can be V Loop or do while or
for Loop any looping statement three things are important things so we have to know three things
we need to mainly concentrate on three things so the first important thing is where exactly we
are starting point okay the starting point is very important where exactly we are starting
point and how many times we have to repeat how many times we need to repeat starting point and
how many times we have to repeat means what the condition is important conditions ISS how many
times we have to report how many times we have to repeat and the first thing is what initialization
initialization means starting point where exactly we want to start where to start where to start
is initialization second thing is condition condition says how many times we have to repeat
how many times you have to repeat the statement and third thing incrementation or decrement
this says in every iteration or every round of execution how much value we have to increase or
how much value we want to decrease so if I look at here 10 statement in every statement we are
increasing by one and sometimes we may decrease the values also so in every iteration we have to
know how much value we have to increment and how much value we have to decrement so these are the
three important things which we have to remember while writing the looping statement initialization
addition incrementation or decrement which is very very important any Loop it can be for Loop or
while loop or do by Loop we need to know three things initialization means where we have to start
condition means how many times we have to repeat incrementation or decementation means how much
value you have to increment in every iteration or how much value we have to decrement in iteration
so we have to know these three things before using any type of loop okay so simple example let's say
I want to print one two 10 numbers now what is the initialization condition and incrementation
so for example let's say I want to print one to 10 numbers that's my requirement let's say one 2
3 4 up to 10 numbers I want to print now what is the initialization here starting point one is
a starting point this is a initialization and uh how many times we have to repeat condition
so whenever this I value let's say the starting point I can just represent with some variable
called I so as soon as the I value is reaches uh 11 let's say I less than or equal to 10 then
we have to stop repeating the loop I should not become 11 as soon as I value becomes 11 we have
to stop this is called conditions so conditions is how many times we have to repeat in every
every time how much value we are incrementing plus one we are incrementing so initially I value
one next Val I value two I value three I value 4 5 6 7 up to 10 it will repeat as soon as I value
become 10 it will print the value then it will exit from the loop so initialization condition
and incrementation how much value we want to increment in every time so these are the three
important things we have to know before using any looping statement okay now first let us start
with the while loop and once you understood all three different type of loops and finally we will
compare so what are the differences between those three loops and when we have to use while loop
when we have to use do while and when we have to use for Loop okay that we will discuss later
so first initially we will try to understand about the loops syntaxes and how to use with the
different examples the first part while loop so how to use this while loop what is the syntax
so in the while loop we need to First write while keyword in the bracket we have to specify
the condition and inside the block inside the block what are the statement we want to repeat so
those statements we have to keep inside the while loop and then after writing all the statements
we have to write one incrementation or decrement statement and before starting the value we need
to write initialization part where we have to start that we need to specify before the Val and
inside the Val we need to specify condition that is decides how many times we have to repeat the
statements and inside the block we can specify the statements like what are all statements we
want to repeat multiple times we can specify them inside the block after completion of the
statements we need to specify incrementation or decrement so this is actual syntax of while loop
Now by using this I will show you some examples then you can understand better while loop let's
go back to the eclipse and create new package for today new package day five C on finish okay
now I'm creating a new class and uh my class name is while loop demo take this main method also
there's a finish okay so first we will see some basic examples so that we can clearly understand
the looping statement the first example example one so in example one I want to just print one
to 10 numbers one to 10 numbers okay so now here what is the starting point one to 10 numbers
what is the starting point one so we will take one variable called inter I equal to 1 so some
iteration I is representing some iteration you can take any variable okay I'm just saying I int
I equal to 1 that is saying initialization Point what is this this is initialization now we'll
directly start while loop while in the bracket this is a syntax so while loop starting point the
while loop ending point okay now inside the while loop uh here we need to put some condition so when
we have to stop as soon as I value becomes 11 we have to stop so I less than or equal to 10 I put
condition I less than or equal to 10 means what whenever I value becomes 10 10 less than or equal
10 condition is true right so even 10 also will print but I value whenever I value becomes 11 this
condition become false so whenever this condition become false then the value will stop repeating
now inside this I will just print the value of I and as soon as I printed value of I immediately
I will increase the value of I by One I ++ means I value will be increased by one so here in
this we put condition this is called condition and this is incrementation incrementation or
decrement so all three things we mentioned so this is initialization says where we have to
stop condition means how many times you have to repeat incrementation how much value we have
to increase in every iteration so if I look at this run as Java application you can see here it
will just print one to 10 numbers exactly one to 10 numbers it has got printed so now let us try to
understand how exactly this Loop is working first thing the starting point is what I one is starting
point so what is the current value of I here 1 1 less than or equal to 10 condition is true as
soon as this condition is true then it will enter into the block so here it is printed one I
value current value of I is what one so one is got printed and after printing I value one then what
I'm doing here incrementing I ++ now I becomes two now I becomes two now again it will go up and now
current value of I is what two right two two less than or equal to 10 Again condition is true now
again it will come inside now the current value of I is what two so two is got printed then after
that incremented now I value becomes three now again it will go up now current value of I is what
three three less than or equal to 10 condition is true again now again come inside now high value
is three and then incrementation four again four less than or equal to 10 condition is true now
come inside again four is got printed so like this it will go on till 9 for example or till 10
then I ++ becomes 11 I value becomes 11 again it will go up 11 less than or equal to 10 condition
become false as soon as this condition become false then it will stop repeating the loop so by
the time we will get all the numbers 1 to 10 okay so this is how it will repeat and how many times
this Loop will repeat who will decide that the condition will decide that so the condition is
true these statements will be executed whenever this condition is false then immediately the loop
will be exited so we have to make this condition false at certain point of time we have to make
this condition false at certain time of certain point of time if I don't make it is false then
what will happen this will go to infinite Loop it will keep on repeating multiple times but
how can how can we make this condition false based by using incrementation or decementation so
if I don't use incrementation then what happens every time I value Remains the Same it will not
increment so it will keep on printing one one one one one the condition will never become false so
the loop will become infinite Loop it will keep printing the all the numbers now observe here I
just commenting this i++ so I value will not be incremented so every time the condition will be
satisfied every time I value is same that is one so condition is always satisfying this Loops keeps
repeating it never exit we need to forcefully exit the loop now observe here when running as a Java
application it will go to infinite Loop see the output it is keep printing one like this we
have to click on this red button forcefully stop your program so this is infinite it keep on
printing the same value every time because this condition never become false okay this condition
never become false yeah this is same actually we already discussed right I ++++ I or I equal to I
+ 1 you can write whichever notation you want I'm just using some Java notation instead of i++ you
can write I equal to I + 1 that is also correct or you can write Plus+ I that is also correct
multiple ways okay we already discussed this incrementation okay so so system print and I and
if I comment this i++ what is happening every time the same number is got printing and condition
never become false that's the reason it will go it will go to infinite for infinite value so
we have to make this condition false at certain point of time so to do that we have to use some
incrementation so now what happens as soon as I value becomes 11 this condition become false
then it will exit from the so when I execute this run as Java application it will print one to
10 numbers like this so this is how exactly while loop Works initialization is important condition
is important and incrementation is also increment so we can increment by one or many okay that
depends upon your requirement Okay so here I just incremented by one why I have incremented by
one by my requirement is what one to 10 numbers I want to print in every iteration how much value
we need to increment here only one that's the reason I say y i++ if you want to increment by
two or if you want to increment by you can change that number no issues the incrementation you can
change I will show you in the next example just hold on have some patience okay this is simple
basic example one to 10 numbers we have printer for that what we have done initialization
Point condition and then incrementation or decrement so three things we have used now
let me show you one more example let's comment this I'll show you multiple examples don't worry
you will understand okay example two so this time I want to print print Hello message okay hello
message 10 times I want to print Hello message 10 times very simple example I want to print Hello
message 10 times now tell me what is the starting point Hello message I want to print 10 times Hello
message I want to print 10 times so what is the starting point condition and incrementation
so like this hello hello message 10 times I want to print first time second time third
time and then 10th time so 10 times I want to print see here we'll take the starting point
is one starting point is what one so because we need to print one to 10 times we have to print
Hello message so in I equal to 1 so how many times we have to print hello how many times you have
to print 10 times so what is the condition now I less than or equal to 10 it is also same now
what is the value we want to print Hello message we want to print so here just say hello okay
so this is a string Hello message we want to print and after printing Hello message we have
to increment the I value so now observe when I run this code this will print Hello message
10 times exactly 10 times okay so pre what is the difference in the previous EX example and
this example in the previous example I printed the I value directly so it is printed 1 2 3 4 5
but in the example I'm not printing I value I'm just printing the message hello multiple times
every time so first time I value one so it is printed hello and incremented I value by two
now two less than 10 Again condition is true now again it comes into the blog Hello message
is executed one more time I ++ I value becomes three 3 less than or equal to again condition is
true then again hello message is executed so how many times it will repeat like this 10 times
it will repeat 10 times it will repeat okay so this is how we can simply use while so guys you
understood first and second examples I will show you some more examples so very basic example
how we can use looping statement and what is an advantage so if you want to print hell message
10 times you have to write 10 print statements so instead of writing 10 print statements I put
only one statement inside the while loop and then repeating 10 times okay repeating 10 times
what does it mean can we do using string means what this should be number okay starting
point should be number reputation reputation sense numbers we need to use printing hello
here we are printing hello right this is a string okay this is a string actually we are
printing 10 times and here we are printing the I value 10 times that means what we are printing
number 10 times here we are printing string 10 times understood or any questions on this guys
very basic very simple so just initialization the condition and then message and here also same
initialization condition instead of message I just directly printing I value that is the only
difference okay both are same actually so here we are printing the number I value and here we just
printing the message that's the only difference okay now let me show you one more example so
this time example three I want to print even numbers only even numbers between between 1 to
10 I want to print only even numbers between 1 to 10 so what are the even numbers between 1 to
10 2 4 6 8 10 so these are the even numbers right so I want to print only even numbers between 1 to
10 10 even numbers between one so there multiple approaches are there so first let us start with
the first approach only even numbers I want to print that's my requirement just understand this
1 to 10 I want to print only even numbers first even number is what two is a first even number
then four six8 10 so these are the even numbers between 1 to 10 right so what is the starting
point here starting point two is a starting point let's say I value two and uh what is an
ending point what is a condition here I less than or equal to 10 this is a condition because
we have to stop 10 and how much value we have to increment here every time how much value we have
to increment two we have to increment so 2 + 2 4 4 + 2 6 so + 2 and here 4 + 2 6 6 + 2 8 8 + 2 10
so every time you have to increment two so how to write the expression here I = to I + two this
is the expression we have to write previously we have incremented only by one so i++ we used but
this time we have to increment by two so we can use I equal to I + 2 or what is another notation
which we have shorthand operator I + = to 2 this is also correct okay now let us try to write the
looping statement so starting point in I = to 2 starting even number then in the v Loop we put the
condition I less than or equal to 10 now what we need to print here we need to directly print
I value now after printing I value we need to increment I value by two so simply I can say
i+ equal to 2 so like this we can increase I value more than one also okay now now every time
I value will be this is exactly equal to what I equal to I + 1 I equal to I + 1 this is the same
this is same sorry I equal to I + 2 so the same expression we have to write inside the shortcut
way so I plus equal to 2 anything is fine okay so initialization condition then printed the
value after that we have incremented by two now execute and say so which will print only even
numbers between 1 to 10 so exactly we got even numbers okay so this is how we can
simply write a looping statement so to print even numbers between 1
to 10 so there is another approach is there let me show you this is
approach one let's comment this code okay now I want to print even numbers
between 1 to 10 but I can use a different thing so different approach I can say approach
two so here I will take initialization condition incrementation only by one only okay so observe
here very simple I can say initialization in I equal to 1 I'm taking starting point 1 to 10
even numbers we want to print let's take from one itself and then in the v Loop condition is
also same I less than or equal to 10 I less than or equal to 10 and inside the V Loop I will just
print the I value then I'll try to increment the I value Plus+ so what is an output you will get from
this it will just print one to 10 numbers it will print all one to 10 numbers right but now I want
to print only even numbers I want to print only even numbers between this range so now what we
need to do is before printing this I value before printing this I value we have to check whether it
is even or odd if it is a even number then print if it is not even number then don't print okay
if it is a even number just print it if it is not even number don't print it so condition is applied
if you want to apply the condition which statement we have to use So based on the condition we have
to execute the statement so which condition we have to use if condition we have to use now before
printing I value I will check a number is even or not how to check number is even or not any number
divided by two which is giving zero as an output that is a even number so that you can put inside
the if condition like this okay now what exactly we are doing here before printing this I value
we are checking whether it is even or odd number if this condition is satisfied that is a even
number so we are printing it if this condition is not satisfied that is not even number so
this will not print and incrementation will happen so else is not required here else part is
not required so why else is not required here why else is not required here because we are not
finding the odd numbers we are just printing only even number so just observe here when I run
this as a Java application see here we got even numbers okay so we should not put this in the
V Loop we should not put this in the Y loop I percent 2 equal to Z how can we put in the Y Loop
because first we need to repeat right one to 10 numbers we have to repeat for that we need to put
condition in the while see if condition and while condition is not the same guys don't compare okay
so while condition decides how many times we have to repeat the loop while condition decides how
many times we have to repeat the loop okay and this condition decides filter which is acting as
a filter so we are just printing the I values here but before printing the I value we are filtering
it based on certain condition we are filtering the data so if this condition is satisfied then
only print I value if not satisfied then don't print and go to the next value okay so this is how
this will execute so when I execute this this will print only even numbers between 1 to 10 it will
not print odd numbers this is another approach so to print even numbers between 1 to 10 approach one
and approach two okay now let me show you one more example okay so my requirement
is uh my requirement is like this example three is over now example four example four so I want to print one to 10 numbers
okay for every number I should spell I should specify even or odd so for example like this one
odd number two even number three again odd number four even number okay like this I want to specify
10 again even number so like this I want to print one to 10 numbers one to 10 numbers and I want to
print each number even or odd separately one is odd number two is even number three is OD number
like this so the same order the same structure I want to print my output okay so then what we
can do so first step let us try to print one to 10 numbers directly okay so what I can do is in
I equal to 1 starting point is one while I less than or equal to 10 10 times we have to repeat
and then I'll print I value every time so when I execute this what will happen i++ so this will
print all one to 10 numbers right when I run as a Java application so this will print one to 10
numbers now what we need to do every number I want to print even or odd along with this number I
value I want to print whether it is even number or odd number okay this is the output I want to get
so now what we need to do is before printing this I value check even or not if I percent 2 equal to
Zer that means what it is even number right print so system print I is concatenate with some string
let's say even like this so print the number first and say whether it is even or not so if this
condition is satisfied what does it mean it is a even number so print a number along with the
even suppose if this condition is not satisfied else if the condition is not satisfied what does
it mean it is a odd number it is odd number right so again if the condition is not satisfied then
again print I value but specify which is odd number like this okay so I value one for example
condition is true print the value along with even the condition is false print I value along with
OD then increment that value then again it will go up again check the condition is true print even
if the condition is false then print OD then again incrementation will happen Okay so now when I
execute this observe the output yes we got the same thing right one odd two even three odd number
four one is even number fifth one is odd number right exactly the same output we got so what does
it mean is inside the looping statement also we can use conditional statements so to filter the
data if condition will not repeat anything okay it is just a condition based on that we can execute
the statement so in the while condition decides how many times we have to repeat the statements if
condition decides a filter on what basis we want to execute the statement that is decided by the
if condition okay so now like this we can print the output so these are the examples of Y Loop
and sometimes we have to uh decrease the values instead of incrementation we should also decrease
the data so for example let's say example five example five so now I want to print the numbers
between 10 to 1 in descending order descending order 10 9 8 7 up to 1 descending order okay now
tell me what is the starting point ending point and condition 10 to one 10 to 1 10 9 8 7 up to 1
now tell me what is the starting point here 10 is the starting point let I equal to 10 I'm taking
10 is a starting point and how much value we have to here we need to increment or decrement we
have to decrement so every time I value decrement by one so I say I minus minus and what could be
the condition whenever I value becomes one we need to stop so what is the condition here is two
conditions we can put either I greater than 0 this is a one condition or we can say I less greater
than or equal to one this is also correct both conditions are correct when I say I greater than
zero whenever I value becomes zero the condition false then it will stop repeating I greater than
or equal to 1 so greater than or equal to one means one is also true it will print after that
I value becomes Z 0 equal to Z condition become false so either you can use condition this one or
you can use condition this one I greater than zero or I greater than or equal to one anything is fine
as soon as the condition is satisfied as soon as condition not satisfied then it will exit from the
looping statement okay now accordingly let us try to write a looping statement print 1 to 10 to 1
in descending order now in I equal to 10 starting point while the condition so what is the condition
I say I greater than Z I'm taking and here just print I value and immediately try to decrease
a value I minus minus I minus minus okay now execute okay now we can see 10 to one in
descending order so descending order we got all the values okay so this is how we can simply
use while loop so by using this while Lo we can practice so many examples so many programs so
many practices we can do in different scenarios and basically what is the purpose of the loop
we can repeat the statements multiple times based upon certain condition and three things
are important initialization where we have to start condition defines how many times we have
to repeat and then incrementation or decrement we required so why decrement incrementation
is required because if I don't decrement or if I don't increment this condition never become
false if this condition never become false means what this will go to infinite Loop it will
keep on executing so we have to make this condition false at certain point of time so
for that we have to increase or decrease the value and then this condition become false and
as soon as this condition become false then it will exit from the while loop okay and one more
thing we can also use conditional statements inside the looping statements we can also
use conditional statements inside the looping statements I have still not discussed about
for Loop right why you asking that question so I have still not introduced for Loop I'm
still discussing about while okay so let me complete and then ask the questions so don't go
beyond the concept guys it will unnecessarily divert the concept okay so many people don't
about the F Loop just wait for some time and then uh I'll clarify that question okay fine no
problem so guys you understood about while loop so far everyone please confirm in the chart
window while loop I'm asking about only while loop okay okay great so I will give you some more
examples for practice so then you will understand many you can do lot of practices okay I will
give list of programs using while loop just practice those programs okay they are more than
enough then you will be more familiar with the while loop okay now let us move on to the another
type of loop called do while loop the next Loop so do while loop and all three Loops are required
three things initialization find condition and incrementation these three things are required for
every Loop okay so now let's talk about do while loop do while loop next one do while loop so what
is the syntax of this do in the bracket we specify the statements and incrementation or decrement
and after closing the bracket we have to write a while here we have to to put one condition and
then semicolon is required guys major differences are there between y Loop and do while loop I'll
explain what are the differences so syntactical differences are what in the while loop first we
need to start with the V with the condition so first we will start with the V with the condition
in the dowi loop first we will start with the do keyword end with the vop okay and in the while
loop we don't put semicolon here we should not put semicolon here okay because that representing
end of the statement so rest of the part will not be executed so should not be put semicolon but in
the do while loop we have to put semicolon for the while because after this while condition there
are no more statements are there so we have to put the semicolon for the while condition this is
syntactical difference and it will start with the do but another major difference in these two
Loops are in the v Loop first it will check the condition if it is true if it is satisfied
then only the statements will be executed but in the dowi loop without checking any condition at
least once these statements are executed at least once no condition is required without checking any
condition by default these statements will execute at least once and then onwards it will check the
condition after execution of the statement first round from the second round the condition will be
validated so first it will directly enter into the block without checking condition and execute the
statement and then condition it will check again it again it will go up and execute the statements
and check the condition this is exactly opposite of while loop in the v Loop first we check the
condition and then execute the statements in the dowi loop first statements will be executed and
then condition will be verified okay so I will also show the differences first let us understand
practically so whatever we have done through while loop same thing we can do by using do Loop also
and same thing we can do by using for Loop also because the ultimate the purpose of looping
statement is same you can use either while loop or do y Loop or for Loop whichever Loop we
are using the purpose is same so whatever we can do by using while loop same thing we can do by
using do while same thing we can do by using fur Loop also so now let us see how can we use do
while with some examples and then I will also show you one more example uh while comparing while
loop and dowi loop okay so let's take a new class and uh do while loop main method and say finish
okay same examples whatever I have done in the Y Loop same thing I'll try to repeat in the but very
less number of times we prefer to this Loop and uh we'll see okay first one to 10 mbers I want to
print one to 10 numbers using do while loop okay so what is the syntax is first we need to start
with the do and bracket and here after closing the bracket we have to use while and here we need
to put the condition what's the condition is what before that we need to do initialization right
in I equal to one is the initialization and do block is started in the while loop I less than
or equal to 10 and then semicolon so this is a syntax now inside this block we have to write the
statement so I'll print the I value and then I'll increment the I value it is same it is exactly the
same guys only the syntax is difference okay now observe this Java application so one to 10 numbers
is got printed so how it is got executing first I value one and directly it is entered into the
block do block because we are not checking any condition at the first round so it will directly
enter into the block printed I value one i++ is become two so here it is printed I value one first
then here i+ Min means I value becomes two and now here it will check the condition so current value
of I is what two two less than or equal to 10 or not true if it is a true then only it will go
to do again and again execute the statement and current value of I is two now I becomes three here
now three less than or equal to 10 Again condition is true now again it will go up and execute the
statement print three now I value becomes four again condition is true again it will go up and
print the statement so it will go on like this till 10 and now i++ I value becomes 11 for example
I value becomes 11 now condition become false 11 less than or equal to 10 condition become false
then it will stop repeating the loop so this is how simply we can write do while loop do while
loop and whatever we do using while loop same thing we can do by using doy Loop also so for
example if you want to uh decrease the value suppose I want to print one to 10 numbers in
descending order let's say I want to print 10 comma 9 comma 8 up to 1 and same thing we can do
in starting point is what 10 starting point is 10 and do after closing the bracket while condition
what's the condition here what's the condition I want to print 10 to one in descending order
what could be the condition what would be the condition please respond so I
10 to one 10 to one I want to print see still some people have not understood
see 10 to one numbers I want to print observe so first number 10 9 8 one is the last right
starting point is what 10 every time I'll increase I'll decrease by one so I minus minus so
whenever I value becomes one then I want to stop that means what either I greater than or equal to
one this is the condition or I can say I greater than zero also fine because all these numbers
are greater than zero only right so as soon as I value becomes zero then this condition become
false or this condition become false so either way you can put two different conditions one of
the condition you can use so I can say I greater than zero or I can say I greater than or equal
to one semicolon is required okay now what is the statement we have to keep inside this system
do do print Ln directly print I value and after that how much value we have to decrement minus
one iusus iusus so now this this will print 10 to 1 in the descending order okay so this is simple
syntax of do while loop in the do while loop first statements will be executed at least once without
checking any condition and after execution of the statements then the condition will be verified
then condition will be verified so that's the major difference between while and dual in the
while first check the condition then statements will be executed me do Val for statements will be
executed then check the statement okay now let us compare while and D what's the difference in the
output let's take another class while versus do while while versus do while Loops same program I
will write in two different uh Loops just observe the difference in output simple int I equal
to one okay in I equal to one and I'm writing while loop directly while I less than or equal
to five five numbers I just want to print one to five numbers system. print Ln I I value I'm
Printing and then I value I'm printing then I'll increment I value by one i++ okay this is the Y
Loop so when I execute this it is printed one to five numbers okay perfectly fine this is a while
loop now I'm writing the do Loop let's keep this command first same program I'll just try to write
by using do Loop enter I equal to 1 I'm taking do okay so do while I less than or equal to
five and then semicolon is required and here system. Pinn I value and then every time I'll
increment I value so if I execute this code this will also do the same thing and you will get
one to five numbers so one to five numbers you will get output okay but where you can see the
difference just observe this what I will do is again go back to the while loop in the while loop
I'll put initial value initial value I I'll keep 10 now tell me the output of the V Loop what is an
output you will get I I value 10 I value 10 so the value will print any values for us will it print
anything for us let's try to execute and nothing there is no output why because the condition
false at the first starting point itself is got failed so I equal to 10 10 less than or equal
to five condition false if the condition is false the statements are not at all executed so first
it will check the condition then statements are executed right because when I say I equal to 10
the condition is false so the statements are not executed this is the behavior of while loop now
let us repeat the same thing for do while loop so here I'm making I value equal to 10 I value equal
to 10 now if I look at the output it will print at least one round of execution will happen so I
just see 10 is got printed why because it is not checking the condition in the first round it will
not check whether I value is less than or equal to five or not so these statements will execute
at least once and after that condition will be verified so first it is printed 10 here directly
then it is incremented and becomes 11 and then here checking the condition 11 less than or equal
to five condition is false so rest of the rounds is not executed it is exited from the do while
loop so that is the major difference between while loop and two while loop in the while loop
if condition is not satisfied the statements will never execute but in the duy loop even though
the condition is not satisfied the statements will be executed at least once at least once the
statements will be executed okay so I understood the difference between while loop and do while
loop execution wise the while loop condition will be verified and then statements will be executed
in the dowi loop first statements will be executed then condition will be verified that means the
statements at least once they will execute in the DU Loop okay this is a major difference between
while and do while now next we'll move on to the next type of loop called for Loop the late again
once you completed the for Loop then again we will compare all three Loops okay so this is a basic
difference between while and do while now the next Loop is for Loop this is very very important and
very useful Loop 80 to 90% of the times we will prefer to use for Loop okay most of the times up
to 90% of the cases we will try to use for Loop instead of using do while and while loop but what
is an advantage of for Loop when you compare with other Loops what is the main benefit of f Loop
so in the v Loop and du loop at least we have to write three statements for conditional specify
the initialization is one statement specify the condition is another statement incrementation
or decrement is another statement at least three statements we have to write in every Loop right in
say one statement second statement third statement these three we have to write in three different
lines even in the Dual also decrement condition and before starting do while we have to write the
initialization so for these three things we have to at least write three different lines but in the
F Loop we can write all these three in one single line in the for Loop we can specify initialization
condition and incrementation in single line that means it will reduce the code it will reduce a lot
of code suppose simple reason I'll tell you so let us say you have written 100 lines of code 100
lines of code in this you have used V Loop five times five times you have written while loop five
different scenarios so total how many statements at least three things we have to write right
conditional initialization and incrementation 3 into five at least 15 statements you have to
write here 15 statements total you have written but same 100 lines code if I use f Loop five
times if I use f Loop five times because all condition initialization incrementation we put
only one single line so how many statements we can write here only five statements so lot of
code is reduced so whenever we are using for Loop it will reduce the code number of lines
it will reduce and it is also more simpler to use when you compare with the while loop and
while loop and that is the reason we will try to prefer the F Loop many times okay now let us
see the syntax of the F Loop how we can specify initialization condition incrementation in single
line so the syntax is like this four Open Bracket closing bracket in this bracket we will write the
statements whatever you want to repeat multiple times but in this first part there three parts
are there in the for Loop the first part is what initialization initialization semicolon second
part is what condition semicolon third part is what incrementation or decrement so three parts we
will include in the for Loop Itself by separating semicolon so first part is called initialization
second part is called condition third part is called incrementation or decrement and statements
and one more uh difference you can notice here in the Y Loop and du Loop incrementation or decrement
is a part of the body part of the block right but this time incrementation decrement is not part of
the body it is a part of the definition you can see here we have to mention and uh in the previous
Loops initialization should be done before starting the loop before starting the loop we
have to do initialization but here initialization is also part of the for Loop the is also part
of the for Loop so we don't need to write a separate statements for initialization condition
and incrementation or decrement all three we can include in single statement together this is
a syntax okay now let's see how it is going to execute how exactly for Loop works so let's try to
understand the flow and then uh we will see some examples okay so this is a for Loop initialization
condition incrementation so the workflow is like this first it will start from the initialization
this is the first one first step initialization once the initialization is over then it will
check the condition Second Step then it will check the condition if the condition is satisfied
if the condition is true directly it will go and execute the statements if the condition is true
go to the state and execute after statements are executed then it will go to the third part
incrementation or decrement so first second step this is a third step and this is a fourth
step after statements are executed automatically it will go to the third thirdd section then
incrementation or decrement is happened once it is happened then again check the condition
fifth step and if the condition is true again then statements will be executed and again it will
go to incrementation or decrement part after that again check the condition and statements will be
executed again incrementation condition statements again incrementation again condition statements
that means what initialization will execute only once first time only once it will execute now
after that condition statements and incrementation again condition statements and incrementation
these three things will be repeated mult multiple times till this condition become false so as soon
as this condition become false then F Loop will be exited okay this is a workflow of for Loop
initialization first it will execute only once then condition will be verified and statements
will execute if the condition is true statements will execute after execution of the statement
then again go to the incrementation and then again check the condition true again statements
will be executed go to incrementation check check the condition again true again statements will be
executed so incrementation condition statements again incrementation condition statements will
be repeated multiple times till this condition become false so that's how the F Loop works okay
so only condition and incrementation will repeated multiple times initialization will execute only
once okay so this is how exactly for Loop works now I'll show you some examples of for Loop let's
go back to the eclipse and create new class and I can say F Loop demo take this main method and say
finish okay so now let me show you some examples by using follow this is the most important Loop
many times we prefer to use okay so first thing let's try to print one to 10 numbers using F Loop
one to 10 numbers using F Loop so we can directly write a looping statement because we don't need to
write initialization condition separately directly you can write Fun Loop this is a syntax and even
F Loop also don't have a semicolon here you should not put semicolon for the F Loop if you put the
semicolon these statements will not be executed okay so remember this only do while loop while is
having semicolon in the while loop don't have any semicolon for the while and even for for Loop
also we don't have any semicolon for the for Loop okay now in the for Loop we have to specify
three things one is what initialization how to write initialization in I equal to 1 this is the
initialization starting point semicolon second part is what condition I less than or equal to 10
is a condition semicolon how many times we have to repeat how much value we have to repeat every
iteration one I Plus+ that's it now come to the block and just write only print statement just
print I that's it simple now when I execute this it will print one to 10 numbers so incrementation
is not part of the body in the previous Loops incrementation is a part of the body means after
printing the I value we have incremented the I value immediately but here it will automatic
process as soon as the statement is got executed next round I value will be incremented
so again let me repeat the workflow first thing initialization will happen and then condition is
verified true if it is true statement will execute and then go to the iteration and then again check
the condition statement is executed then go here so these three things will be executed multiple
times condition statement and iteration so these three will execute multiple times so when it will
stop as soon as this condition become false then it will exit from the forl then it will stop for
Lo okay so let me try to execute again so when I execute this you will get one to 10 numbers
so all three we can include within single line so that's the main advantage of using for Loop
let me show you some more examples so comment this so let's say I want to print only
even numbers between 1 to 10 1 to 10 even numbers 1 to 10 even numbers only even
numbers okay now how to write a looping statement you have to tell now tell me what
is the initial point I want to print only even numbers what is the starting even
number two so initialization is what in I equal to two okay fine now what's the next
condition what is the condition what is the condition so 10 times you have to repeat right
so I less than or equal to 10 now how much value we have to increment how much value you have to
increment two so here you can write I equal to I + 2 or you can directly write Shand operator
I plus equal to two also you can write so this time we are incrementing by two every time and
then directly print system print and I value so this will print only even numbers between 1 to 10
can just look at here only even numbers between 1 to 10 okay now let's see another example suppose
I want to print one to 10 numbers which includes even and odd number so just like this in the
while loop we have seen right so we can see like this this is a one so this is the example
so 1 to 10 numbers between 1 to 10 I will print every number along with even or odd every number
along with even or odd using F Loop next example so one to 10 numbers I want to print one to 10
numbers but every number should print like this 1 2 3 4 5 6 up to 10 along with each even or
odd number okay now how we can write a looping statement I'm commenting this don't
confuse by seeing this is example one okay and this is example two this is example
three okay now you can just guide me how to write a for Loop and this time we have to to put
conditions also conditional statements inside the F Loop right so first let us start writing the F
Loop where is the starting point I into I equal to 1 I less than or equal to 10 I ++ okay now before
printing the number we have to check whether it is even number or odd number if it is even number
we have to print number along with even if it is odd number we have to print the number along
with the order so we need to put one if condition if if I percent 2 equal to Z what does it mean
it's a even number so here we will print number I value along with this some text let's say even
else else what does it mean if this condition is not match that number is what odd number so here
you can say system print and print I along with odd that's it execute now so you can just see
it will print even or odd numbers between 1 to 10 one is odd number two is even number like
this up to 10 it is got print so we can also use conditional statements inside the loop okay
now I want to decrease the values I want to print 10 to one 1 to 10 in descending order another
example example four 10 to one and descending order okay 10 to one in the
descending order so now how to write a looping statement
for and directly write for Loop for starting point what is the starting point
int I equal to 10 is a starting point now what's the condition I greater than zero or I greater
than or equal to one anything is fine I minus minus we need to decrease the value so directly
you can print system do print Ln print I value directly so now we got a numbers in descending
order okay so this is very simple one when you compare with the while and do Loop for Loop is
more simpler than while loop and do while loop okay so with this understanding we can practice
so many examp examp there are so many situations we will try to use this looping statements
so many problems we can solve by using this looping statements either while loop or do
Loop or for Loop whatever it is okay so now we have discussed about three different looping
statements y Loop doy Loop and while for Loop so now we need to understand where exactly we have
to use while loop where we have to prefer to use doile and where we have to prefer for Loop okay
depends upon certain condition so which Loop we have to prefer which Loop we have to prefer so
in many interviews people may ask so there are multiple conditions are there okay so the first
thing which Loop we have to prefer so first thing if you want to use while loop if you know the
number of iterations in advance the for Loop is often the most suitable so suppose if you know
how many number of times we have to repeat the loop before itself sometimes you don't know how
many times you have to repeat okay if you have if you know already how many times you have to
repeat the loop then obviously we can directly go with the for Loop without second question directly
can go with the F Loop if you know how many times you want to repeat the loop if you know that you
can directly go to the for Loop itself okay the second thing suppose if you want to execute the
loop at least once without checking any condition okay if you want to execute the statement at least
once without checking any condition then you can go with the do while loop because we don't have
any other way even for Loop and while loop also first check the condition then statements will be
executed but if there are any scenario like if you want to execute the statements without checking
any conditions at least once then directly go with the do Loop okay and sometimes suppose
if you don't know how many times you have to repeat the loop you don't know the condition you
don't know how many times you have to repeat the loop but based upon certain condition internally
you have to exit from the loop if you don't know how many times you have to repeat then you can go
with the while loop but you may get one question here if you don't know how many times we have to
repeat the loop if we don't know the condition how can we use while loop right if I don't know how
many times we have to repeat how can we use file loop I will show you a simple example and in those
cases we have to use y Loop so if you know number of iterations go with the for Loop and if you
want to execute statement at least once without checking any condition go with the doy Loop and if
it don't know the condition then go with the vile Loop okay but without condition how can we write
a v Loop I'll show a simple example tell look at here so I'm just creating a simple example so if
you don't know the condition for example then how can we use while loop while L without condition
while loop without condition only while loop it is possible in the rest of the loops we have to
specify the condition okay so in the while loop without having condition how it is possible just
observe here I'm writing one while loop while loop and here I'm keeping true true means what every
time the condition is true right so here I can say okay uh let me print ciso I'm just printing
something called Hello message I'm printing okay okay fine and here what I said okay so let me take
one value in I equal to let us say uh let us say 100 let's me I equal to let's say one starting
from one in I equal to one okay now normally if you want to print this hello mes 10 times what is
the condition you will put I less than or equal to 10 so this is a condition you will put so when I
execute this it will directly print one on to 10 numbers also see we are not put incrementation or
decrement so infinite Loop so we need to increment I value by one so if I execute this it will print
Hello message 10 times perfectly fine suppose here I have I don't put any condition okay let's keep
true that means what every time the condition is satisfied every time the condition is satisfied
right if I execute this what will happen when I say true here i++ I'm not incrementing also
so then what happens what happens it will go to infinite Loop or not it will go to infinite
Loop keep on printing hello messages so why it is going to infinite Loop because we always making
condition true we always making condition true that's the reason the statement is got executing
even if you put i++ also here still it will go to infinite for still it will go to infinite
for why is it so because even though we are incrementing I value here we are not checking the
condition based on the I value we're not keeping the condition here based on the I value right so
every time this condition is true so still the statements are executing in infinite times but
now my scenario is based on certain condition I want to break the loop based on certain condition
I want to break the loop okay I want to break the loop then how can we do that see as soon as you
said i++ here I will put one condition if I value if I value equal to equal to let's say 10 as soon
as I value becomes a 10 then what I will say here I will use something called Break statement
break then what happens what happens here so first condition is true hell is got printed I
value keeps increment before putting this if condition what happens it will go to infinite for
Loop so the same messages got printed n number of times even though this I value is incrementing it
is not considering because the condition is always true condition is always true we are not making
this condition false at all so after that what I have done suppose if the I value is equal to 10
whenever I value becomes 10 then I'm break the Lo so what is a breaking is a jump in statement
actually so that will come out from the V Looper so if you execute this code observe this see only
10 times it is got printed this time you observe only 10 times it has got printed so what exactly
Happening Here is even though we are not making this condition false actually when this while
loop should be exited when the while loop will be exited if this condition is false then only
the Y Loop is got exited right if the condition never false never become false means it will go
to infinite Loop but uh what I have done here is I'm not making this condition false but still
I'm exiting from the while loop without making this condition false still I'm coming out from
the while loop how I making how I'm coming out from the while loop because of this condition but
here I don't know how many times I want to repeat I don't know how many times I've repeat so I put
I don't put any condition here but still based on certain condition I can exit the loop based on
certain condition I can exit the loop without making this condition false that means what even
though we don't know how many number of times you want to iterate still you can use while loop
but other Loops it is not possible only in the Y Loop it is possible okay so if you know number
of iterations exactly directly go with the for Loop there is no secondary question and if you
want to execute the statement without checking any condition at least once go with the do while
loop and if you don't know how many number of times you want to repeat the loop then go with
the while loop and simple generic example I'll tell you guys you can easily understand take two
different type of vehicles I'm taking bike bus and flight two three different type of vehicle
okay so if you want to travel by bike what are the things you required if you want to travel
by bike you need to know where exactly you are starting Source location you have to know where
you are going to travel destination point you have to know and also you need to know how much
of fuel you have in your bike so you have to know Source whenever you are traveling through
bike you will know all these things right you have to plan accordingly so you know where you
are starting you know where exactly you want to go and how much fuel is required to reach the
destination so all three things you will check right so if you know these three things means what
starting point initialization incrementation means destination point and condition all three if
you know directly you can go with the F Loop this is a f Loop example actually if you know
all these things exactly you can go with the F Loop now suppose if you want to travel by bus
what is the criteria you don't need to buy any ticket for that right so directly you can get into
the bus and then you can buy the ticket you can directly get into the bus and then you can buy the
ticket that means what do while so do while loop could directly execute once the statement is got
executed then condition will be verified so same thing will happen here if you want to travel by
bus you can directly get into the bus after that you can buy the ticket means what without checking
any condition at least you are executing statement once in the DU you can just relate the scenario
that's the reason I'm just giving this example okay so if you want to traveler by bus you don't
need to buy the ticket that means you don't need to satisfy the condition first you can get
into the bus and then buy the ticket buying the ticket is what satisfying the condition same
in the do Loop also we do the same thing first we'll execute the statements and then checking
for the condition now if you want to travel by flight if you want to travel by the flight
you have to satisfy the condition first you have to to satisfy the means you have to buy the
ticket first and then you can get into the flight right that means what while rupes pra while is
nothing but first condition will be satisfied then statements will be executed same thing
if you want to get into the flight first you need to get the ticket and then you can get into
the flight just relate that example just we will understand based on this example I'm just giving
some example like this okay so if you know Source destination Fel everything clear and then you
prefer the follow that means what you have to know initialization point you have to know the
condition you have to know the incrementation part if you know these three things directly
go with the F Loop there is no second question directly you can go with the F Loop okay and if
you want to execute certain statement without checking any condition then directly go with
the L while loop and uh if you want to satisfy the condition first and then execute the set
of statement then go with the V Loop and also sometimes if we don't know the condition in those
cases also we can go with the V Loop so with this example I hope you get clear Clarity on this three
different looping statement but 80 to 90% of the times F Loop will be preferred that's the first
priority second one while loop will be preferred some cases and do Loop is very very rare case
and we never use du most of the times okay so these are the three different type of looping
statements so so is this clear everyone while loop do while and for loop I will give you more
number of assignments today based on this looping statements you have to practice them okay now let
us jump to the last one jumping statements uh we have jumping statement break and continue very
simple very easy we can use them along with the looping statements itself so jumping statements
so we have only two uh statements bre break and contingent so break we have already used in
the switch case statement that is the jumping statement actually so today we will understand
more about this break and continue so these are the two statements comes under jumping
statements okay so let me show you how to use them and uh so most of the times we use these
two commands along with the looping statements or conditional statements okay break and continue
most of the times uh not continue break break is especially used for conditional statements as
well as looping statements but continue statement most of the times we use only with the looping
statements okay remember that so I will show you example simple example let's go and create a
new class first let us start with the break break statement take this main method and then
finish break statement okay now I will write some piece of code here you guys can
tell me what is an output of this okay for I'm directly writing one F Loop in I equal
to 1 initialization I less than or equal to 10 condition I ++ incrementing I value
by 1 okay now here if I print okay so here I'm keeping one conditional statement if if
I value is equal to five if I value equal to five I'm using break command here and if
I value not equal to five then I'm trying to print the I value now tell me the output
of this program what is an output just guess it so the for Loop contains the if condition
so I'm trying to print one to 10 numbers but inside this I put one condition I equal
to five so what is an output of this code okay let's try to execute see run as Java
application so we got one to four even five is also not get printed after 5 6 7 8 9 10 all
numbers are got skipped they have not printed only one 12 four is got printed see how it is
going to work initial initialization point is what one so what is Our intention here one to
10 numbers I want to print one to 10 numbers because I put condition 10 right so one to 10
numbers I want to print that's my intention but internally what I'm doing this condition
is filtering basically whenever I become five means what one is got printed I equal to one
condition not satisfied one is got printed two got printed three got printed four is also
printed but as soon as I value become five what happens the above condition is true the F
Loop condition is true five less than or equal 10 is true so it is entered into the block but
here what happens 5 equal to 5 becomes true that means if condition become true satisfied then
as soon as this if condition is satisfied then what happens the break command executed
what this break command will do this will exit the looping statement it will basically
break the loop okay break command will break the entire Loop so as soon as I value become
five I'm breaking the loop it will come out from the loop if it is come out from the loop
these statements will never repeat again as soon as you break the loop the next statements
whatever we are having repeat for reputation those statements will automatically skipped so
basically it is jumping out from the block or it is breaking the loop that is the reason we call
it as a jumping statement call it as a jumping statement I'll repeat this guys once you go to the
next topic don't repeat the previous one if you want to repeat the previous one just hold on for
some time I will clarify that okay so just focus on because if you're still thinking about the
previous one you cannot understand the current one so currently I'm explaining something different
right break is a totally different concept and you are asking about previous example wait for some
time I'll come to that part okay so I'll repeat this once again just listen this initially what
is an I value one it will enter condition is also true now come inside if condition not satisfied
so I is got executed so here one is got printed then i++ I value becomes two 2 less than or equal
10 condition is true inside the block if condition false so this statement is got executed two is
got executed next time I value three again if condition is false so I value executed three third
round I value four again if condition is false so here print state statement is got executed four
next time I value five here and this condition is true even if condition is also true whenever
this if condition becomes true what happens the brake command is executed that will exit from the
for Loop immediately if the break is existed from the for Loop and the rest of the numbers will not
be executed even though this condition still there but the break command is jump out from this for
Loop it will not go to the further values because as soon as I value equal to five immediately we
are coming out from the loop if come out from the loop means what rest of the numbers will not
be executed okay so this is how it will execute exactly works the break command basically it will
jump out from the program if you want to break the loop even though we have multiple iterations
here see one to 10 times 10 times it repeats actually but how many times it is repeating now
only four times how it is possible because based on the condition we are breaking the loop
based on the condition we are breaking the loop okay understood guys so how it is exiting
the for loop as soon as this condition is satisfied the break is got executed and that
exited the for Loop and rest of the values are not printed okay so if you print the i in
the same break just below the break statement okay now let us see this what will happen you
guys can tell me the output for this if I put this I value inside this break after the break
then what happens what happens I just remove this it is a giving syntax error means what
after breaking statement we should not execute any other statement okay because the break
itself will exit from the loop right it will jump jump out from the loop so this statement
will not be valid statement after the break this is not valid statement unreachable code
this is the error you will get unreachable code means what as soon as we have exited
the break statement next statement will not be continued so even though if you write
any other statement that is not considered so Java will give you syntax error so you have to
make sure that after break you should not put any statements okay so when you execute
this uh it will execute only one to four numbers okay just a second just a moment guys let me remove
the points it is going to coverage model so guys understood about the break command
so how we can exit the loop even though the for Loop condition is true so this condition is
true I less than equal to 5 it is true but still the if condition is causing the break
so it is basically breaking the for Loop so continue is another statement which is exactly
opposite of this okay now let me just copy the same the last example continue statement continue
so same code instead of a break I will just say say only continue so now you tell me what is an
output of this continue now what is an output of this one to 10 numbers I want to print but
whenever I Val equal to five I say continue whenever I value equal to five is say continue
okay now observe this output when I run this code it is printed 1 to 10 numbers but you can see
five is got missed five is got skipped actually five is not got printed the rest of the numbers it
is printed so why five is got skipped here so just observe so whenever the first starting with one
right so one 2 3 four there is no problem but as soon as I value become five so if condition true
if condition becomes true so as soon as this if condition is true then continue will directly go
here okay okay continue will directly go here it will not print see as soon as I become five
continue will execute continue will directly jump to the incrementation part so this statement
is ignored especially whenever I value become five the statement will not execute so five we will
not get after that I value become six right so six seven 8 9 10 this condition never match so
obviously it will print rest of the number 6 7 8 9 10 only in case of five only in case of
five it will continue this only in case of five the statement is not executed and rest
of the numbers it will go and execute okay now I want to skip three five and N now tell me
how to write the condition I want to skip three five and N 3 five and nine three numbers I want
to skip simple can change the condition I equal to three r r operator r i equal to 5 or I = to
9 so what happens whenever I value becomes three continue go to the next number whenever I value
equal to five skip it and go to the next number whenever I value equal to 9 continue and skip
it so except 3 five and 9 you will get all the numbers three five9 are got skipped 3 five 9 all
three numbers got skipped that is called continue so continue is also jumping statement because we
are directly jump to the next number by ignoring the current value we are going to the next number
Next Level so as soon as the I value becomes these three three five or nine jump to the next number
that is called continue but the values will not print because the print statement is outside of
this if condition so values will not be printed as soon as these values are satisfied then it will
go and continue to the next number so that's how we can use continue so break and continue so
these two statements most of the times we use along with the looping and conditional statements
together okay simple example so in the coming sessions we will see so many examples almost in
every program we will use uh looping statements jumping statement conditional statement we'll
see so many things okay so this is all about control statements in Java so what are all
control statements in Java supported three types of control statements we discussed one is
first category is conditional statements let me summarize conditional statements so what are
the conditional statements in Java supported if condition if else condition nested if
else condition switch case statement if condition if condition if else condition
n def condition switch case statement and second category looping or iterative statements
looping or iterative statements while loop do while loop for Loop so there is another flavor
of for Loop is there so that we can use with concept tomorrow session the next session I
will explain about enhanced F Loop and third category jumping statements jumping statements
break and continue so these are called jumping statements okay guys understood so yesterday
I have given some assignments on conditional statements right so today I will give some more
assignments uh when you come to the assignment part very very important assignments most of the
interviews people will ask these type of questions I will also give the solution here you can just go
through it okay now the first assignment reverse a number the first I will also just give the
clue on this you guys can try to use reverse a number if I give some number as an input our
program should reverse it and provide the output for example when I say 1 2 3 4 is a number the
program should display 4 321 whatever number we have given to the program or whatever number
we have declared the program should reverse that number and provide that output that is
called reverse a number okay here we have to use percent operator slash operator equal so
these are the operators which we have to use along with the looping statement along with the
looping statement so for every program there is internal logic is is present so we need to First
understand the logic if you know the logic you can easily build the program even during the interview
also even if you're not able to write the code at least you have to explain the logic so I'll tell
you simple example let's say one two 34 is there and how we can reverse the number through
programmatically how we can write the logic let's say 1 two 34 is my number so what we need
to do is we have to take this is actual number okay so we need to take one variable a temporary
variable let say number or you can say reverse number okay this is the number actual number
is this one and reverse number so from the actual number we need to First capture the last
digit four get this digit and add to the reverse number first and then remove this number and next
capture the three and concatenate here and then remove this number then the next number is two so
let that get the two and add it here and remove this two the next one one get this number here add
it here and remove the so now number becomes zero as soon as the number becom zero we have to stop
getting the numbers now finally you will get the reverse number here but here two things are there
we are doing two things continuously what is this we are getting the last digit every time after
getting the last digit we have to remove this number and rest of 1 two three will be there
in the number after getting three we have to remove the three then one two will be there after
getting two we have to remove the two then we have one after getting the one we have to remove one
also then number becomes zero as number becomes zero we have to stop repeating Val so two things
are happening here capturing the last number and removing the digit immediately so to capture
the last digit number we use something called percentile operator and to remove this last digit
number we use something called slash Operator by using the combination of these two operators
we can capture the last digit and remove the last digit so we need to repeat like this till the
number value becomes zero so this is actual logic so in detail I have explained in this video I have
given the video link also you can just go through the video I've explained how to reverse a string
in very simple manner or try yourself first while we have to use a looping statement along with the
operators okay so just go through this video you will understand how to reverse a number very very
popular program most of the Java interviews people will ask this type of question reverse a number
now the next one is palindrome number whenever you provide a number as an input after reversing this
number if you get the same number that is called palindrome for example I say one to one after
reversing you will get only one to one so this is a palr number so you need to find out the number
is palr number or not through programmatically this is again related to reverse number if you
know how to reverse a number after reversing compare verginal number with reverse number
if both are equal then you can tell number is a palen r if both are not equal number is not pal
number so this is actually enhancement code of the previous one if you know first we need to reverse
a number after reversing the number then we have to compare reverse number with original number
if both are equal we can decide which is a parent RO number or El no so in this you have to use
conditional statements conditional statements plus looping also looping statements also you need to
use so I will provide the link later here so you can just try because you can easily do it because
what is the difference between first one second one here you need to additionally add one if
condition because you you want to compare original number reverse number for that you need to
additionally add one if condition otherwise rest of them exactly the same now the third program
count number of digits in a number count number of digits in a number suppose if he provide some
number like this and how many digits are there is in this number total number of digits one 2 3 four
five six digits are there so the programmatically you need to tell how many digits are there total
six digits are there similar if you provide 10 digits number the program should say 10 digits
and again this is also related to the first one so we need to capture the last number every time
and we need to count so here we have to use again looping statements and this is the video link
I have given how to count number of digits in a number you can just go to this video and these are
all assignments guys you have to practice practice you have to do your own first and then uh to get
understanding go through the video next one count number of even and odd digits in a number so in
the previous example we have to find total number of digits but in this example we have to find
total number of even digits total number of odd digits and if I look at this number how many even
numbers are there in this two is one even number four is even number six is even number so three
even numbers are there so three even numbers and how many odd numbers we have here four and five
two odd numbers we have so two odd numbers so we have to get output like this print the number of
even numbers number of odd numbers this is the video link go through it now the fifth one find
the sum of digits in a number so this time we we we are not counting the numbers we are finding
the sum of digits means what 1 2 3 4 I have given so it will find some 1+ 2 + 3 + 4 total is 10 like
this you can give any number of digits even if you give 10 digits it will find sum of all the digits
and finally it will give you total of or sum of all the digits in a number and this is a video
link I have provided so you can just go to the video then you can understand okay so these are
the five assignments you have to do uh based upon today's concept concept and yesterday Concepts
conditional statements and looping statements so by using these two type of statements you can
practice these examples five different type of assignments I have given along with the solutions
so video links also I have given so go through and finish this okay so this is a for assignment so
you will gets a lot of time tomorrow and day after tomorrow during the weekend so before coming to
to the next class on Monday so please finish all those things take your own time and spend good
amount of time because you will have lot of time during the weekend so spend amount of time and
then finish all these things okay so otherwise it will be very difficult to understand the coming
sessions so you have to practice this you have to complete this before coming to the next session
so I will see all the Solutions in your Google Drive so I already told you how to upload all the
Solutions in your Google Drive so once you have done the practice upload everything in the Google
Drive and then I will check during the weekend where is done and where is not done I will recheck
once okay so before attending the next session you have to practice all these things and get ready
with the next concept so that is our plan okay so then I'll stop here for today's session and
we'll complete we'll continue in the next session
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact