Java Essentials - Break statement in java
2K views
Nov 18, 2024
Java Essentials - Break statement in java
View Video Transcript
0:00
In this video, we are discussing break statement in Java
0:04
This break statement can be used in our switch case construct and also in our looping
0:11
When the break statement is encountered inside a loop, the loop is immediately terminated
0:17
and the program control resumes at the next instruction following the loop
0:23
That means, whenever the break statement will be encountered within the loop, then immediately the control will
0:29
come out from the loop construct or the loop body and the next statement following the loop will have
0:35
its control and can get its execution it can be used to terminate a case in the switch statement also
0:44
so here we're having this conditional code is there and then we're having one condition if the
0:49
condition is true then conditional code is going to get executed once again and if the condition
0:55
is false it is coming out from this looping construct and the next
0:59
line will get the control. But within this conditional code, if the break statement is getting
1:05
executed has got its execution, then immediately it is coming out from the loop and the next
1:11
code are going to get executed, which will be following this loop construct. So let us go for
1:18
one practical demonstration for the easy understanding of usage of break statement in our program body How to use break statement in Java We shall discuss that in this In thisavier we shall discuss that in this session so let us suppose we have defined one variable a initialized with the value zero of the
1:36
type integer while true that means we are we are going to execute one
1:40
infinite loop because the true means always the condition will remain true so while
1:45
true will form an infinite loop go on executing within this loop body we are just
1:49
printing the current value of a and then we are checking if the value of a is
1:53
is equal to is equal to 10 then break break means it will come out from the loop body
1:59
that means within this calip recess we're having one loop body so this is the loop body we're
2:04
having so if a is equal to is equal to 10 then it will come out from this loop body
2:09
that means the next line whatever there after this while will get executed so let us put
2:14
another line here for the better understanding Okay, end of the program is the line
2:25
So when the value of A will be equal to 10, after printing this A, when the value of A will be equal to 10, then it will break, it will come out from the loop body and the remaining part of the program will get executed
2:36
Otherwise, it will just increase the value of A by 1. So let me go for the execution. You can find that a, the loop is getting executed for A is equal to 0 to 10
2:46
Then it is coming out from the loop body and end of the program, that message, that string is getting printed
2:52
So you are getting the idea. purpose of break statement in our Java code. Thanks for watching this video
#Computer Education
#Education
#Java (Programming Language)