|
|
1.1 ! root 1: ! 2: ! 3: switch C Keyword switch ! 4: ! 5: ! 6: ! 7: ! 8: Test a variable against a table ! 9: ! 10: ! 11: switch is a C keyword that lets you perform a number of tests on ! 12: a variable in a convenient manner. For example, ! 13: ! 14: ! 15: while(foo < 10) ! 16: switch(foo) { ! 17: case 1: ! 18: dosomething(); ! 19: break; ! 20: case 2: ! 21: somethingelse(); ! 22: break; ! 23: case 3: ! 24: anotherthing(); ! 25: break; ! 26: default: ! 27: break; ! 28: } ! 29: } ! 30: ! 31: ! 32: is equivalent to ! 33: ! 34: ! 35: while(foo < 10) { ! 36: if(foo == 1) { ! 37: dosomething(); ! 38: continue; ! 39: } else if(foo == 2) { ! 40: somethingelse(); ! 41: anotherthing(); ! 42: continue; ! 43: } else if(foo == 3) { ! 44: /* Note: compiler eliminates duplicate code */ ! 45: anotherthing(); ! 46: continue; ! 47: } else ! 48: break; ! 49: } ! 50: ! 51: ! 52: switch is always used with the case statement, and nearly always ! 53: with the default statement. ! 54: ! 55: ***** See Also ***** ! 56: ! 57: break, C keywords, case, default, keyword, while ! 58: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.