Js中的控制結構

來源:互聯網
上載者:User

 

Js 中的控制結構

  Switch的使用

var day = 12;

//      switch (day) {

 

//          case 1:

//             document.write("星期一");

//              break;

//          case 2:

//             document.write("星期二");

//              break;

//          case 3:

//              document.write("星期三");

//              break;

//          case 4:

//             document.write("星期四");

//              break;

//          case 5:

//             document.write("星期五");

//              break;

//          case 6:

//              document.write("星期六");

//              break;

//          case 7:

//             document.write("星期七");

//              break;

//          default:document.write("請輸入-7之間的數字");

//      }

用與不用break的區別如下例所示: www.2cto.com

//      //定義年月日三個變數並賦值 計算此日期是當年的第幾天

//      var year =2012; //年

//      var month =5;//月

//      var day = 12;//日

//      var days = 0;

//      switch(month-1) {

//          case 11:days += 30;

//          case 10:days += 31;

//          case 9:days += 30;

//          case 8:days += 31;

//          case 7:days += 31;

//          case 6:days += 30;

//          case 5:days += 31;

//          case 4:days += 30;

//          case 3:days += 31;

//          case 2:

//         if((year%4==0&&year%100!=0)||(year%400==0)){

//             days+=29

//          

//          }

//          else{

//        

//           days +=28;

//          }

//        

//          case 1:days += 31;

 

 

//      }

//      days += day;

//     document.write(year+"年"+month+"月"+day+"日,是今年的第"+days+"天");

 

For 迴圈的使用:

for (a = 1; a < 10; a++) {

//         document.write("a");

 

 

//      }

 

While  與do while 的使用

 

//      n = 1;

 

//      while (n <=10) {

//          n++;

 

 

 

//      }

//      //do while 是先執行在判斷

//      m = 9;

//      do {

 

//         document.write("m");

//      }

 

作者 Aweijun360

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.