Skip to content

Latest commit

 

History

History
59 lines (51 loc) · 1.32 KB

5.13.md

File metadata and controls

59 lines (51 loc) · 1.32 KB

(a)

unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch) {
  case ’a’: aCnt++; break;  // Need `break`
  case ’e’: eCnt++; break;  // Need `break`
  default: iouCnt++;
}

(b)

unsigned index = some_value();
int ix;
switch (index) {
  case 1:
    //int ix = get_value();  // Cannot bypass initialization
    ix = get_value();
    ivec[ ix ] = index;
    break;
  default:
    ix = ivec.size()-1;
    ivec[ ix ] = index;
}

(c)

unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch (digit) {
  //case 1, 3, 5, 7, 9:  // case label must be integral const expression
  case 1: case 3: case 5: case 7: case 9:
    oddcnt++;
    break;
  //case 2, 4, 6, 8, 10:
  case 2: case 4: case 6: case 8: case 10:
    evencnt++;
    break;
}

(d)

//unsigned ival=512, jval=1024, kval=4096;
// case label must be integral const expression
constexpr unsigned ival=512, jval=1024, kval=4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt) {
  case ival:
    bufsize = ival * sizeof(int);
    break;
  case jval:
    bufsize = jval * sizeof(int);
    break;
  case kval:
    bufsize = kval * sizeof(int);
    break;
}