-2

How to use goto statement in java..

package test;

import java.util.Scanner;

public class PrimeComposite {

    public static void main(String[] args) {

        int n,r;

        Scanner input = new Scanner(System.in);

        System.out.print("Ener a number: ");
        n= input.nextInt();

        for (int i=2; i<=n; i++){
            r= n%i;
            if (r==0){
                System.out.print("The number is composite");
                goto a: //want to jump from this loop to the a: label.

            }

            System.out.print("The number is prime");

        }
        a: //want to jump here if the if condition gets true... 

    }
}
Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
San
  • 1
  • 1
  • 4
    Please, don't, seriously, just don't. Use `break;` instead or actually devise an exit condition for the loop – MadProgrammer Feb 01 '16 at 03:46
  • 1
    Java has `goto` as a reserved word. It is **not** an *implemented* statement. In your loop, just `break`. – Elliott Frisch Feb 01 '16 at 03:46
  • 1
    @San like @Elliott-frisch said no `goto` in java see [http://stackoverflow.com/questions/2545103/is-there-a-goto-statement-in-java](http://stackoverflow.com/questions/2545103/is-there-a-goto-statement-in-java) - just use `break` for your particular use case and possibly read how you can use `break` and `continue` with `labels` for future use cases that mimic `goto`. – non sequitor Feb 01 '16 at 04:00
  • As I figure this is a homework assignment: even for this simple way of telling if a number is prime, your upper loop limit is wrong. Besides using `break` instead of `goto`, re-think your algorithm. – Andrew Lazarus Feb 01 '16 at 04:11

1 Answers1

0

There is no goto statement in Java. Only a reserved word.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    There is, in fact. It is reserved and unimplemented. But it is part of the language :-P #nitpick – Tony Ennis Feb 01 '16 at 04:17
  • @TonyEnnis There isn't, in fact. There is a *reserved word.* No statement. No grammar. Nothing. – user207421 Feb 01 '16 at 04:18
  • good to know. Help me understand what 'goto' means in this context, please: https://en.wikipedia.org/wiki/List_of_Java_keywords – Tony Ennis Feb 01 '16 at 12:15
  • @TonyEnnis It means exactly what I said. It is a reserved word. NB the normative reference for Java isn't Wikipedia, it is the Java Language Specification. – user207421 Feb 01 '16 at 21:56
  • I don't know what the standard is, but this Oracle page says goto is a reserved keyword and cannot be used as a variable. I am not very subtle person in general; what do I not understand? https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9 – Tony Ennis Feb 01 '16 at 23:28
  • @TonyEnnis You can keep waving lists of reserved words at me as much as you like, but a list of reserved words is not a list of statements. I said a day ago that `goto` is a reserved word, not a statement. What part of that don't you understand? Java statements are defined in [JLS #14.5&ff](https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.5). And please note that the *question* is *about* the '`goto` *statement*', not the '`goto` reserved word'. – user207421 Feb 02 '16 at 08:33
  • @downvoter(s) I can only assume you are suffering from the same confusion as this person. – user207421 Feb 03 '16 at 21:25