-2
public class Array {
    public static void main(String[] args) {        
        int[] arr= {5,6,9,-5,-2,4,-3,1,-1};
        
        int i=0;
        int n=arr.length;
        int j=n-1;
        while(true) 
            while(arr[i]>0 && i<j) {
                while(arr[j]<0 && i<j) {
                    if(i<j) {
                        int temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                        i++;
                        j--;
                    }else 
                        break;
                }
                
            }
        for(int x=0;x<arr.length;x++) {
            System.out.print(arr[x]+" "); //unreachable code
        }
    }
}

Why this code is showing "unreachable code" error? And How can I fix it? how can I solve this error please help?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    You have an infinite loop `while(true)` with no way out of it; how do you expect the `for` loop below should be reached? If you intend the `break` statement to exit from the outermost loop, you need to use a labelled break. – kaya3 Jan 21 '21 at 09:30
  • 3
    As a side note, I'd *strongly* recommend using braces even for single-statement loop/if bodies. – Jon Skeet Jan 21 '21 at 09:35

2 Answers2

1

It is all because of this line of code:

    while(true) 

The rest of the code on the for loop will become unreachable because there is no way the first while loop will terminate thus causing your program to be running on an infinite loop

Unreachable code:

for(int x=0;x<arr.length;x++) {
    System.out.print(arr[x]+" "); //unreachable code
}

To fix you need to set a variable for the first while condition that would somehow change a value once a condition is met. Based on your code you can place it on the else part of your code:

boolean run = true;
while(run) {
        while(arr[i]>0 && i<j) {
            while(arr[j]<0 && i<j) {
                if(i<j) {
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                    i++;
                    j--;
                } else {
                     run = false; 
                     break;
                 }
            }
            
        }
}
Donato Amasa
  • 846
  • 2
  • 6
  • 22
  • Note that the intention of the `break` is to leave all loops immediately; what you propose will still continue going through `while(arr[j]<0 && i0 && i – daniu Jan 21 '21 at 09:52
0

break leaves the nearest innermost loop it is in, in your case while(arr[j]<0 && i<j). That means your while(true) is an infinite loop.

You can fix this by either maintaining a flag that will trigger an outer break:

boolean goOn = true;
while(goOn) 
    while(goOn && arr[i]>0 && i<j) {
        while(goOn && arr[j]<0 && i<j) {
            if(i<j) {
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i++;
                j--;
            }else  {
                goOn = false;
                break;
            }
        }
        
    }
for(int x=0;x<arr.length;x++) {
    System.out.print(arr[x]+" "); //unreachable code
}

or create a label and break there:

while(true) 
    while(arr[i]>0 && i<j) {
        while(arr[j]<0 && i<j) {
            if(i<j) {
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i++;
                j--;
            }else  {
                break exit;
            }
        }
        
    }
exit:
for(int x=0;x<arr.length;x++) {
    System.out.print(arr[x]+" "); //unreachable code
}

The latter is a feature that is rarely used in Java.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • @Saksham You need to move the `if(i < j)` check further outside. Since the same check is in the `while`-conditions, it will never reach the point it's currently at. Best to use a debugger to see what values `i` and `j` have in each iteration. – daniu Jan 21 '21 at 10:26