java - Why should i give a name to an If Statement? -


i discovered can give name , while statements. understand useful if want break or continue specific loop.
why should give name if?? looks useless

name: if(true){     //do } 

this compiles without problems

if have code block name, can use break exit it, use have found naming blocks.

 name: if(true) {     //     if(/* condition */) {         break name;     }     // more } 

it works without if:

 name: {     //     if(/* condition */) {         break name;     }     // more } 

this comes in work when there set of guard conditions on logic, , set of logic fallen down regardless of outcome of guards.

a further example alternative structures more difficult read , modify:

 block: if(/* guard */) {    // prep work    if(/* guard */) {     break block;   }    // prep work    if(/* guard */) {     break block;   }    // real work } 

though use bare block , not if.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -