Tuesday, 4 April 2017

Is there "goto" in Java?

Yes and No, I think both will be correct But the absolute answer is "Yes, there is goto in Java".(Not for usage but it is present and reserved). Now you might be wondering, How?

Let's understand this with an Example:

import java.io.*;
import java.util.*;
import java.lang.*;

class Test6{

public static void main(String[] args) throws IOException {

starting_point:

for (int i=1;i<10 ;i++ ) {

System.out.println("Now the Value of Number "+i);

if (i=9) {
goto starting_point;

}

}

}


Explanation: 

Now on Compiling this errors occurs,

and that is Because the goto is reserved keyword. True the goto is there  but it is not available to be used in the Program. The reason is that if in case it were to be added to a later version of Java and if the Programmer had used that word  in his Program, then the Program would break. So the Program will not even execute.

There is another option instead of using goto, you can use labels(with break).

No comments:

Post a Comment