when are labels useful in java ??
Answers
Answered by
1
Here is one benefit of using labels in Java:
block: { // some code if(condition) break block; // rest of code that won't be executed if condition is true }
Another usage with nested-loops:
outterLoop: for(int i = 0; i < 10; i++) { while(condition) { // some code if(someConditon) break outterLoop; // break the for-loop if(anotherConditon) break; // break the while-loop // another code } // more code }
or:
outterLoop: for(int i = 0; i < 10; i++) { while(condition) { // some code if(someConditon) continue outterLoop; // go to the next iteration of the for-loop if(anotherConditon) continue; // go to the next iteration of the while-loop // another code } // more code }
block: { // some code if(condition) break block; // rest of code that won't be executed if condition is true }
Another usage with nested-loops:
outterLoop: for(int i = 0; i < 10; i++) { while(condition) { // some code if(someConditon) break outterLoop; // break the for-loop if(anotherConditon) break; // break the while-loop // another code } // more code }
or:
outterLoop: for(int i = 0; i < 10; i++) { while(condition) { // some code if(someConditon) continue outterLoop; // go to the next iteration of the for-loop if(anotherConditon) continue; // go to the next iteration of the while-loop // another code } // more code }
Sukanyayayayayayayay:
I'm in 9th standard
Similar questions