Session 5- Working with Java Loops & Jump Statements | … — Transcript

Learn Java loops and jump statements with examples of while, do-while, for, and enhanced for loops to reduce code duplication and improve efficiency.

Key Takeaways

  • Loops allow repetitive execution of code without duplication.
  • Java provides multiple loop types suited for different scenarios.
  • Initialization, condition, and increment/decrement are essential parts of any loop.
  • Enhanced for loop is designed for iterating over collections.
  • Using loops reduces code size and improves maintainability.

Summary

  • Introduction to control statements in Java, focusing on looping or iterative statements.
  • Explanation of what a loop is: executing a group of statements multiple times without duplication.
  • Overview of four types of loops in Java: while loop, do-while loop, for loop, and enhanced for loop (for-each loop).
  • Enhanced for loop is especially useful for collections like arrays, ArrayLists, and HashMaps.
  • Detailed discussion on three main loops: while, do-while, and for loops with examples.
  • Use case example: printing numbers from 1 to 10 using loops instead of multiple print statements.
  • Advantages of loops include reducing code size and avoiding duplication.
  • Three critical components of any loop: initialization (starting point), condition (how many times to repeat), and incrementation/decrementation (how the loop variable changes each iteration).
  • Emphasis on understanding these three components for writing effective loops.
  • Loops help automate repetitive tasks efficiently in Java programming.

Full Transcript — Download SRT & Markdown

00:02
Speaker A
Okay, so we are discussing control statements in Java. In the last class, we have seen conditional statements: if condition, if-else condition, nested if-else, and then switch case statement. So, they all come under conditional statements. Based on the condition, we can execute a set or group of statements. Now, today we are going to discuss another type of control statements called looping statements, looping or iterative statements.
00:19
Speaker A
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
00:40
Speaker A
Looping or iterative statements. So first, we need to understand what is a loop and what is iteration. A loop is nothing but a set of statements or group of statements which we are going to execute multiple times or repetitively. That is basically called a loop. Suppose you have written a program for 10 lines or 50 lines and you want to execute certain number of lines multiple times. You want to repeat. So, without writing the same statement again and again, duplicated, you write the same statements only once but want to repeat multiple times. In those cases, we can try to use looping statements.
00:58
Speaker A
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
01:16
Speaker A
Okay, there are three different loops available in Java. While loop is one type of loop, and do-while, and then for loop. So these are the three different types of loops available in Java. And there is one more loop which is also called enhanced for loop. 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 for-each loop. This is especially designed for collections. Whenever you're working with arrays concept or ArrayList or HashMap, those types of concepts, whenever you're using, we have to prefer this enhanced for loop or for-each loop.
01:40
Speaker A
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
02:01
Speaker A
Okay, so we will discuss this later. When we discuss the array concept, I will introduce you how we can work with enhanced for loop. Now, today we are going to discuss about three different loops: while loop, do-while loop, and then for loop.
02:18
Speaker A
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
02:38
Speaker A
Okay, so first of all, what is the use of loop? Why do we need to use this looping statement? Let me just give you a simple example. Suppose through a program, I want to print one to ten numbers. Let's say I want to print one to ten numbers through a Java program. So normally, how will we write? You have to write 10 print statements, right? System.out.print(1), then again System.out.print(2), then again System.out.print(3), so like this you need to write up to 10 statements: System.out.print(10). So like this, you need to write 10 statements.
02:57
Speaker A
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
03:16
Speaker A
So if you just want to print one to ten 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.
03:38
Speaker A
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
03:56
Speaker A
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 the 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 the advantage? It will reduce the code. We can reduce the code and we can avoid duplication. We can avoid the duplication and it will automatically reduce the code. So that is the main usage of using looping statement.
04:14
Speaker A
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
04:32
Speaker A
But whenever you're writing or when we use any looping statement, it can be while loop or do-while or for loop, any looping statement, three things are important. 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.
04:51
Speaker A
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
05:12
Speaker A
Condition is how many times we have to repeat. 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.
05:29
Speaker A
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
05:48
Speaker A
And third thing is 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 here, 10 statements, 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.
06:04
Speaker A
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
06:24
Speaker A
So these are the three important things which we have to remember while writing the looping statement: initialization, condition, incrementation or decrement, which is very, very important. Any loop, it can be for loop or while loop or do-while 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 decrementation 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.
06:46
Speaker A
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
06:57
Speaker A
Okay, so simple example, let's say I want to print one to ten numbers. Now what is the initialization, condition, and incrementation? So for example, let's say I want to print one to ten numbers. That's my requirement. Let's say 1, 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 initialization.
07:14
Speaker A
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
07:36
Speaker A
And 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 reaches 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 condition.
07:52
Speaker A
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
08:17
Speaker A
So condition is how many times we have to repeat. Every time how much value we are incrementing? Plus one we are incrementing. So initially I value 1, next value I value 2, I value 3, I value 4, 5, 6, 7 up to 10. It will repeat. As soon as I value becomes 10, it will print the value, then it will exit from the loop.
08:35
Speaker A
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
08:52
Speaker A
So initialization, condition, and incrementation, how much value we want to increment every time. So these are the three important things we have to know before using any looping statement.
09:12
Speaker A
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
09:34
Speaker A
Okay, now first let us start with the while loop. Once you understand all three different types of loops, 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.
09:57
Speaker A
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
10:16
Speaker A
So first initially, we will try to understand about the loops syntaxes and how to use with 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 statements we want to repeat. So those statements we have to keep inside the while loop.
10:32
Speaker A
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
10:46
Speaker A
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 value and inside the value, we need to specify condition that 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...
11:07
Speaker A
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
11:25
Speaker A
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
11:47
Speaker A
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
12:02
Speaker A
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
12:17
Speaker A
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
12:31
Speaker A
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
12:42
Speaker A
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
13:06
Speaker A
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
13:21
Speaker A
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
13:42
Speaker A
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
14:00
Speaker A
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
14:20
Speaker A
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
14:39
Speaker A
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
14:59
Speaker A
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
15:20
Speaker A
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
15:42
Speaker A
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
15:57
Speaker A
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
16:21
Speaker A
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
16:38
Speaker A
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
16:47
Speaker A
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
17:07
Speaker A
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
17:40
Speaker A
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
18:02
Speaker A
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
18:22
Speaker A
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
18:34
Speaker A
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
18:57
Speaker A
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
19:15
Speaker A
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
19:33
Speaker A
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
19:56
Speaker A
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
20:16
Speaker A
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
20:43
Speaker A
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
21:09
Speaker A
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
21:35
Speaker A
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
21:54
Speaker A
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
22:14
Speaker A
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
22:39
Speaker A
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
22:53
Speaker A
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
23:09
Speaker A
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
23:23
Speaker A
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
23:41
Speaker A
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
23:54
Speaker A
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
24:09
Speaker A
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
24:29
Speaker A
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
24:46
Speaker A
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
25:11
Speaker A
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
25:25
Speaker A
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
25:39
Speaker A
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
25:53
Speaker A
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
26:07
Speaker A
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
26:27
Speaker A
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
26:43
Speaker A
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
27:02
Speaker A
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
27:23
Speaker A
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
27:37
Speaker A
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
27:56
Speaker A
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
28:13
Speaker A
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
28:28
Speaker A
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
29:00
Speaker A
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
29:26
Speaker A
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
29:39
Speaker A
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
29:54
Speaker A
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
30:06
Speaker 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
30:19
Speaker A
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
30:37
Speaker A
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
30:59
Speaker A
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
31:24
Speaker A
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
31:43
Speaker A
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
32:04
Speaker A
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
32:22
Speaker A
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
32:49
Speaker A
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
33:08
Speaker A
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
33:28
Speaker A
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
33:43
Speaker A
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
34:01
Speaker A
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
34:13
Speaker A
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
34:38
Speaker A
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
35:06
Speaker A
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
35:25
Speaker A
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
35:43
Speaker A
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
35:59
Speaker A
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
36:20
Speaker A
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
36:37
Speaker A
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
37:02
Speaker A
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
37:21
Speaker A
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
37:47
Speaker A
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
38:09
Speaker A
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
38:22
Speaker A
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
38:41
Speaker A
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
38:58
Speaker A
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
39:18
Speaker A
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
39:36
Speaker A
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
39:55
Speaker A
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
40:11
Speaker A
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
40:21
Speaker A
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
40:35
Speaker A
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
40:53
Speaker A
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
41:07
Speaker A
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
41:24
Speaker 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
41:37
Speaker A
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
41:56
Speaker A
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
42:09
Speaker A
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
42:21
Speaker A
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
42:41
Speaker A
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
43:02
Speaker A
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
43:20
Speaker A
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
43:46
Speaker A
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
44:11
Speaker A
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
44:27
Speaker A
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
44:42
Speaker A
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
45:06
Speaker A
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
45:21
Speaker A
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
45:46
Speaker A
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
46:11
Speaker A
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
46:29
Speaker A
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
46:59
Speaker A
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
47:11
Speaker A
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
47:31
Speaker A
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
47:50
Speaker A
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
48:08
Speaker A
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
48:24
Speaker A
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
48:41
Speaker A
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
48:57
Speaker A
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
49:17
Speaker A
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
49:39
Speaker A
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
49:58
Speaker A
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
50:11
Speaker A
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
50:31
Speaker A
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
50:50
Speaker A
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
51:10
Speaker A
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
51:28
Speaker A
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
51:47
Speaker A
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
52:05
Speaker A
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
52:22
Speaker A
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
52:45
Speaker A
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
53:05
Speaker A
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
53:23
Speaker A
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
53:40
Speaker A
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
54:09
Speaker A
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
54:28
Speaker A
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
54:46
Speaker A
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
55:04
Speaker A
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
55:25
Speaker A
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
55:45
Speaker A
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
56:01
Speaker A
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
56:25
Speaker A
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
56:47
Speaker A
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
57:04
Speaker A
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
57:21
Speaker A
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
57:41
Speaker A
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
58:05
Speaker A
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
58:26
Speaker A
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
58:43
Speaker A
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
59:01
Speaker A
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
59:21
Speaker A
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
59:45
Speaker A
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
60:05
Speaker A
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
60:20
Speaker A
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
60:42
Speaker A
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
61:04
Speaker A
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
61:28
Speaker A
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
61:57
Speaker A
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
62:12
Speaker A
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
62:22
Speaker A
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
62:43
Speaker A
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
63:00
Speaker A
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
63:14
Speaker A
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
63:39
Speaker A
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
64:01
Speaker A
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
64:26
Speaker A
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
64:45
Speaker A
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
65:04
Speaker A
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
65:24
Speaker A
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
65:39
Speaker A
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
65:57
Speaker A
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
66:12
Speaker A
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
66:28
Speaker A
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
66:39
Speaker A
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
66:53
Speaker A
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
67:12
Speaker A
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
67:32
Speaker A
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
67:54
Speaker A
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
68:13
Speaker A
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
68:25
Speaker A
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
68:48
Speaker A
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
69:08
Speaker A
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
69:28
Speaker A
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
69:49
Speaker A
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
70:06
Speaker A
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
70:28
Speaker A
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
70:47
Speaker A
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
70:57
Speaker A
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
71:15
Speaker A
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
71:33
Speaker A
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
71:51
Speaker A
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
72:13
Speaker A
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
72:32
Speaker A
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
72:53
Speaker A
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
73:09
Speaker A
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
73:22
Speaker A
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
73:42
Speaker A
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
73:55
Speaker A
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
74:12
Speaker A
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
74:26
Speaker A
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
74:41
Speaker A
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
75:03
Speaker A
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
75:19
Speaker A
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
75:35
Speaker A
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
75:52
Speaker A
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
76:04
Speaker A
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
76:27
Speaker A
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
76:45
Speaker A
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
77:03
Speaker A
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
77:22
Speaker A
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
77:42
Speaker A
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
78:05
Speaker A
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
78:28
Speaker A
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
78:48
Speaker A
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
79:06
Speaker A
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
79:25
Speaker A
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
79:41
Speaker A
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
79:57
Speaker A
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
80:15
Speaker A
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
80:32
Speaker A
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
81:01
Speaker A
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
81:17
Speaker A
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
81:38
Speaker A
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
81:58
Speaker A
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
82:17
Speaker A
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
82:27
Speaker A
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
82:46
Speaker A
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
83:09
Speaker A
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
83:30
Speaker A
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
83:56
Speaker A
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
84:12
Speaker A
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
84:31
Speaker A
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
85:23
Speaker A
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
85:52
Speaker A
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
86:08
Speaker A
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
86:31
Speaker A
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
86:47
Speaker A
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
87:06
Speaker A
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
87:25
Speaker A
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
87:54
Speaker A
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
88:15
Speaker A
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
88:38
Speaker A
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
88:49
Speaker A
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
89:06
Speaker A
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
89:27
Speaker A
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
89:45
Speaker A
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
90:22
Speaker A
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
90:48
Speaker A
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
91:07
Speaker A
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
91:26
Speaker A
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
91:45
Speaker A
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
92:01
Speaker A
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
92:23
Speaker A
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
92:46
Speaker A
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
93:07
Speaker A
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
93:24
Speaker A
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
93:34
Speaker A
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
93:52
Speaker A
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
94:11
Speaker A
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
94:31
Speaker A
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
94:49
Speaker A
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
95:08
Speaker A
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
95:24
Speaker A
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
95:44
Speaker A
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
95:58
Speaker A
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
96:17
Speaker A
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
96:39
Speaker A
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
97:00
Speaker A
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
97:19
Speaker A
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
97:31
Speaker A
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
97:47
Speaker A
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
98:00
Speaker A
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
98:13
Speaker A
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
98:32
Speaker A
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
98:48
Speaker A
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
99:06
Speaker A
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
99:21
Speaker A
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
99:39
Speaker A
so then I'll stop here for today's session and we'll complete we'll continue in the next session
Topics:Java loopswhile loopdo-while loopfor loopenhanced for loopfor-each loopJava control statementsiteration in Javaloop initializationloop condition

Frequently Asked Questions

What are the different types of loops in Java?

Java has four types of loops: while loop, do-while loop, for loop, and enhanced for loop (also called for-each loop).

Why should I use loops instead of multiple print statements?

Loops help reduce code duplication by allowing you to write a single statement that repeats multiple times, making the code shorter and easier to maintain.

What are the three important components of a loop?

The three key components are initialization (starting point), condition (how many times to repeat), and incrementation or decrementation (how the loop variable changes each iteration).

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →