JavaScript and process control operators

First, the operator Detailed

  1, arithmetic operators Description: ++ (increment operator), - (decrement operator)  Syntax: + NUM, NUM +, - NUM, num--  Function: variable itself incremented or decremented by 1 examples:     var NUM = 10;     NUM ++; // +. 1 corresponds NUM = NUM;     the console.log (NUM);       num--; // equivalent. 1-NUM = NUM;         the console.log (NUM); Note: ( 1) for the increment decrement operators, if present in front of the operator, the first increment or decrement its own, and then execute the current statement.    (2) The increment decrement operators, if present in the back of the operator, the first execution of the current statement is finished and then decrement their self-energizing. Examples:        var NUM = 10;        the console.log (NUM ++); // first calculates itself, further participation statement execution        console.log (num ++); // first participation statement is executed, then the calculation itself        console.log (num);  2, assignment operators description: + =, - =, * =, / =,% =  syntax: NUM + =. 6;  explanation:
  


  





  
  

  





 
  

Represents accumulation, [num + = 6;} corresponding to {num = num + 6;] Examples:     var NUM = 10;     NUM = +. 1; // NUM = NUM +. 1; NUM ++;     the console.log (NUM); . 3, compare operator description: comparison operators comparison rules can be based on the values of two variables, returns a boolean that indicates whether the rule is satisfied.   Type: ==, ===,! ==,! ===,>,> =, <, <= examples:     var num1 = 15;     var num2 = 30;     var num3 = '15';     the console.log (num1 == num3); Consideration: size comparison can be digital, then string can not compare it? If so, how comparison? [ Comparison between js string in accordance with the priority rank, compare the ASCII code of each character size, including a comparison between the number string. ] Examples:     1.console.log ( ". 1" < ". 3"); // to true      2.console.log ( '. 5' < '10'); // to false       3.console.log ( "123" < " 123 "); // to false     4.console.log ("
  




  
  

  




  
  
  



     was item1 = undefined;

      var item2 = null;
      console.log(item1==item2);
      console.log(item1===item2);  // false
      console.log(undefined===undefined);
      console.log(null===null);

  4、布尔运算符
  描述:&&(与)、||(或)、!(非)
  语法:
     (1)&&与运算符:(表达式1)&&(表达式2);
          运算符两端表达式结果均为true的时候,整个表达式为true,有一个为false,则整个结果为false
        var num1 = 15;
        var num2 = 30;
        var result = (num1>5)&&(num2<50);
      console.log(result);

     (2)||或运算符:(表达式1)||(表达式2);
       运算符两端表达式任一结果为true的时候,整个表达式为true,两者皆为false,则整个结果为false
       var num1 = 15;
       var num2 = 30;
       var result = (num1>5)||(num2>50);
       console.log(result);

      (3)!非运算符:!(表达式)
      表达式结果为true,则整个表达式结果为false;
      表达式结果为false,则整个表达式结果为true;
      var num1 = 15;
      var result = !(num1>5);
      console.log(result);

  5、运算符优先级(截取常用部分)
    初等单目一二级    //()小括号是初等运算符,因为优先级最高
    乘除取余加减移
    关系等于不等于
    与或赋值终对齐
      注:
      (1)运算符的优先级按照出现的顺序排列,谁先出现谁优先级高。
      (2)
单目:一个运算数 i++, i--, !a
      
(3)双目:两个运算数 a+b a-b a*b
        (4)三目:三个运算数 bc=a?b:c; (if(a) bc =b;else bc =c;)

二、流程控制

1、if语句
描述:表示判断表达式是否成立,如果成立执行大括号中的语句。如果不成立直接跳过。
语法:if(表达式){ 语句; }
例子:
  var numInput = document.getElementById('num');
  var btn = document.getElementById('btn');
  btn.onclick = function () {
  var num = parseInt(numInput.value);
  //如果用户输入的数字大于10,那么提示合法,否则提示不合法
  if(num>10){
  alert('输入合法');
  }
  if(num<=10){
  alert('输入不合法');
  }
  };

注意:
  (1)表达式必须放在小括号之中。执行语句必须放在大括号内。
  (2)if条件语句最后不用加分号。
  (3)执行语句如果只有一句,那么大括号可以不写。但是推荐所有情况都加上大括号。
  【如果不写大括号,那么默认if后面的第一个语句是if的执行语句】

 

2、if-else语句
描述:如果满足条件执行if中的语句,如果不满足则执行else中的语句。
  if(表达式){
  语句1;
  ...
  }else{
  语句2;
  ...
  }
例子:
  var num = 10;
  if(num>0){
  console.log('输入合法');
  }else{
  console.log('输入不合法');
  }
  console.log('hello world!');
注意:
  (1)if-else语句是if的升级版本,表示二元判断,一定会执行一种情况。
  (2)if-else是一个语句,末尾不加分号。并不意味着执行语句不加分号!
  (3)if后面的表达式,只要能返回一个布尔值即可。
例子:
  var num = 10;
  if(true){
  console.log('输入合法');
  }else{
  console.log('输入不合法');
  }
  console.log('hello world!');

 

3、if-else if语句
描述:
  判断表达式1是否成立,如果表达式1成立执行语句1并if结构立即结束。
  如果不成立则判断表达式2是否成立,如果表达式2成立执行语句2并if结构立即结束。
  如果不成立则判断表达式3是否成立,如果表达式3成立执行语句3并if结构立即结束。
  ...
  如果以上所有条件都不成立,则直接执行else语句,并立即结束。

语法:
  if(表达式1){
  语句1;
  }else if(表达式2){
  语句2;
  }
  ...
  else{
  语句4;
  }
注意:
  (1)if-else if末尾没有分号
  (2)else语句不能单独存在,总是向上查找离他最近的if,构成if-else结构
  例子:
    var bmi = 22.9;
    if(bmi>22.3&&bmi<23.9){
    console.log('健康');
    }else if(bmi<22.3){
    console.log('太瘦了');
    }else{
    console.log('太肿了');
    }

 

4、switch
描述:在多种情况下判断,并选择一种情况执行的语句结构。
语法:
  switch (表达式){
  case 结果1:{执行语句1;...}
  break;//当执行了语句1之后,能够跳出并结束switch语句
  case 结果2:{执行语句2;...}
  break;
  ...
  default:{执行语句n;}
  }
解释:如果【表达式的结果】等于【结果1】,就执行语句1,并结束switch。
如果不等于,则判断是否等于结果2,如果等于就执行语句2,并结束switch。
如果不等于,则判断是否等于结果3,如果等于就执行语句3,并结束switch。
...
如果所有case后面的结果都不满足,那么直接执行default后面的语句,并结束switch。
注意:
  (1)switch语句当中case后面的break必须要写。如果不写,则会从满足的这个case开始将后面所有的情况全都执行一遍。
  (2)default语句理论上可以不写,如果不写当都不满足时switch直接跳出。
  (3)switch语句一定会执行一种情况对应的语句(语法结构完整情况下)

 

5、while循环语句
描述:当满足循环条件的时候执行循环体,直到不满足为止,跳出循环。
语法:
  while(循环条件){
  循环体
  }
注意:
  (1)循环变量必须在循环中,向着循环结束的方向发生变化。
  【如果不改变,或改变方向反了,则会将循环变成无限循环。也叫死循环。】
  (2)while循环最少执行次数为0次
  (3)循环执行开始之前,循环变量必须先赋初值
  (4)循环体中如果只有一条语句,那么大括号可以不写。(最好写上)
  (5)while循环末尾没有分号!
例子:
  输出10以内的偶数

  var num = 1;
  while(num<11){
  //只要满足条件,就意味着num还在1-10之间。
  //那么就判断num当前是不是偶数即可。
  if(num%2==0){
  console.log(num+'是偶数');
  }
  num++;
  }

 

6、do-while循环语句
描述:先执行一次循环体。
然后判断循环条件,如果满足则继续执行循环体。
如果不满足,则跳出循环,循环终止。
语法:
  do{
  循环体
  }while(循环条件);

说明:do-while循环的注意事项、逻辑结构基本都和while循环相同,
唯一的区别就是do-while循环会先执行一次循环体,
而while循环则是先判断循环条件。
例子:
  var i = 0;
  do{
  if(i%2==0){
  console.log(i+'是偶数');
  }
  i++;
  }while(i<10);

 

7、for循环语句
描述:能够设定循环多少次,并直接设定循环条件和循环增量的语法结构。
语法:
  for(表达式1;表达式2;表达式3){
  循环体
  }
例子:
  for(var num = 0; num<10; num++){
  console.log(num);
  }
解释:
  【1】执行表达式1:var num=0; 循环变量赋初值
  【2】执行表达式2:num<10; 判断循环能否继续执行
  (1)满足条件,循环继续,执行循环体
  (2)不满足条件,循环终止
  【3】因为满足条件,因此执行循环体:console.log(num);
  【4】执行表达式3:num++; 让循环变量向着循环结束的方向变化
  【5】执行表达式2...
注意:
  (1)表达式1可以不写,如果不写需要在for循环之外,给循环变量赋初值
  (2)表达式2可以不写,如果不写代表循环条件恒成立(死循环)
  (3)表达式3可以不写,如果不写需要在for循环的循环体中,让循环变量发生改变
  (4)三个表达式实际上可以都不写,但是用于间隔的分号必须写!

 

8、辅助流程控制语句
1.break
描述:用来终止代码块或者循环,循环立即终止。
例子:
  for(var i = 0;i<100;i++){
  if(i == 10){
  console.log('我需要这个10');
  break;//循环在此立即终止
  }
  console.log(i);
  }

2.continue
描述:用来终止本次循环,并立即开始下一次循环,循环不终止。
例子:
  for(var i = 0;i<100;i++){
  if(i == 10){
  console.log('我需要这个10');
  continue;//立即终止本次循环,并立即开始下一次循环。
  }
  console.log(i);
  }

Guess you like

Origin www.cnblogs.com/meijifu/p/12113255.html