zoukankan      html  css  js  c++  java
  • javascripct流程语句

    1、条件选择

          if 语句:只有当指定条件为true时,使用该语句来执行代码

         if...else语句:当条件为true时执行代码,当条件为 false 时执行其他代码

         if...else if....else 语句:使用该语句来选择多个代码块之一来执行

    var x=""; 
    if (time<10) 
     { 
     x="Good morning"; 
     } 
    else if (time<20) 
     { 
     x="Good day"; 
     } 
    else 
     { 
     x="Good evening"; 
     } 

          switch语句: 使用该语句来选择多个代码块之一来执行。switch 语句用于基于不同的条件来执行不同的动作 var x; 

    switch (d) 
     { 
     case 0: 
     x="Today it's Sunday"; 
     break; 
     case 1: 
     x="Today it's Monday"; 
     break; 
     case 2: 
     x="Today it's Tuesday"; 
     break; 
     case 3: 
     x="Today it's Wednesday"; 
     break; 
     case 4: 
     x="Today it's Thursday"; 
     break; 
     case 5: 
     x="Today it's Friday"; 
     break; 
     case 6: 
     x="Today it's Saturday"; 
     break; 
    //defualt: ..... }

    2、循环

        

    (1)for语句:循环代码块一定的次数

    1
    2
    3
    4
    for(var box=1;box<=10;box++)
    {
     document.write("box="+box+"<br/>");
    }

    (2)for...in语句: 循环遍历对象的属性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var box={
     name:"张三",
     age:24,
     sex:"男"
     };
    for(x in box)
    {
     document.write(box[x]+"<br/>");
    }

    (3)while语句:当指定的条件为 true 时循环指定的代码块。先判断,再执行语句,这种比较实用。

    1
    2
    3
    4
    5
    6
    var box=1;
    while(box<=5)
    {
     document.write("box="+box+"<br/>");
     box++;
    }

      

     (4)do...while - 同样当指定的条件为 true 时循环指定的代码块。先执行一次,再判断

    1
    2
    3
    4
    5
    var box=1;
    do{
     document.write("box="+box+"<br/>");
     box++;
    }while(box<=10)

    3、其他

    (1)break语句:用于跳出循环。

    1
    2
    3
    4
    5
    6
    7
    8
    for(var box=1;box<=10;box++)
     {
     if(box==5)
     {
     break;//强制退出整个循环
     }
     document.write("box="+box+"<br/>");
     }

    (2)continue语句:用于跳过循环中的一个迭代。

    1
    2
    3
    4
    5
    6
    7
    8
    for(var box=1;box<=10;box++)
    {
     if(box==5)
     {
     continue;//退出当前循环,还会继续执行后面的循环
     }
     document.write("box="+box+"<br/>");
     

    执行到第四次循环时,跳出第五次循环,继续向下面执行,输出的少了box=5。
    (3)with语句:将代码的作用域设置到一个特定的对象中
          先来看一般我们是怎么样输出对象的属性的值的:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    var box={
     name:"张三",
     age:24,
     sex:"男"
     };
     var n=box.name;
     var a=box.age;
     var s=box.sex;
     document.write(n+"<br/>");
    document.write(a+"<br/>");
    document.write(s);

          

  • 相关阅读:
    麒麟短线王实战技法
    Silverlight的资源
    Windows Live SkyDrive, Windows Live Sync 和 Live Mesh
    Listview.Subitem.BackColor.ForeColor改变字体颜色和背景
    windows mobile控制面板程序
    Windows Azure百度百科
    wcf中如何Host多个WCF服务?
    强弱跟踪
    修改默认的HTTP Response Header
    DataTable 内部索引已损坏
  • 原文地址:https://www.cnblogs.com/vanstrict/p/5678862.html
Copyright © 2011-2022 走看看