In Java, a transfer statement is a control flow statement that changes the execution sequence of a program. Java includes the following transfer statements:
Break: Exits a loop or a `switch` statement prematurely, transferring control to the statement immediately following the loop or `switch`
Continue: Skips the remainder of the current loop iteration and proceeds directly to the next iteration of the loop.
Return: Exits from a method and optionally returns a value to the method's caller.
Transfer Statements In Java
o Break statement
o Continue statement
o Labeled break and continue statements
o Do-while vs continue
Break statement:
We can use break statement in the following cases.
Inside switch to stop fall-through.
Inside loops to break the loop based on some condition.
Inside label blocks to break block execution based on some condition.
How break statement works?
Java break and Nested Loop
In the case of nested loops, the break statement terminates the innermost loop.
Labeled break Statement
We have used the unlabeled break statement. It terminates the innermost loop and switch statement. However, there is another form of break statement in Java known as the labeled break.
Break Statement Examples:
Inside switch :
We can use break statement inside switch to stop fall-through
Example 1:
class Test{
public static void main(String args[]){
int x=0;
switch(x)
{
case 0:
System.out.println("hello");
break ;
case 1:
System.out.println("hi");
}
Output:
D:\Java>javac Test.java
D:\Java>java Test
Hello
Inside loops :
We can use break statement inside loops to break the loop based on some condition.
Example 2:
class Test{
public static void main(String args[]){
for(int i=0; i<10; i++) {
if(i==5)
break;
System.out.println(i);
}
}}
Output:
D:\Java>javac Test.java
D:\Java>java Test
0
1
2
3
4
Inside Labeled block :
We can use break statement inside label blocks to break block execution based on some
condition.
Example:
class Test{
public static void main(String args[]){
int x=10;
l1 : {
System.out.println("begin");
if(x==10)
break l1;
System.out.println("end");
}
System.out.println("hello");
}
}
Output:
D:\Java>javac Test.java
D:\Java>java Test
begin
Hello
These are the only places where we can use break statement. If we are using anywhere
else we will get compile time error.
Example:
class Test{
public static void main(String args[]){
int x=10;
if(x==10)
break;
System.out.println("hello");
}
}
Output:
Compile time error.
D:\Java>javac Test.java
Test.java:5: break outside switch or loop
break;
Break statement summary: The `break` statement is an essential loop control mechanism used to terminate the execution of a loop prematurely. When a `break` statement is encountered within a loop, the iterations of the loop cease immediately. This means that the program exits the loop entirely, and control is transferred to the first statement that follows the loop. By using the `break` statement, you can interrupt the normal flow of the loop based on a specified condition or event, allowing the program to proceed with subsequent code outside the loop. This functionality is particularly useful when you need to exit a loop early, for example, when a certain condition is met or when an error occurs, ensuring that unnecessary iterations are avoided and the program continues with its next set of instructions efficiently.
Continue statement:
We can use continue statement to skip current iteration and continue for the next
Iteration.
Working of Java continue statement
Java continue with Nested Loop
Labeled continue Statement
We have used the unlabeled continue statement. However, there is another form of continue statement in Java known as labeled continue.
Continue statement Examples
Example:
Output:
D:\Java>javac Test.java
D:\Java>java Test
1
3
5
7
9
We can use continue only inside loops if we are using anywhere else we will get compile
time error saying "continue outside of loop".
Example:
class Test
{
public static void main(String args[]){
int x=10;
if(x==10);
continue;
System.out.println("hello");
}
}
Output:
Compile time error.
D:\Enum>javac Test.java
Test.java:6: continue outside of loop
continue;
Labeled break and continue statements:
In the nested loops to break (or) continue a particular loop we should go for labeled
break and continue statements.
class Test
{
public static void main(String args[]){
l1:
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
break;
System.out.println(i+"........."+j);
}
}
}
}
Break:
1.........0
2.........0
2.........1
Break l1:
No output.
Continue:
0.........1
0.........2
1.........0
1.........2
2.........0
2.........1
Continue l1:
1.........0
2.........0
2.........1
Do-while vs continue:
Output:
1
4
6
8
10
Compiler won't check unreachability in the case of if-else it will check only in loops.
Example 1:
class Test
{
public static void main(String args[]){
while(true)
{
System.out.println("hello");
}
System.out.println("hi");
}
}
Output:
Compile time error.
D:\Enum>javac Test.java
Test.java:8: unreachable statement
System.out.println("hi");
Example 2:
class Test
{
public static void main(String args[]){
if(true)
{
System.out.println("hello");
}
else
{
System.out.println("hi");
}}}
Output:
Hello
Continue statement summary:
The `continue` statement in Java is a control mechanism used within loops (`for`, `while`, `do-while`) to skip the current iteration and immediately move to the next one. When the `continue` statement is executed, the remaining code in the current loop iteration is ignored, and the loop proceeds to the next iteration. In a `for` loop, control jumps to the update step; in `while` and `do-while` loops, control goes back to the loop's condition check. This statement is especially useful for bypassing specific iterations based on certain conditions, allowing for more precise control over the loop's execution.
コメント