Article From:https://www.cnblogs.com/xhh776554/p/9064214.html

Process control

1. Conditional statement

Divided into single branch statements; double branched statements; multiple branching statements.

1.Single branch statement — if

Grammar:

if(Judgment condition){A code block that is executed when the condition is true}

2.Double branch statement — if else

Grammar:

if(Judgment condition){A code block that is judged to be false.}Else{A code block that is executed when the condition is false}

3.Multiple branch statements — if else if

Grammar:

if(Judgment condition 1){A code block that is executed when condition 1 is true}Else if (judgment condition 2){A code block that is executed when condition 2 is true}Else if (judgment condition 3){A code block that is executed when condition 3 is true}

As long as one condition is true, it means that the whole sentence will end.

4.Multiple branch statements — switch

Grammar:

switch(Comparison value){Case compares the value of 1:case's comparison value with switch comparison value.Case compares the value of 1:case's comparison value with switch comparison value.Break; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /Comparison value of case comparison value 1:case and swiTCH code that compares straight at all equal valuesIf defaute:// does not find any congruent comparison values, if you need to do some operation, use defaute to guide the operation statement.}

loop

A.While cycle

Grammar:

while(Cycle conditions) {The code that is executed when the loop condition is true}
  • A variable is usually included in a cycle condition.
  • A block of code that is executed in true condition usually contains an operation that controls variables in the loop condition.

List:

let num=0;
while(num<5){
    console.log(num);
    num+=2;//Control the variables in the cycle condition}

Two.Do… While cycle

Grammar:

do{
    whileThe condition of the judgment is the true code block.}While (cycle condition);
  • Basically the same as the while cycle, the difference is that whether the cyclic condition is true or false, the code in braces will be executed and judged.

Three.For cycle

Grammar:

for(Initialization expression; conditional judgement expression; cyclic condition change) {The condition judgment is the real straight code block.}

forLoop application — array traversal output

let arr=[1,2,3,4,5];
for(let a=0;a<arr.length;a++){
    console.log(arr[a]);
}

Four. Control cycle

  • break;Terminate the whole cycle of his place
  • continue;Terminate the current loop and enter the next cycle
  • Generally speaking, it is necessary to terminate the loop after meeting certain conditions, add break/continue statements to the block of code that satisfies the conditions, and choose to use according to the requirements.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *