useful use of switch block

Hi,

recently I have browsed through wonderfl.net and found unknown to me but useful use-case of switch block. The switch is selection control block and usually is used as follows:

switch(index)
{
    case 0:
        //block of code (to the nearest break) executed when tested statement, here index variable has value of 0.
    break;
    case 1:
        //block of code (to the nearest break) executed when tested statement, here index variable has value of 1.
    break;
    case 2:
    case 3:
        //block of code (to the nearest break) executed when tested statement, here index variable has value of 2 or 3.
    break;
    default:
        //block of code executed when tested statement wasn't matched by any of <em>case </em>blocks.
    
}

But at the aforementioned website I have found code that used switch in different way:

switch (true) 
{
	case index == 0:
		//code executed when index == 0
	break;
	case index == 1:
		//code executed when index == 1
	break;
	case index == 2 || index == 3:
		//code executed when index == 2 or 3
	break;
	default:
		//when index wasn't matched by any of case blocks
}

this way gives more flexibility over “test cases”

best regards

This entry was posted in actionscript, flash and tagged , , , . Bookmark the permalink.