Determining if something was processed in a switch/case construct

This tip submitted by Marty Corbett on 2012-01-31 17:34:13. It has been viewed 2703 times.
Rating of 4.3 with 3 votes



Instead of coding the following (the .. indicates the possible presence of other code):

int processed = false;
int variable=4;
switch(variable)
{
  case 1: .. processed=true; .. break;
  case 2: .. processed=true; .. break;
  case 3: .. processed=true; .. break;
  case 4: .. processed=true; .. break;
 ..
}
if(processed) { ... }

use the following:

int processed = true;
int variable=4;
switch(variable)
{
  default:  processed = false;  break;
  case 1: .. break;
  case 2: .. break;
  case 3: .. break;
  case 4: .. break;
 ..
}

if(processed) { ... }







More tips

Help your fellow programmers! Add a tip!