switch
statement is a control structure we did not cover last semester.
switch (expression) { case constvalue: expressions; [break;] case constvalue: expressions; [break;] default: expressions; }
case
statement, the expressions following that are executed.
case
statement, and default
is present, the statements following the default statement are executed.
break
is encountered, execution is transferred outside of the switch statement.
string SizeTToString(SizeT size){ string name{"unknown"}; switch(size) { case SizeT::TINY: name = "tiny"; break; case SizeT::SMALL: name = "small"; break; case SizeT::MEDIUM: name = "medium"; break; case SizeT::LARGE: name = "large"; break; case SizeT::HUGE: name = "huge"; break; case SizeT::GARGANTUAN: name = "gargantuan"; break; default: cout << "Error, unknown size" << endl; }
goto
continue
return
string HugeComment(SizeT size){ string response; switch(size) { case SizeT::TINY: case SizeT::SMALL: case SizeT::GARGANTUAN: response = "What, I don't see you!"; break; case SizeT::MEDIUM: response = "Look at that tiny little thing!"; break; case SizeT::LARGE: response = "Will you be my friend?"; break; case SizeT::HUGE: response = "Look at that big thing!"; break; default: response = "I don't know what size that thing is!"; } return response; }