It would be extremely small and the choice between switch I haven't bothered to test the JavaĬompiler because, if there is a speed difference, Or processing characters (whose codes are treated like numbers), youĮfficiency? Some compilers can produce moreĮfficient code for certain switch statements than forĮquivalent if statements. (eg, the number of the button that was clicked in a JOptionPane), If you are handling specific coded values However, selecting sections of code depending on specific integer values The ability of switch to choose between many sections ofĬode seems to make it more powerful than if. Policy of defensive programming - assume there will be bugs in your code The choice of if or switch should be based onĬomment = "You look so much better than usual." Ĭomment = "Your work is up to its usual standards." Ĭomment = "You're quite competent for so little experience." Ĭomment = "Oops - something is wrong with this code." Īlways include a default clause in your switch statement as a general When one of a number of blocks of code is selectedīased on a single value, the switch statement is generallyĮasier to read. Int which = (int)(Math.random() * 3) // Result is 0, 1, or 2.Ĭase 0: comment = "You look so much better than usual." Ĭase 1: comment = "Your work is up to its usual standards." Ĭase 2: comment = "You're quite competent for so little experience." ĭefault: comment = "Oops - something is wrong with this code." Ī switch statement can often be rewritten as an ifįor example, the preceding switch statement could be Flowing directly into the next case is almost always an error. If there is no break, execution flows thru into the It is written after the last case be convention, and typically isn't followedīy break because execution just continues out the bottom of switch if thisīreak The break statement causes execution to exit to the statement after theĮnd of the switch. This is the equivalent of the "else" for the switch statement. This begins the statements that are executed when the switch expression has that case value.ĭefault If no case value matches the switch expression value, execution continues at the default clause. case The case keyword is followed by an integer constant and a colon. If no case matched and there is no default clause, execution continues after the end of the switch statement. If there is no corresponding case value, the default clause is executed. Which exits the switch statement and continues with the statement following Normally the code in a case clause ends with a break statement, The switch statement executes the case corresponding to the value of the expression. Which is followed by the cases, all enclosed in braces. Syntax example Syntax switch ( expr) Switch keywords switch The switch keyword is followed by a parenthesized integer expression, Statements based on an integer (including char) or enum value. The switch statement allows you to choose from many Of code to execute based on a boolean value (only two possible values). The if statement allows you to select one of two sections Java Notes switch Statement - Overview Purpose of switch: select one of many possible statements to execute
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |