Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©2005  Department of Computer & Information Science Switch Statements.

Similar presentations


Presentation on theme: "Copyright ©2005  Department of Computer & Information Science Switch Statements."— Presentation transcript:

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

2 Copyright ©2005  Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to program switch statements with case blocks to test a single variable against multiple possible values.Understand how to program switch statements with case blocks to test a single variable against multiple possible values.

3 Copyright ©2005  Department of Computer & Information Science Nesting Decision Structures? Of course, JavaScript allows us to program nested if-then-else structures to deal with multiple possible situations.Of course, JavaScript allows us to program nested if-then-else structures to deal with multiple possible situations. However, sometimes it is easier to use a switch statement instead, especially if you are testing a single variable against multiple possible values …However, sometimes it is easier to use a switch statement instead, especially if you are testing a single variable against multiple possible values …

4 Copyright ©2005  Department of Computer & Information Science The switch Statement The switch statement allows us to test a single variable against multiple possible values.The switch statement allows us to test a single variable against multiple possible values. We test each possible value in case blocks, each of which includes a value against which we test and an executable block, executed if the value matches.We test each possible value in case blocks, each of which includes a value against which we test and an executable block, executed if the value matches.

5 Copyright ©2005  Department of Computer & Information Science More on the switch Statement We can have any number of cases. Each case's executable block needs to contain a break statement at the end in order to "break out" of the switch structure, if we found a matching value.We can have any number of cases. Each case's executable block needs to contain a break statement at the end in order to "break out" of the switch structure, if we found a matching value. It's a good idea to include a default case at the end of the switch statement.It's a good idea to include a default case at the end of the switch statement.

6 Copyright ©2005  Department of Computer & Information Science switch Statement – General Form switch(variable) { case value1: //Block for value1 break;default: //Block for default }

7 Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called switchCase_01.html.

8 Copyright ©2005  Department of Computer & Information Science Summary We can use switch statements to test single variables against multiple possible values.We can use switch statements to test single variables against multiple possible values. case blocks provide executable blocks for matching values in a switch statement.case blocks provide executable blocks for matching values in a switch statement. continued …

9 Copyright ©2005  Department of Computer & Information Science Summary Each case block must end with a break statement.Each case block must end with a break statement. It's considered good programming to include a default block after all case blocks.It's considered good programming to include a default block after all case blocks.


Download ppt "Copyright ©2005  Department of Computer & Information Science Switch Statements."

Similar presentations


Ads by Google