13_JavaScript Basics (3)

Conditional branch statement

Conditional branch statement, also called conditional statement, is to perform some statements in accordance with certain conditions, does not perform some statements.
JS There are three syntax can be expressed conditional branching.

1.if……else……

Conditional branch of the main grammar, syntax can write all the main conditional branching statements. In other words, while we learn switch case statement, switch case statement is capable of, if else statements must also be competent.
Yesterday already mentioned, refresher:
the structural integrity of the standard:

if(){

}else if(){

}else if(){

}else{

}

Only one else, and be sure to last. He said that the situation when all branches are not met executed.
Can no else:

if(){
    
}else if(){
    
}else if(){
}
可以没有分支:
if(){

}

Sights floor location:

if(8 > 5){
    console.log("哈哈");
}
console.log("嘻嘻");   //这行语句不在if里,所以不管if成功没有,都执行

If only one line statement, you can omit the braces:

if(8 > 15)
    console.log("哈哈");
console.log("嘻嘻");

Jumping phenomenon, should be rational use, such as this morning's bmi Morning Quiz!

var a = 4;
if(a < 8){
    a++;    //a变为5,然后跳楼,不会进行下面的a==5的比较了
}else if(a == 5){
    a++;
}
console.log(a);  //5

2.switch case statement

First look at a simple example:

switch(xingzuo){
    case "白羊座" :
        alert("今天要注意调整自己的状态,有些人的睡眠质量比较差");
        break;
    case "金牛座" :
        alert("今天会在感情方面遇到一些小小的问题");
        break;
    case "双子座" :
        alert("要做好心理准备会在工作上被他人挑剔找茬,这是你避免不了的事情");
        break;
    case "巨蟹座":
        alert("工作上要打醒十二分精神,要不然很大可能会经常要返工哦");
        break;
    case "狮子座":
        alert("恋爱中的人今天的心有点摇摆不定,有人可能会有精神上的出轨");
        break;
    default :
        alert("对不起,我们没有收录你的星座运势!");
        break;
}

grammar:

switch(待检测值){
    case 值1 :
        值1 与 待检测值 相同时做的事情
        break;
    case 值2 :
        值2 与 待检测值 相同时做的事情
        break;
    case 值3 :
        值3 与 待检测值 相同时做的事情
        break;
    default :
        默认要做的
        break;
}

switch caseWhat philosophy statement is? The meaning of existence, simplifies if……else……writing:

You will find the following branches of the if statement, the judge xingzuo is not at all certain, and the strings are equal, bloated:

if(xingzuo == "白羊座"){
    alert("今天要注意调整自己的状态,有些人的睡眠质量比较差");
}else if(xingzuo == "金牛座"){
    alert("今天会在感情方面遇到一些小小的问题");
}else if(xingzuo == "双子座"){
    alert("要做好心理准备会在工作上被他人挑剔找茬,这是你避免不了的事情");
}else if(xingzuo == "巨蟹座"){
    alert("工作上要打醒十二分精神,要不然很大可能会经常要返工哦");
}else if(xingzuo == "狮子座"){
    alert("恋爱中的人今天的心有点摇摆不定,有人可能会有精神上的出轨");
}else{
    alert("对不起,我们没有收录你的星座运势!");
}

We tested and found to switch the same determination, the bottom layer is relatively === comparison even with Type

<script type="text/javascript">
    var a = "5";
    switch(a){
        case 5 :
            alert("哈哈");  //不会弹出!!因为类型不一样
            break;
    }
</script>

If you do not write the break, then switch statement insisted on the performance of interesting, in addition to the implementation of this case inside the statement, will also perform the following case statement unconditional, until I met a break stop it.

var a = 5;
switch(a){
    case 4:
        alert("我是4");   //没有通过验证,不执行
    case 5:
        alert("我是5");   //弹出
    case 6:                //无视这行case,直接直接这行case里面的语句
        alert("我是6");   //弹出, 
        break;             //挡住了,就不能继续往下运行了
    case 7:
        alert("我是7");
        break;
    default:
        alert("我是默认");
}

// user input the month, tell it how many days this month

switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        alert("这个月有31天");
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        alert("这个月有30天");
        break;
    case 2:
        alert("这个月一般来说是28天,可能29天");
}

Beginners are the most adorable mistakes:

switch(month){
    case 1 || 3 || 5 || 7 || 8 || 10 || 12 :
        alert("这个月有31天");
        break;
    case 4 || 6 || 9 || 11 :
        alert("这个月有30天");
        break;
    default :
        alert("我是默认");
}

Write does not work! Because the 1 || 3 || 5 || 7known expression, a computer met expression, the calculation will be evaluated
1 || 3 || 5 || 7 || 8 || 10 || 12

A value of the expression is equivalent to the case 1:

The switch statement can only do such a thing. A value is to see the situation too few to perform different thing.
If you are within a certain range, children do something, also, but does not recommend such a way. At this point you should use if ...... ...... else if

switch(true){
    case score < 60 :
        alert("不及格");
        break;
    case score < 75 :
        alert("及格");
        break;
    case score < 85 :
        alert("良好");
        break;
    case score <= 100 :
        alert("优秀");
        break;
    default :
        alert("你输入的成绩错误");
}

3. ternary operator

? :Is a group of operators, which is a JS only three elements need to participate operator.

条件 ? val1 : val2
Expression of value, depending on condition is true or false. If the condition is true, then the value is val1 expression. If the condition is false, the value of the expression is val2.

Simple example:

console.log(true ? 3 : 8);  //3
console.log(false ? 3 : 8); //8

Ternary operator, so often use:
the year-end award multiple employees, with a monthly salary related. Salary greater than 8000, the coefficient is 1.2. 1 otherwise.
var xishu = salary > 8000 ? 1.2 : 1;
Equivalent to:

if(salary > 8000){
    xishu = 1.2;
}else{
    xishu = 1;5 }

To summarize, conditional branching statements a total of 3, but the wording of a short circuit && || also have a sense of choice, counted a total of four kinds.
if……else if……(Must immediately think of! This thing invincible)
switch case(one respect very good, reflected in the clarity of your code)
ternary operator (the time of the assignment, there are two possible values depending on the condition, immediately thought of the ternary operator)
&& short-circuit wording (experience problems with the unfamiliar right)

loop statement

JS in the flow control statement to two: a conditional branch, loop. By these two statements, we can complete all the procedures.
Loop is repeated until a series of commands that meet specific criteria.
Loops can solve a lot of similar statements written.

1.for loop

The overall perception:

for(var i = 1 ; i <= 99 ; i++){
    console.log("第" + i + "次说我爱你"); 
}

You will find, for loop is a shell, so that the variable i in turn, turns a value of 1, 2, ...... 99.
We called the loop variable i variable that can be set, we are used to represent the i loop variable
syntax, the most prone to error, and finally no semicolon:
for (var i = 1; i <= 99; i ++; ) {← wrong writing
console.log ( "first" + i + "time to say I love you"); 3}

The nature of the for loop, must be clear.
for nature

The system meets the for loop will execute the statement ① immediately, then declares a variable i, value of 1.

The system will immediately detect whether ② meet this conditional expression, if it is true, execution ③; if ② is false, out of the loop, execute the statement ⑤.

After performing ③, the system performs an immediate statement ④, ② sentence is then detected again, if true, do ③, if false, do ⑤;

After performing ③, the system performs an immediate statement ④, ......

2. exhaustive idea

We beginners, the most critical is to have an exhaustive idea for loop.
For example, now we want to find all about the number 48.
What about the number of: a ÷ b no remainder, divisible. Then we say a is a multiple of b, b is called a divisor of.
The computer is a simple-minded, you can not tell immediately what number divisible by 48, so we have to let the computer from the beginning, one by one experiment.
We try not number 1 is about 48 of
us try 2 is not about the number 48
that we try not number 3 is about 48
......
we try to number about 48 are not 48

To list all the possibilities, and then screened. For the whole law is to give meaning listed. Chinese relatively genteel, brute-force method.
For a grander sight, a higher level.

For example, we are now looking for a few daffodils. Number of daffodils, a three-digit, each digit of the cube and equal to itself.
153 For example, since 13 + 1 = 53 + 33 + 125 + 27 = 153. So 153 is the number of daffodils.
Find all daffodils number 100 to 999.
Exhaustive, from 100 to 999, a a test,
we try number 100 is not daffodils
we try narcissistic number 101 is not
we try narcissistic number 102 is not
we try number 103 is not daffodils
... ...
we try not narcissistic number is 999

for loop, there is a small application called accumulator.
For example, we now want to calculate 1 + 2 + 3 + 4 + ...... 100

var sum = 0;  //累加器
for(var i = 1 ; i <= 100 ; i++){
    sum = sum + i;
}
console.log(sum);

Calculation of 6!

var sum = 1;   //累乘器,累乘器的初值是1
for(var i = 6 ; i >= 1 ; i--){
    sum = sum * i;
}
console.log(sum);

Guess you like

Origin www.cnblogs.com/shy-kevin/p/12058649.html