循环语句


    一、命名时,*不能放在首位上,也不能放在命名字的中间,以免和乘号(*)相冲;

    二、流程控制语句 :

        1、var a = window.prompt("请输入一个数值")可以用于在网页上进行检测数字;

        2、if(a . trim):字符串运用的方法,可以去掉前后的空格;

        3、条件语句:if else:(判断)

            if(表达式){
                if(表达式2){
                            语句1;
                }
            } else if(表达式3){
                                语句2
              }

        4、switch语句:

            var a = window.prompt("请输入一个数")

            switch (a) {
                            case"1":
                                console.log("你输入的数字为1");

            可以添加break:用来跳出switch,影响显示的个数

                            case"2":
                                console.log("你输入的数字为2");

                            case"3":
                                console.log("你输入的数字为3");
            可以添加default:其他数值
                            ......
                        }

----------


    三、循环语句:要先弄清楚流程,执行的顺序

        1、while 循环:
            
            while(表达式){
                            语句1;}
                        语句2;(不1则2)

            例:1+2+3+4+5

                var i = 1 ;(循环变量)
                var sum = 0 ;
                while ( i <= 5 (循环的次数)){
                                                sum += i ;
                                                i++ ; }
                    console.log(sum);

        2、do while 循环:

            do {语句1
                }while (表达式);
                语句2;

            例:1+2+3+4+5

                var i = 1 ;(循环变量)
                var sum = 0 ;
                do {
                    sum += i ;
                    i++;
                }while( i <= 5 );
                    console.log(sum);

        3、for循环:

            for(表达式1 表达式2 表达式3){
                                        语句
                }

            例:1+2+3+4+5

            for(var i = 1 ; i <= 5 ; i++ ){
                        sum += i;                                                        
                }
                    console.log(sum);

        document.write (在网页上输出,不在console上输出);

        console.log(Math.pow(3,3)); 3的3次方

posted @ 2016-05-03 01:23  白教主  阅读(123)  评论(0编辑  收藏  举报