Java Essentials - For loop in java
Nov 18, 2024
Java Essentials - For loop in java
View Video Transcript
0:00
in this video we are going to discuss
0:02
another looping construct in Java that
0:04
is a for loop in Java we know that this
0:07
for loop can be applied for those cases
0:09
for those et additions where for number
0:12
of times the iteration is going to take
0:14
place is pre known to us so for loop is
0:17
mainly used whether the number of
0:20
iterations are pre known to us so when
0:22
this number of iterations are pre known
0:24
to us then only we can go for the for
0:27
loop execution the basic syntax is like
0:31
this so far then we are having this
0:34
initialization code boolean expression
0:37
which can return either true or false
0:39
so they will be separated by semicolon
0:42
then semicolon then a bit of the loop
0:45
control variable and then within curly
0:47
braces will be writing the statements
0:49
which are under the for loop and
0:51
supposed to get executed when the
0:53
condition is true so in the
0:55
initialization phase it initializes the
0:58
loop variable so let us suppose the I is
1:00
the loop variable so here I can write I
1:03
is equal to 1 or I is equal to 10 so
1:06
that is the loop initialization phase in
1:09
the boolean expression section it checks
1:12
whether the given condition is true or
1:14
false
1:15
finally it updates the loop control
1:18
variable here in this particular section
1:20
and these three statements are to be
1:23
separated by two semicolons
1:27
so each different section for the for
1:30
loop like initialization condition
1:33
checking and update are separated by a
1:36
semicolon so we can use this for loop
1:39
for the infinite looping using this
1:41
construct here here you can find that
1:43
only we have mentioned two semicolons
1:46
there is no initialization code there is
1:49
no condition and there is no update
1:51
score there so that is why we have put
1:53
this within this first brackets where
1:56
put this semicolon twice and this is
1:58
forming an infinite loop
2:01
so through this flowchart let us explain
2:04
more so control is coming from here this
2:08
is our init code or initialization code
2:10
then condition is there this butt
2:13
the condition when it is true then the
2:15
code block will get executed consisting
2:17
of single of more multiple statements
2:20
then the increment or decrement may take
2:22
place so a bit of the loop control
2:25
variable will take place but here you
2:26
have mentioned increment so also I'm
2:29
mentioning increment here then control
2:31
is coming back to the condition and when
2:34
this condition is true again this
2:36
looping will take less executing this
2:39
code block once again but if the
2:41
condition is false it will come out and
2:43
the program will get terminated after
2:45
executing the next unexecuted
2:47
instructions so let us go for one
2:50
practical demonstration for the better
2:52
understanding on this for loop in our
2:55
java program in this demonstration you
2:58
are discussing about the for loop in
3:00
Java so int sum is equal to 0 so the sum
3:04
has got initialized with a value 0 of
3:06
the type integer
3:07
and then for int a is equal to 0
3:10
semicolon I less than 10 semicolon a
3:14
plus plus that means here we were
3:16
defining one variable and easily
3:18
initializing it with 0 so there is a
3:20
loop control variable here the a is
3:22
known as the loop control variable so at
3:25
first the initialization then semicolon
3:27
then you will be having one condition
3:29
depending upon the condition it will be
3:31
decided whether the loop will get
3:32
executed further or it will get
3:34
terminated and then semicolon and then
3:37
we'll be having increment or decrement
3:39
factor or this a of this particular loop
3:42
control variable so at first
3:44
initialization then semicolon then
3:47
condition then semicolon then increment
3:49
or decrement of the loop control
3:51
variable value so this is a basic syntax
3:54
the basic grammar of this 4 so now when
3:58
this condition will be true when this
3:59
condition will be true then this block
4:01
will get executed what we are doing here
4:03
actually we are printing one line and
4:05
then we are going for sum is equal to
4:07
sum plus a and outside of the loop sum
4:10
has got initialized with the value 0 so
4:12
it can have maximum value as it is
4:14
getting increased by one starting from 0
4:16
so a can have maximum value that is 9
4:19
but at 9 less than 10 the condition will
4:22
be true so this sum can't add with the
4:24
values ranging from 0 to
4:26
9 because a can range from zero tonight
4:28
because after nine when the value of a
4:31
will get incremented by one when the
4:33
value of a gay will get incremented by
4:35
one so it will become 10 so 10 less than
4:38
10 the condition will become false it
4:40
will come out from the loop for loop and
4:42
this line will get printed so let us go
4:44
for the execution for the better
4:45
crediting you can find that the value of
4:48
a is ranging from 0 to 9 and these
4:51
values are getting added with the sum
4:53
and at the end after getting out from
4:55
the loop we're printing that the value
4:57
of sum is 45 that means when the value
5:00
of a will be equal to 10 then it is
5:02
coming out from the loop so that's why
5:05
if we print the value of a outside here
5:09
if you print the value of a outside of
5:11
this loop I am expecting that it will
5:14
print the value a as 10 here so let us
5:17
go for the execution one error is there
5:20
so I am just going for this sprinting
5:25
is the loop control variable so that's
5:27
why it is not having the scope outside
5:29
so it is better to go forward here int a
5:33
okay so I'm defining this a if we write
5:38
this int a here this that means this
5:40
scope will be confined within this for
5:42
loop only so not accessible from the
5:44
outside but if I define this vegetable a
5:46
outside of this loop then obviously it
5:49
is accessible after the for loop also so
5:51
now let me go for the execution you can
5:53
find that a is equal to 10 you are
5:55
finding the value of a is becoming 10 so
5:58
10 less than 10 the condition is
5:59
becoming false so that's why it is
6:01
coming out from the loop now see instead
6:05
of having this we are not erasing the
6:06
semicolon instead of having this also
6:08
you can write directly here okay so now
6:13
let me go for the execution you can find
6:15
we are going to have the same output
6:17
instead of right writing this a plus
6:19
plus instead of writing this a plus plus
6:22
here I can also put this a plus plus
6:24
here I can also put this a plus plus
6:27
here you can find that we're having the
6:30
same output whatever you are getting but
6:32
don't omit the semicolon so semicolons
6:34
should be there but you can have this
6:36
initialization statement and this
6:39
increment or decrement statement in
6:41
other part if I write a plus plus here
6:43
so let me go for another experiment if I
6:46
write a plus plus so you see for each
6:48
and every iteration a is getting
6:50
incremented by one here and then one
6:52
here so a is getting incremented by two
6:54
for each and every tradition so if I go
6:57
for the execution you can find that the
6:59
value of a will get incremented by two
7:01
always so after coming out from eight it
7:04
is becoming 10 so when it is becoming 10
7:06
it is coming out from the loop and now
7:08
we are getting this 10 as a final value
7:11
for a after coming out from the loop so
7:13
0 plus 2 is 2 plus 4 is 6 plus 6 is 12
7:17
plus 8 so I'm getting here 20 as a sum
7:20
so I think now the conception is getting
7:23
clear to you that how to use a for loop
7:26
in our Java code and whenever we require
7:29
some iteration with equal some looping
7:32
if we get some repetitive work you can
7:35
use for we can use do-while or you can
7:37
use while do
7:39
for watching this video
#Java (Programming Language)