[Micro-build low-code] JavaScript basics - loop and conditional control

We explained the basics of variables in the last article , and in this section we continue to introduce loops and conditional control statements in javascript. Before introducing loops, we first need to introduce the basics of expressions

expression

There are several kinds of expressions in javascript, one is arithmetic calculation expression and the other is logical calculation expression. Arithmetic calculation expressions are expressed by arithmetic operators, such as

1+1

This is doing the addition of numbers and the result is equal to 2. Arithmetic operators also follow the rules of four operations, such as multiplication and division followed by addition and subtraction

1+2*3

The result of this expression is 7, multiply 2 by 3 to get 6, and then add it up. Arithmetic operators can use parentheses to change precedence, such as this

(1+2)*3

The parentheses are used to sum and then multiply. Generally, if you are not sure about the priority, it is best to enclose it in parentheses.

If it is not a number operation, such as a number on the left and a string on the right, then the + sign becomes a string concatenation, such as

1+2

After the connection, it becomes 12, which does not mean 12. It is equivalent to the connection of strings 1 and 2. The type of connection is also a string. This is also why some beginners will ask why the calculation is not carried out. It depends on the type of your variable, whether it is a number or a string.

In addition to arithmetic expressions, another commonly used expression is conditional expressions. Conditional expressions generally use comparison operators.

operator illustrate
< less than
> more than the
== Are they equal
=== Identity, the value and type must be the same
<= less than or equal to
>= greater or equal to
! = not equal to

It is generally difficult for beginners to understand the comparison operator. The comparison operator will calculate the value of the expression and return true or false. We generally construct an expression, such as

3>0

Mathematically, 3 is greater than 0, then this formula is true, and it returns true, if

1>3

Returns false if this expression does not hold. Usually we don't write constants for comparison, often variable comparisons. For example, we return a result from the data source, and use the total number of returned items to compare with the number, such as

result.total>0

I decide the execution logic of the subsequent code by whether the number of records is greater than 0

cycle

After we understand the meaning of expressions, we often apply them to loops. A loop is to help us perform one thing repeatedly. For example, when we count the beans, each time we take a bean, the count is 1, and when we take another bean, the count is 2. Repeated execution is a kind of cycle. There are various syntaxes for loops, usually there are while loops, for loops. The syntax of while loop is

while(表达式){
    
    
	执行需要重复执行的动作
	改变表达式的计算规则,有可能是让循环变量+1或者-1
}

Let's demonstrate with code

let loops = 5
while(loops>0){
    
    
	console.log(loops);
	loops = loops-1;
}

The execution steps of the loop are:
1. First define a variable loops and assign the value to 5
2. Check the value of loops>0, now 5>0, the result of the expression is true
3. If the condition is true, enter the loop
4. Print the loop variable, currently output 5 on the console
5, the loop variable is reduced by 1, the current value of loops is 4

Then repeat it again
1. Check the value of loops>0, now it is 4>0, the result of the expression is true
2. If the condition is true, enter the loop
3. Print the loop variable, currently outputting 4 in the console
4. The loop variable is decremented by 1, and the current value of loops is 3

Repeat the execution
1. Check the value of loops>0, now it is 3>0, the result of the expression is true
2. If the condition is true, enter the loop
3. Print the loop variable, currently outputting 3 on the console
4. The loop variable Subtract 1, the current value of loops is 2

Repeat the execution
1. Check the value of loops>0, now it is 2>0, the result of the expression is true
2. If the condition is true, enter the loop
3. Print the loop variable, currently outputting 2 on the console
4. The loop variable Subtract 1, the current value of loops is 1

Repeat the execution
1. Check the value of loops>0, now it is 1>0, the result of the expression is true
2. If the condition is true, enter the loop
3. Print the loop variable, currently outputting 1 on the console
4. The loop variable Subtract 1, the current value of loops is 0

Repeat the execution
1. Check the value of loops>0, now it is 0>0, the expression evaluates to false
2. If the condition is false, it will not enter the loop, and the loop will end

For beginners in code development, perhaps the most difficult thing to understand is the loop. You can follow my steps and calculate the results of each execution in your mind in the book, and then you will understand what it means.

Conditional control

In addition to cyclic execution, there is also a conditional execution, the syntax is

if(条件){
    
    

}else if(条件){
    
    

}else{
    
    
}

Each if is a branch. It needs to check whether the return result of the condition is true. If it is true, enter this branch. If the condition is not satisfied, execute the last else statement.

Conditional control usually helps us make decisions, just like in reality, if it rains, I will stay at home, and if it does not rain, I will go out, then using conditional control to achieve it is

if(天下雨){
    
    
  我就在家
}else{
    
    
	我就出门
}

What we usually call an algorithm is actually a kind of logic. As long as you are proficient in these two grammars, you can get started with basic program development. Of course, only logic is not enough. We usually also need data structures, such as arrays, Maps, and Sets. Different data structures are used to solve different problems and need to be carefully studied.

Summarize

This article introduces the basic syntax of javascript, such as expressions, loops, and conditional control. Beginners need to be proficient in programming before they can complete functional development according to their own needs.

Guess you like

Origin blog.csdn.net/u012877217/article/details/127192351