.TH switch "" "" "C Keyword"
.PC "Test a variable against a table"
.PP
.B switch
is a C keyword that lets you perform a number of tests on a variable
in a convenient manner.
For example,
.DM
while(foo < 10)
	switch(foo) {
	case 1:
		dosomething();
		break;
	case 2:
		somethingelse();
	case 3:
		anotherthing();
		break;
	default:
		break;	
	}
}
.DE
.PP
is equivalent to
.DM
while(foo < 10) {
	if(foo == 1) {
		dosomething();
		continue;
	} else if (foo == 2) {
		somethingelse();
		anotherthing();
		continue;
	} else if(foo == 3) {
	 /* Note: compiler eliminates duplicate code */
		anotherthing();
		continue;
	} else
		break;
}
.DE
.PP
.B switch
is always used with the
.B case
statement, and nearly always with the
.B default
statement.
.SH "See Also"
.Xr "break," break.k
.Xr "C keywords," c_keyword
.Xr "case," case.k
.Xr "default," default
.Xr "while" while.k
.br
\*(AS, \(sc6.6.4.2
