CS 121 Lecture Notes - Lecture 34: Switch Statement

28 views4 pages

Document Summary

The switch statement: provides another way to make a selection from. First evaluates an expression, then based on the result, select the statements from several possible cases. case value1: case value2: case value3: switch (expression) case statement-list1 statement-list2 statement-list3: if expression matches value2, control jumps to there. Most situations, we want to execute only the first matched case. This is useful when all cases do not match, you want some by-default statements executed. So, the default case is placed at the end of the switch statement, & control will transfer to it if no other case value matches. The type of a switch expression must be integers, characters or enumerated types (java 7, also accepts strings). Relation between if-else & switch: case b"; case a"; switch (option) B = 66; case c"; acount++; break; bcount++; break; ccount++; break; if (option a") else if (option == b") acount++; bcount++; else ccount++;

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents