js code block and conditional judgment statement

Code block

  • The program is composed of one statement
  • Statements are executed one by one in order from top to bottom
  • In JS, you can use {} to group statements
  • The statements in the same {} are called a group of statements, they are either executed or not executed
  • A statement in {} is also called a code block
  • No need to write a semicolon after the code block
  • JS code blocks only have the function of grouping, and have no other purpose
  • The content of the code block is completely visible from the outside
    Insert picture description here
    . The code in {} is called a statement, and they are executed in order from top to bottom.
    When h{} is added to multiple statements, it becomes a code block, representing a whole.
    The content in the code block will not be ignored by the outside content and can still be executed.
    For example, in the above figure, the value of a can be output normally in the console

Conditional statement

  • Use conditional judgment statements to judge before executing a statement.
    If the condition is true, the statement will be executed, and if the condition is not true, the statement will not be executed.
  • if statement
  • Syntax 1:
    if (conditional expression) { statement... } 1. When the if statement is executed, it will evaluate the conditional expression first . 2. If the value of the conditional expression is true, execute the statement after the if 3 . If the value of the conditional expression is false, the statement after the if is not executed, and the if statement can only control the statement immediately following it





  • If you want the if statement to control multiple statements, you can put these statements in a code block
  • The code block after the if statement is not necessary, but try to write it in the development, even if there is only one statement.
    For example:

var a = 11;
if(a> 10 && a <= 20) { console.log("a is greater than 10 and less than or equal to 20"); }

When the value of a is greater than 10 and less than or equal to 20, the following statement is executed, otherwise it is not executed

  • Syntax 2:
    if (conditional expression) { statement... }else{ statement... } if...else...statement




  • When the statement is executed, the conditional expression after the if will be evaluated first, if the value is true, the statement after the if is executed, and if the value is false, the statement after the else is executed

var a = 8;
if(a> 10 && a <= 20) { console.log("a is greater than 10 and less than or equal to 20"); }else{ console.log("a is less than or equal to 10"); }



When the value of the expression is true, execute the statement after if, otherwise execute the statement after else

  • Syntax 3:
    if(conditional expression){ statement... }else if(conditional expression){ statement... }else if(conditional expression){ statement... }else{ statement... }







  • if…else if…else语句
  • When the statement is executed, the conditional expression will be evaluated sequentially from top to bottom
  • judgment. If the value is true, execute the current statement, if the value is false, then continue to judge downward
  • If all the conditions are not met, the statement after the last else is executed
  • Only one code block in this statement can be executed. Once the code block is executed, the statement is directly ended
  • E.g:

var age =90;
if (age> 80) { console.log("You are already an old man"); }else if(age> 60){ console.log("You have retired"); }else if (age> 30){ console.log("You are middle-aged"); }else if(age> 17){ console.log("You are an adult"); }else{ console.log("Are you still Children"); }









When the first condition is met, output (you are already an old man) and end the statement directly

The prompt() function can pop up a prompt box. There will be a text box in the prompt box. The user can enter a paragraph in the text box. The function requires a string to be passed as a parameter, and the string will be used as the prompt of the prompt box. The return value of the text prompt() function is string.
For example:

var num1 = prompt("Please enter the first number:")

Save the entered content in the variable num1
Effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48769418/article/details/107849057