instructional systems
Index:
[Session12] JavaScript 2 (Basic syntax, control structure (if, for, etc.))
1 Chapter1
2 Chapter2
3 Chapter3
4 Chapter4
5 Chapter5
Your Location: Home Page  >  [3] Interactive web pages using JavaScript (on condition)  >  [Session12]JavaScript 2 (Basic syntax, control structure (if, for, etc.))  >  Chapter4  >  4.2 Conditional Branch Control Statement 2 (switch...case)
Conditional Branch Control Statement 2 (switch...case)
[The purpose of this section]
The conditional branch control statement is also called a selection control statement, and in JavaScript, there are if...else statement and switch...case statement, to be used depending on the conditional branch statement. In this section, switch statement is explained.

The Basic of switch...case Statement

The switch...case statement takes the following format.。

switch (integral constant) {
  case integral constant 1: statement 1; statement 2;...;break;
  case integral constant 2: statement 3; statement 4;...;break;
  case integral constant 3: statement 5; statement 6;...;break;
            ..........;
  default: statement 7; statement 8;...;
}

The integral constant in the switch is not evaluation expression (TRUE/FALSE value) but is a numerical value. The case statements as the labels are described in as many handling items as necessary. At the side of a case statement, only one numeric label is written. If no sets of numeric label and integral constant of the case statements agree, the default label statement will apply. Although this default statement can be omitted, if none of numerical label and integral constant agree, then the control will move to the next line of switch control statement. If the numeric label agrees with the integral constant of the case statement, the control will move to the case statement that has the matching numeric label, and multiple statements will be executed until “break;” statement appears. Upon the arrival of the “break;” statement, the control will move to outside of the switch statement. For details, see the following program.

Sample 1 (switch1.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <meta http-equiv="Content-Script-Type" content="text/javascript
 charset=utf-8">
 <title>Experiment of switch...case statement 1</title>
</head>
<body>
<h3>Experiment of switch...case statement 1</h3>
 <script type="text/javascript">
 <!--
    document.write('<br>');
    k=2;
    switch(k) {
      case 1:
      case 2: i=0;j=9;
              document.write(‘k=‘,k,’ then i=‘,i,’ j=‘,j); break;
              document.write('<br>');
      case 3: i=3;j=4;
              document.write(‘k=‘,k,’ then i=‘,i,’ j=‘,j); break;
              document.write('<br>');

     default: document.write('We got nothing.');
    }
 //-->
 </script>
</body>
</html>

サンプル2(swtich2.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <meta http-equiv="Content-Script-Type" content="text/javascript
 charset=utf-8">
 <title>Experiment of switch...case statement 2</title>
</head>
<body>
<h3>Experiment of switch...case statement 2</h3>
 <script type="text/javascript">
 <!--
    day=new Date();
    a=day.getDay()
    switch(a) {
      case 0: document.write('It’s Sunday.',a);break;
      case 6: document.write('It’s Saturday.',a);break;
     default: document.write('It’s weekday. ',a);
    }
 //-->
 </script>
</body>
</html>

The “break;” statement suddenly appeared with switch case, but this too is one of the control statements, and has a very powerful function. When this “break;” appears in a conditional control statement, the control will move immediately to outside of the conditional syntax. Also, in the control statements like repetition control statement that we will explain later, the control will skip all the control statements at the moment the “break;” statement appears. Particularly when a repetition control statement exists inside the repetition statement, and when a “break;” statement exists in the inner repetition statement, and the “break;” statement is executed, the control will immediately move to the outside of the inner repetition statement.

Copyright (c) Yasuo Musashi 2003, All Rights Reserved