9
Copyright Copyright ©2005 ©2005 Department of Computer & Information Science Department of Computer & Information Science Switch Statements Switch Statements

Copyright ©2005 Department of Computer & Information Science Switch Statements

Embed Size (px)

Citation preview

Page 1: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Switch StatementsSwitch Statements

Page 2: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

GoalsGoals

By the end of this lecture you By the end of this lecture you should …should …

• Understand how to program Understand how to program switch statements with case switch statements with case blocks to test a single variable blocks to test a single variable against multiple possible values.against multiple possible values.

Page 3: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Nesting Decision Nesting Decision Structures?Structures?

• Of course, JavaScript allows us to Of course, JavaScript allows us to program nested program nested if-then-elseif-then-else structures to deal with multiple structures to deal with multiple possible situations. possible situations.

• However, sometimes it is easier to use However, sometimes it is easier to use a a switchswitch statement instead, especially statement instead, especially if you are testing a single variable if you are testing a single variable against multiple possible values …against multiple possible values …

Page 4: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

The The switchswitch Statement Statement

• The The switchswitch statement allows us to statement allows us to test a single variable against multiple test a single variable against multiple possible values. possible values.

• We test each possible value in We test each possible value in casecase blocks, each of which includes a blocks, each of which includes a value against which we test and an value against which we test and an executable block, executed if the executable block, executed if the value matches.value matches.

Page 5: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

More on the More on the switchswitch StatementStatement

• We can have any number of We can have any number of casescases. . Each Each case'scase's executable block needs to executable block needs to contain a contain a breakbreak statement at the end in statement at the end in order to "break out" of the order to "break out" of the switchswitch structure, if we found a matching value.structure, if we found a matching value.

• It's a good idea to include a It's a good idea to include a defaultdefault case at the end of the case at the end of the switchswitch statement.statement.

Page 6: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

switch Statement – General switch Statement – General FormForm

switch(switch(variablevariable)){{

case case value1value1:://Block for //Block for value1value1break;break;

default:default://Block for default//Block for default

}}

Page 7: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called switchCase_01.html.

Page 8: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

SummarySummary

• We can use We can use switchswitch statements statements to test single variables against to test single variables against multiple possible values.multiple possible values.

• casecase blocks provide executable blocks provide executable blocks for matching values in a blocks for matching values in a switchswitch statement. statement.

continued …continued …

Page 9: Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

SummarySummary

• Each Each casecase block must end with a block must end with a breakbreak statement. statement.

• It's considered good It's considered good programming to include a programming to include a defaultdefault block after all case block after all case blocks.blocks.