java - Break in nested for loops -
possible duplicate:
how break main/outer loop in double/nested loop?
i have following situation:
for(int = 0; < schiffe.length-1; i++){ if(schiffe[i].schaden){ schlepper.fliege(schiffe[i].x, schiffe[i].y, schiffe[i].z); schlepper.wirdabgeschleppt = schiffe[i]; for(int k = 0; k < stationen.length-1; k++){ if(stationen[k].reparatur == null){ schlepper.fliege(stationen[k].x, stationen[k].y, stationen[k].z); break; } } } }
i want
schlepper.fliege(stationen[k].x, stationen[k].y, stationen[k].z);
be performed once , break out of inner loop , continue for(int i... loop. used break in code. not sure if right. break cause break loops or second loop?
it breaks inner loop, , therefore want. break more 1 level, in java, can use "labelled break" so:
schiffe_loop: for(int = 0; < schiffe.length-1; i++){ some_stuff(); for(int k = 0; k < stationen.length-1; k++){ if (something_really_bad_happened()) { break schiffe_loop; } } }
but not need this.
you may consider making separate method inner loop anyway; it's can give name ("find_available_station" or that), , need somewhere else.
also, please aware loops right miss last element of each array. think why using < instead of <=; it's use every value 0 but not including specified one.
Comments
Post a Comment