Use of “switch” as a conditional<\/p>\n
A switch<\/strong> statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.<\/p>\n The syntax for a switch<\/strong> statement in C++ is as follows:<\/p>\n The following rules apply to a switch statement:<\/p>\n The expression<\/strong> used in a switch<\/strong> statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.<\/p>\n<\/li>\n You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.<\/p>\n<\/li>\n The constant-expression<\/strong> for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.<\/p>\n<\/li>\n When the variable being switched on is equal to a case, the statements following that case will execute until a break<\/strong> statement is reached.<\/p>\n<\/li>\n When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.<\/p>\n<\/li>\n Not every case needs to contain a break. If no break appears, the flow of control will fall through<\/em>to subsequent cases until a break is reached.<\/p>\n<\/li>\n A switch<\/strong> statement can have an optional default<\/strong> case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.<\/p>\n<\/li>\n<\/ul>\n #TC1017<\/a> #Mastery17<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" Use of “switch” as a conditionalA switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntax:The syntax for a… Continue reading Syntax:<\/h2>\n
switch<\/span>(<\/span>expression<\/span>){<\/span>\n case<\/span> constant<\/span>-<\/span>expression <\/span>:<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n break<\/span>;<\/span> \/\/optional<\/span>\n case<\/span> constant<\/span>-<\/span>expression <\/span>:<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n break<\/span>;<\/span> \/\/optional<\/span>\n \n \/\/ you can have any number of case statements.<\/span>\n default<\/span> :<\/span> \/\/Optional<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n}
<\/span><\/pre>\n\n