if/switch statement and for loop while&to….while in JS

In any programming language, it is necessary to support three types of process control required to satisfy program structuring:

  • Sequence control
  • Branch control (conditional control)
  • loop control

Sequential control: In program flow control, the most basic thing is sequence control. The program will be executed in top-down order.

if statement (commonly used statements in JS)

Classification: single branch, double branch, multiple branches, branch nesting

//Single branch
if (conditional expression){
   code snippet
}
//double branch
if (conditional expression) {
    Code snippet 1
}else {
   Code snippet 2
}
//Multiple branches
if (conditional expression 1){
    Code snippet 1
} else if (conditional expression 2){
   Code snippet 2
} else if (conditional expression 3){
   Code snippet 3
}else{
    Code snippet 4
}

() can be a value or an expression

//Single branch
var score = 78;
if(score>=60 & amp; & amp; score<=100){
  console.log('passed');
}

Switch

switch(n(variable)){

case value 1:

code snippet;

break;

casse value 2:

code snippet;

break;

default:

code snippet;

}

//Principle: First set the expression n (usually a variable). The value of the expression is then compared with the value of each case in the structure. If there is a match, the code block associated with the case is executed. Please use break to prevent the code from automatically running to the next case.

Note: () can only be a variable or value, not an expression

var d = new Date();
console.log(d); // Get the date and time object
var week = d.getDay(); // Get the day of the week
switch (week) {
  case 0:
    console.log('Sunday');
    break;
  case 1:
    console.log('Monday');
    break;
  case 2:
    console.log('Tuesday');
    break;
  case 3:
    console.log('Wednesday');
    break;
  case 4:
    console.log('Thursday');
    break;
  case 5:
    console.log('Friday');
    break;
  case 6:
    console.log('Saturday');
    break;
}

When to use if and when to use switch

if: The condition is a range or a specific value switch: The condition is a specific value. Judgments that can be implemented using switch can be implemented using if. However, judgments that can be implemented using if statements may not be implemented using switch.

Loop control statement:

while statement

While loops loop through a block of code while a specified condition is true. While loop, conditional judgment is performed first, and then the code of the loop body is executed.

expression:

while (conditional expression) {

loop body

}

Note: In the current loop, if the conditions are not met, it will not be executed even once.

var i= 1;

while(i<=10){

console.log(i);

i + + ;

}

do…while

The do/while loop is a variation of the while loop. The loop will execute the block of code once, before checking whether the condition is true, and then repeat the loop if the condition is true. The do while loop executes the loop body code first, and then performs conditional judgment. Execute the code of the loop body at least once.

1

expression:

do{

code snippet

}while (conditional expression)

Note: In the current loop, the code is executed at least once

var i=1;

do{

i + + ; // 2

console.log(i);

}while(i<=10)

for loop

for (loop variable initialization expression; loop conditional expression; update loop variable expression) {

loop body

}

Notice:

  1. Loop variables can be defined outside the for loop structure
  2. If the loop variable is not updated, it will become an infinite loop.
  3. ;’ in the loop condition cannot be omitted

Loop out 5 div tags

for(var i=o;i<5;i + + ){
   document.write('<div>1</div>')
}

Loop output 1-100

for(var i=1;i<=100;i + + ){
  document.write(i);
}

Write an inverted pyramid using a for loop

var n=window.prompt("9");
document.write("Inverted Pyramid<br/>");
        for(i=1;i<=n;i + + ){
            for(j=1;j<=i-1;j + + ){
                document.write(" & amp;nbsp;");
            }
            for(k=1;k<=2*(n-i + 1)-1;k + + ){
                document.write("*");
            }
            document.write("<br/>");
        }

99 multiplication table

 for (var a = 1; a <= 9; a + = 1) {
        d=1 + '×' + a
        console.log(d);
        for (var c = 1; c<=a; c + =1 ) {
            // document.write("<div>" + c + '×' + a + "</div>");
            document.write("<div>" + c + '×' + a + '=' + c*a + "</div>");
            
        }
          document.write('<br/>')
            
    }

for…in

for (variable in object) {

code snippet

}

  • The for…in statement is used to iterate through each attribute of the object. Save the attribute name as a string in a variable each time
  • Used when it is impossible to predict any information about the object and the number of cycles
var arr = [
  {
    "name":"Java programming ideas",
    "price":78.9
  },
  {
    "name":"Python entry guide",
    "price":59
  },
  {
    "name":"JavaScript Programming",
    "price":69
  }
];
for(var i=0;i<arr.length;i + + ){
  // console.log(arr[i]);
  // json - {"name":"Java Programming Thoughts","price":78.9}
  for(key in arr[i]){
    console.log(arr[i][key]);
  }
}

Jump statement

continue

The continue statement can only be used in loops (while, do…while, for) statements to terminate the current loop and allow the next loop to continue based on the control expression.

var iNum = 0;
for (var i=1; i<10; i + + ) {
  if (i % 5 == 0) {
    document.write(i);
    continue;
  }
  iNum + + ;
}
alert(iNum);
break

The break statement can only be used in loops (while, do…while, for) or switch statements. The break statement is used to exit the loop and prevent repeated execution of any code or exiting a switch statement.

for (var i=1; i<10; i + + ) {
  if (i % 5 == 0) {
    break;
  }
  iNum + + ;
}
alert(iNum);