2.4.8 switch


Contents

Name

switch

Description

The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.

Remark

Examples

EX1

switch over integer
void switch_num_ex()
{	
	int nn = InputBox(1, "Integer(e.g., 2):");
	switch(nn)
	{
	case 1:
		out_str("one");
		break;
	case 2:
		out_str("two");
		break;
	case 3:
		out_str("three");
		break;
	default:
		out_str("no match");
		break;
	}
}

EX2

switch over string
void switch_string_ex()
{	
	switch(string strInput = InputBox("Month(e.g., May):"))
	{
	case "March":
		out_int(strInput, 3);
		break;
	case "April":
		out_int(strInput, 4);
		break;
	case "May":
		out_int(strInput, 5);
		break;
	case "June":
		out_int(strInput, 6);
		break;
	case "July":
		out_int(strInput, 7);
		break;
	default:
		out_str("no match");
		break;
	}
}

Header to Include

See Also

Reference