Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get unreachable statement error in Java?

I'm putting together a code for a hailstone sequence I found in an online tutorial, but in doing so I ran into an unreachable statement error. I don't know if my code is correct and I don't want advice in correcting it if I'm wrong(regarding the hailstone sequence, I want to do that myself..:) ). I just want help in resolving the "unreachable statement" error at line 19.

class HailstoneSequence {
    public static void main(String[] args) {
        int[][] a = new int[10][];
        a[0][0] = 125;
        int number = 125;

        for (int i = 0;; i++) {
            for (int j = 1; j < 10; j++) {
                if (number % 2 == 0) {
                    a[i][j] = number / 2;
                    number = number / 2;
                } else {
                    a[i][j] = (number * 3) + 1;
                    number = (number * 3) + 1;
                }
            }
        }

        for (int i = 0;; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.println(a[i][j]);
            }
        }
    }
}
like image 747
user3889963 Avatar asked Feb 19 '26 14:02

user3889963


1 Answers

This is an infinite loop:

for(int i=0;;i++){

Whatever comes after it never get executed (i.e. is unreachable).

like image 109
NPE Avatar answered Feb 21 '26 04:02

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!