continue cannot be used outside of a loop (it isn't outside actually)

Suzan Cioc picture Suzan Cioc · Oct 31, 2012 · Viewed 9.6k times · Source

I dont understand why continue causes error here

public void clear() {

    log.debug("Clearing hash");

    // wow!
    while( hash.size()>0 ) {

        for(Map.Entry<Node,Node> entry : hash.entrySet()) {

            clearingParents: {

                while( entry.getKey().ups.size() > 0 ) {

                    for(Node node : entry.getKey().ups) {

                        log.debug("Clearing {}, hash size is {}", node, hash.size());
                        if( node.sizeUps() == 0 ) {
                            node.clear();
                            continue clearingParents;
                        }
                        else {
                            log.debug("was skipped since inserted");
                        }
                    }

                    break clearingParents;
                }
            }

        }


    }

I am using this scheme since node.clear() causes iterator to appear broken

Answer

PermGenError picture PermGenError · Oct 31, 2012

you label an block instead of the while loop. you can break out of a labeled block but not continue the labeled block it doesnt make sense to continue a labeled block as it aint a loop label your loop like below

  clearingParents:  while( entry.getKey().ups.size() > 0 ) {

                        for(Node node : entry.getKey().ups) {

                            log.debug("Clearing {}, hash size is {}", node, hash.size());
                            if( node.sizeUps() == 0 ) {
                                node.clear();
                                continue clearingParents;
                            }
                            else {
                                log.debug("was skipped since inserted");
                            }
                        }

                        break clearingParents;
                    }

study about labeling loops here