简体   繁体   中英

How to break if statement inside nested loop in java

Why my code don't stop when reach if statement, I want to break outside all loops once the break statement call, thanks before, sorry for my bad english

    for (int i = 0;i<nums.length;i++){
        for (int j = 0; j <nums.length;j++){
            if(i!=j){
                System.out.print(i+" "+j+" ");System.out.println("Value : "+nums[i]+" "+nums[j]);
                sum = nums[i]+nums[j];
                if(sum==target){
                    System.out.println("Result : "+nums[i]+" "+nums[j]);
                    finalArray[0] = i;
                    finalArray[1] = j;
                    break;
                }
            }
        }
    }
}

}

You can do this:

outer:
for (int i = 0;i<nums.length;i++){
    for (int j = 0; j <nums.length;j++){
        if(i!=j){
            System.out.print(i+" "+j+" ");System.out.println("Value : "+nums[i]+" "+nums[j]);
            sum = nums[i]+nums[j];
            if(sum==target){
                System.out.println("Result : "+nums[i]+" "+nums[j]);
                finalArray[0] = i;
                finalArray[1] = j;
                break outer;
            }
        }
    }
}

You can replace outer with whatever you want.

You can use labels to refer to loops like this:

loop1:        
for (int i = 0;i<nums.length;i++){
            for (int j = 0; j <nums.length;j++){
                if(i!=j){
                    System.out.print(i+" "+j+" ");System.out.println("Value : "+nums[i]+" "+nums[j]);
                    sum = nums[i]+nums[j];
                    if(sum==target){
                        System.out.println("Result : "+nums[i]+" "+nums[j]);
                        finalArray[0] = i;
                        finalArray[1] = j;
                        break loop1;
                    }
                }
            }
        }
    }

Try setting a flag in the inner loop when you want to break from the outer loop. once inner loop breaks check if that flag is set, if it is, break from outer loop as well. Something like this:

boolean break_outer = false
for (int i = 0;i<nums.length;i++){
        for (int j = 0; j <nums.length;j++){
            if(i!=j){
                System.out.print(i+" "+j+" ");System.out.println("Value : "+nums[i]+" "+nums[j]);
                sum = nums[i]+nums[j];
                if(sum==target){
                    System.out.println("Result : "+nums[i]+" "+nums[j]);
                    finalArray[0] = i;
                    finalArray[1] = j;
                    break_outer = true
                    break;
                }
            }
        }
        if (break_outer)
              break;
    }

You already have quite a bit of code there. It seems to be getting something very specific for you. You could make it a method and have it return the values at that point.

2cents

I am not confident I have enough information to write the code. However, I'd like to try. With any luck it will give you something you need:

private int[] convertToFinalArray(int[] nums, int target) {
    int sum = 0;

    int[] finalArray = new int[nums.length]; // I am inferring the length.
    for(int i = 0;i<nums.length; i++) {
        for (int j = 0; j < nums.length; j++) {
            if (i != j) {
                System.out.print(i + " " + j + " ");
                System.out.println("Value : " + nums[i] + " " + nums[j]);
                sum = nums[i] + nums[j];
                if (sum == target) {
                    System.out.println("Result : " + nums[i] + " " + nums[j]);
                    finalArray[0] = i;
                    finalArray[1] = j;
                    return finalArray;
                }
            }
        }
    }
    return finalArray;
}```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM