美文网首页
if条件、for循环、while循环

if条件、for循环、while循环

作者: ThiagoChang | 来源:发表于2018-06-08 21:14 被阅读0次

1. if语句

 if (1 > 0) {
       document.write('不错不错')
 }

当括号里的条件成立,花括号里的语句才会执行

 var score = parseInt(window.prompt('input'))
 if (score > 90 && score <= 100){
       document.write('alibaba');
 }else if (score > 80 && score <= 90){
       document.write('tencent');
 }else if (score > 70 && score <=80){
       document.write('baidu');
 }else if (score > 60 && score <= 70){
       document.write('mogujie');
 }else{
       document.write('error')
 }

2.for 循环

 document.write('abc')
 document.write('abc')
 document.write('abc')
 document.write('abc')
 document.write('abc')

等于

 for (var i = 0; i <5; i++){
       document.write('abc')

先判断i<5这个条件,然后执行{ }内的语句,再执行i++,一直重复,直到i<5这个条件不再成立


打印0-9:

 for (i = 0; i <10; i++){
       document.write(i)
 }

打印1-100中能被3/5/7整除的数:

 for(i = 1; i < 100; i++){
       if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0){
             document.write(i + " ")
         }
 }

i = 0这个赋值语句可以放在for()的前面;i++这个赋值语句可以放在花括号内部,且在花括号内需要执行的语句之后, 但是要用分号隔开:

 var i = 0;
 for (i < 10){
       document.write(i);
       i++;
 }

3.while循环:如果for循环里面只有一个条件,那么就等于while循环

 i = 0;
 while(i < 10){
       document.write(i);
       i++;
 }

相关文章

  • C语言中必须会用的语句

    1、循环 循环语句有for(初值;条件;循环结束后执行)循环 while(条件)循环 do{函数}while(条件...

  • while和do while循环语句

    while 循环和 do…while 循环的相同处是:都是循环结构,使用 while(循环条件) 表示循环条件,使...

  • While笔记

    #While循环 1.语法 while(循环条件) { 循环...

  • Java script 5.28-5.29 总结+案例

    一、循环语句 1)while循环语句 While 循环会在指定条件为真时循环执行代码块。 格式: while(条件...

  • Python 学习笔记 - 循环 while

    Python 循环 - while Python 中有 for 循环 while 循环 如果条件符合,while...

  • scala基础(2)

    scala控制结构 if条件表达式、while循环、for循环 if条件表达式 while循环 for循环 for...

  • js循环

    循环语句 while(条件){ 条件为true执行; } 列:while 循环 var a=1; while(a<...

  • Day3 while和for循环

    while(判断条件){运算条件;} for(初始化参数;循环条件;表达式)运算条件; while 循环循环条件判...

  • Dart 循环语句

    for 循环 while(表达式/循环条件){} do{语句/循环体}while(表达式/循环条件); break...

  • C++循环

    C++中的循环主要包含while循环、for循环、do…while循环以及嵌套循环,while循环就是当给定条件为...

网友评论

      本文标题:if条件、for循环、while循环

      本文链接:https://www.haomeiwen.com/subject/xzimsftx.html