JavaScript as a scripting language, branch loop is an essential part, but it is somewhat different from branch loops in other languages. Let me introduce it in detail~
1. if conditional statement
if means if, and the logic of writing code is, if condition 1 is met, statement 1 is executed, otherwise, statement 2 is executed. This is the simplest if-else structure.
<script type="text/javascript">
var a = 3;
var b = 5;
if(a>b){
document.write('happy');
}else if(a==b){
document.write('fine');
}else{
document.write('sad');
}
</script>
The final print result is sad, 3<5, which does not meet the first two conditions and belongs to the other (else) situation, so the sad is output. If the conditions are changed to, a = 5, b = 3;
At this time, the first condition is met, and the latter two conditions are not executed. The output result is happy.
2. switch selection statement
Represents multi-condition selection, which code block is executed according to which case value is met. The value after switch must be a basic type value, not a reference type value. Case h can be followed by expressions. A break is required at the end of the code block of each case. If not, the execution will continue until a break is encountered. If it is followed by continue, the current statement will not be executed and other statements will be executed. It's a bit nonsense (ఠൠఠ )ノ, forget it and just go to the code demonstration.
<script type="text/javascript">
var num = 3;
switch (num){
case 1:
document.write("星期一");
break;
case 2:
document.write("星期二");
break;
case 3:
document.write("星期三");
break;
case 4:
document.write("星期四");
break;
case 5:
document.write("星期五");
break;
case 6:
document.write("星期六");
break;
case 7:
document.write("星期日");
break;
default:
document.write("出错了哦!没有这个星期~");
break;
}
</script>
The demo result of the above code is Wednesday. First, change the value of num to 5, and then remove all breaks in case 5 and the following. The result is (Friday, Saturday and Sunday are wrong! No this week~), this is the penetration of break . Simply put, the difference between break and continue. [I don’t understand, you can send me a private message~]
- break: Jump out of the entire loop and the loop ends.
- continue: Go past this cycle and continue to the next cycle.
3. for loop
Format: for(statement 1; statement 2; statement 3) {
The code block to be executed
}
Note: Statements 1, 2, 3 can be omitted, for (;;) will form an endless loop, it is not recommended to try, you can do it if you are curious~~
Case: Write a 1--100 accumulation! The result is 5050, you can verify it~
<script type="text/jscript">
var sum =0;
for(i=0 ;i<=100;i++){
sum+=i;
}
console.log(sum);
</script>
4. for in traversal *
The for in statement loop is to traverse the properties of an object, which is mostly used for compound types such as objects and arrays.
Format: for(key in object){
Code block
}
<script type="text/javascript">
var dog = {id:"001",name:"旺财",age:3};
for(key in dog){
document.write(key+":"+dog[key]);
document.write('<br/>');
}
</script>
The following figure is the result of the demonstration:
5. While loop
Format: while(expression){
Code block
}
While(true) and while(1) are very typical infinite loops, don’t try, they will break easily~
[Press Ctrl+F4 to forcibly end the loop. This method may not work because of different software systems. If it doesn’t work, just turn off the process.]
<script type="text/javascript">
//变量初始化
var i = 1;
//条件判断
while(i<=10){
//如果是奇数,则输出
if(!(i%2==0)){
document.write(i+" ");
}
//变量更新
i++;
}
</script>
The result of the demonstration is: 1 3 5 7 9, the above code: output all the base numbers of 1--10.
6. do-while loop
Format: do{
Code block
}while(expression)
do-while is very similar to while. I won’t demonstrate it~ The do-while statement will execute the statement in do at least once. But if the while statement is not satisfied, nothing will be executed.
Reprinting without permission is forbidden~ Come on today~ Aoli! ! !