js语句使用介绍
程序员文章站
2023-09-19 15:55:38
js语句使用介绍
/**
* created by administrator on 2017/12/24.
* 1.顺序语句
* 2.选择语句 if else...
js语句使用介绍
/** * created by administrator on 2017/12/24. * 1.顺序语句 * 2.选择语句 if else swtich case * 3.循环语句 for while do...while */ //选择语句 if //if(条件成立){执行代码} var a = 100; if (a > 0 && a < 101){ console.log("a > 0"); }else { console.log("go out of if"); } a = 75; if (a >=80){ console.log("优秀"); }else if(a >= 70){ console.log("良"); }else if(a >= 60){ console.log("及格"); }else{ console.log("不及格"); } console.log("======================"); //swtich var index = 5; switch (index) { case 1: console.log("1"); break; case 2: console.log("2"); break; case 3: console.log("3"); break; case 4: console.log("4"); break; case 5: console.log("5"); break; default: console.log("default"); break; } console.log("======================"); //循环结构 while for do...while a = 10; while(a > 0 ){ console.log(a); a--; } console.log("======================"); //编写程序1+2+3+...100的和 //第一种 var sum = 0; var i = 1; while(i <= 100){ sum += i; i++; } console.log(sum); //第二种 for( sum = 0 , i =1; i <= 100; i++ ){ sum += i; } console.log(sum); console.log("======================"); //计算100以内所有的奇数的和 //第一种 sum = 0; i = 1; while(i <= 100){ if ((i % 2) == 0){ i++; continue; } sum += i; i++; } console.log(sum); //第二种 for ( sum = 0, i = 1; i<=100; i++){ if ( (i % 2) == 0){ continue; } sum += i; } console.log(sum);
下一篇: 骨关节炎饮食有什么禁忌吗