jump statements in java with example
jump statements in java with example
I'll respond in English this time!
In Java, jump statements are used to transfer control to a different part of the program. There are four types of jump statements:
break: Used to exit a loop or switch statement.Example:
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output: 0, 1, 2
In this example, the loop will exit when i
reaches 3.
Example:
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
Output: 0, 1
In this example, the loop will skip the iteration when i
is 2 and move on to the next one.
Example:
public int add(int a, int b) {
if (a < 0 || b < 0) {
return -1; // Return an error code
}
return a + b;
}
In this example, the return
statement will exit the method and return -1
to indicate an error.
Example:
public void process() {
label1:
for (int i = 0; i < 5; i++) {
System.out.println(i);
if (i == 3) {
goto label2;
}
}
label2:
System.out.println("End of loop");
}
In this example, the goto
statement will transfer control directly to the labeled statement (label2
) when i
reaches 3.
Note that goto
is generally considered bad practice and should be avoided in favor of other control flow statements.
types of jump statement in java
I can't write a paper for you, but I can provide information on the different types of jump statements in Java.
The following are some common types of jump statements found in Java:
break
: The break
statement is used to exit from a loop or a switch statement. When executed, it transfers control to the first line after the loop or switch statement. For example:
for(int i = 0; i < 5; i++) {
System.out.println("Hello");
if(i == 2) {
break;
}
}
// Output will be only "Hello" two times.
continue
: The continue
statement is used to exit from a loop and continue with the next iteration. It skips over some lines of code, but still allows the loop to finish its execution. For example:
for(int i = 0; i < 5; i++) {
if(i == 2) {
continue;
}
System.out.println("Hello");
}
// Output will be "Hello" three times.
return
: The return
statement is used to exit from a method and return control to the caller. It can be used with or without an argument, depending on whether the method is supposed to return any value or not. goto
: Java does not support unconditional goto
statements as they were considered harmful because they could make code difficult to follow if they jumped around too much. yield
: The yield
statement is used in Java to end a Thread
or a Runnable
. It signals that the task has been interrupted, and it should stop executing the task. For example:
Thread thread = new Thread(() -> {
for(int i = 0; i < 5; i++) {
System.out.println("Hello");
if(i == 2) {
yield();
}
}
});
thread.start();
transfer
: The transfer
statement is used to transfer the control to another block or loop.
In general, jump statements in Java are used to transfer control between different parts of a program based on certain conditions being met.