Goto Statement In Java
Goto Statement
In this article we will be discussing about Goto Statement in Java.
A goto statement in any programming language provides an unconditional jump from the goto to a labeled statement in the same function.
Goto Statement
Goto Statement in Java isn’t supported by Java. It is reserved as a keyword in the Java Virtual Machine,In case if they want to add it in a later version.
Instead of Goto function, Java uses Label. Labels are used to change the flow of the program and jump to a specific instruction or label based on a condition.
Use of Break And Continue in Label:
Break and continue are used to break or to continue the program when the label function is called. These are the two reserved keywords in Java Virtual Machine.
Use case of break statement with label:
import java.util.*; public class Main{ public static void main(String[] args){ // Label name for the break statement Lakshit: for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { if (j == 2) // break statement for the label break Lakshit; System.out.println("PrepInsta " + j); } } } }
Output: PrepInsta 1
Use case of Continue statement with label:
import java.util.*; public class Main { public static void main(String[] args){ // Label name for the continue statement Lakshit: for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { if (j == 2) // Continue Statement continue Lakshit; System.out.println("PrepInsta " + j); } } } }
Output: PrepInsta 1 PrepInsta 1 PrepInsta 1 PrepInsta 1 PrepInsta 1 PrepInsta 1
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
sony earphones
PrepInsta – Server busy!
Hey there,
Kindly Whatsapp us at 8448440710 Whatsapp number, our team will guide you further precisely.